2016-07-12 9 views
0

Ich erhalte Fehler in Index-und Match-Funktionen, wenn der Zähler des Datums ändert. Ich habe einen Kommentar geschrieben, als ich mit einem Fehler konfrontiert werde. Hier ist der Code:Excel VBA: Typ misMatch-Fehler bei der Verwendung von Index und Match-Funktionen

Sub regionalAverage() 

Application.ScreenUpdating = False 

Application.DisplayStatusBar = False 

Application.EnableEvents = False 

ActiveSheet.DisplayPageBreaks = False 

Dim address(2) As String 
Dim rw As Variant 
Dim col As Variant 
Dim date_ini As Date 
Dim date_fin As Date 

    'create WorkSheet 

date_ini = #1/1/2008# 
date_fin = #1/4/2008# 
For j = 1 To 3 
    For conteo = date_ini To date_fin 
     For i = 1 To 2 
      With Sheets(i) 
       With Application 
       col = .Match(j, Worksheets(i).Range("F2:F23393"), 0) 
       rw = .Match(CLng(conteo), Worksheets(i).Range("D2:D23393"), 0) 
       address(i) = .Index(Worksheets(i).Range("H2:H23393"), col, rw) 
       ' the error appear here 
       End With 

      End With 
     Next i 
    ' computation 
     area = 6.429571 
     Sheets("Output").Activate 
     Range("A1").Select 
     ActiveCell.Offset(0, j).Select 
     colname = Split(ActiveCell(1).address(1, 0), "$")(0) 
     Columns("" & colname & ":" & colname & "").Select 
     Selection.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, _ 
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
      MatchCase:=False, SearchFormat:=False).Select 

     ActiveCell.Value = "=(SUM(" & address(1) & "," & address(2) & "))/" & area & "" 

    Next conteo 
Next j 

End Sub 

Wenn das Datum ändert sich auf 1/2/2008 Ich Gesicht mit Fehler, wie kann ich es lösen?!

Danke

+0

meinen Sie die Zeile mit' Adresse (i) = .Index' gemacht? oder danach die Zeile von "End With"? –

+0

Ich meine die Zeile mit 'Adresse (i) = .Index' –

+0

Sie deklariert' Dim-Adresse (2) als String' und hier, wenn i = 2, dann suchen Sie bereits Array-Mitglied 3, und Sie erhalten Ihren Fehler (Das Array Adresse() beginnt bei 0, dann 1). –

Antwort

0

folgende Änderungen (markiert mit "im Code unten)

Sub regionalAverage() 

Application.ScreenUpdating = False 
Application.DisplayStatusBar = False 
Application.EnableEvents = False 
ActiveSheet.DisplayPageBreaks = False 

' *** change the declaration here *** 
Dim address() As String 
Dim rw As Variant 
Dim col As Variant 
Dim date_ini As Date 
Dim date_fin As Date 

' *** add Redim here, so the index of the array will start from 1 *** 
ReDim address(1 To 2) 

date_ini = #1/1/2008# 
date_fin = #1/4/2008# 
For j = 1 To 3 
    For conteo = date_ini To date_fin 
     For i = 1 To 2 
      With Sheets(i) 
       With Application 
        col = .Match(j, Worksheets(i).Range("F2:F23393"), 0) 
        rw = .Match(CLng(conteo), Worksheets(i).Range("D2:D23393"), 0) 

        On Error Resume Next 
        address(i) = .Index(Worksheets(i).Range("H2:H23393"), rw, col) ' switched between rw (i think it's your row reference) and col 

        ' to help debug the error you get 
        If Err.Number <> 0 Then 
         MsgBox "Error number " & Err.Number & " in row " & rw & " Col " & col 
        End If 

        On Error GoTo 0 

       End With 

      End With 
     Next i 

    ' computation 
     area = 6.429571 
     Sheets("Output").Activate 
     Range("A1").Select 
     ActiveCell.Offset(0, j).Select 
     colname = Split(ActiveCell(1).address(1, 0), "$")(0) 
     Columns("" & colname & ":" & colname & "").Select 
     Selection.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, _ 
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
      MatchCase:=False, SearchFormat:=False).Select 

     ActiveCell.Value = "=(SUM(" & address(1) & "," & address(2) & "))/" & area & "" 

    Next conteo 
Next j 

End Sub 
+0

Welchen Fehler bekommst du jetzt? Kannst du einen Screenshot des Fehlers an deine ursprüngliche Nachricht anhängen? –

+0

Hast du es gelöst? Danke –

+0

@MajidJavanmard bereit meine Nachricht oben, fügen Sie bitte einen Screenshot der Fehler messgae Sie bekommen. Was ist der Wert von 'col',' rw'? –