2014-08-29 5 views
2

S'il vous plaît aider debug: Erreur d'exécution « 438 » objet ne prend pas en charge cette propriété ou méthodeExcel VBA: Erreur d'exécution « 438 » objet ne prend pas en charge cette propriété ou méthode

Je suis ne sais pas pourquoi mon ConvertToStdDateFormat la fonction (InputRange comme Range) est de ne pas accepter la gamme 'ThisRange'

Voici ce que mon entrée ressemble

201301 201401  201301 201401 
201302 201402  201302 201402 
201303 201403  201303 201403 
201304 201404  201304 201404 
201305 201405  201305 201405 

est Ci-dessous le code

Sub trythis() 
Dim ThisRange As Range 
Dim MonthYear_array As Variant 
start_date_row = 1 
end_date_row = 12 

With ActiveSheet 
    Set ThisRange = .Range(Cells(start_date_row, 1), Cells(end_date_row, 2)) 
    MonthYear_array = .Range(Cells(start_date_row, 4), Cells(end_date_row, 5)).Value 
End With 

Call ConvertToStdDateFormat(ActiveSheet.Range(Cells(start_date_row,1), Cells(end_date_row, 2))) 
Call ConvertToStdDateFormat(ActiveSheet.ThisRange) 
End Sub 


Public Function GetMonthYearFormatted(InputDate) 
'InputDate should be in the format "201401" i.e. year(2014)month(01) 
    IPString = CStr(InputDate) 
    monthval = CInt(Right(IPString, 2)) 
    yearval = CInt(Left(IPString, 4)) 
    opDate = DateSerial(yearval, monthval, 1) 
    OPFormatDate = Month(opDate) & "-" & Year(opDate) 
    GetMonthYearFormatted = OPFormatDate 
End Function 

Function ConvertToStdDateFormat(InputRange As Range) 
    Dim temp_array As Variant 
    temp_array = InputRange 
    For colsC = 1 To UBound(temp_array, 2) 
     For rowsC = 1 To UBound(temp_array, 1) 
      temp_array(rowsC, colsC) = GetMonthYearFormatted(temp_array(rowsC, colsC)) 
     Next rowsC 
    Next colsC 
    InputRange.Resize(UBound(temp_array, 1), UBound(temp_array, 2)) = temp_array 
    ConvertToStdDateFormat = Null 
End Function 

Répondre

2

Il suffit de remplacer la ligne

Call ConvertToStdDateFormat(ActiveSheet.ThisRange) 

par

Call ConvertToStdDateFormat(ThisRange) 

et le code fonctionnera (la feuille de calcul où la plage se trouve est stocké dans l'objet de la plage elle-même et peut être référencé par ThisRange.Worksheet) .

Pour faciliter le débogage, il peut être utile de démarrer tous les modules avec la ligne Option Explicit. Cela applique la déclaration explicite de toutes les variables utilisées (c'est-à-dire les lignes Dim x as Integer).

Questions connexes