簡體   English   中英

Excel VBA:使用索引和匹配功能時發生類型不匹配錯誤

[英]Excel VBA:Type misMatch error in using Index and Match functions

當日期計數器更改時,我在索引和匹配函數中遇到錯誤。 遇到錯誤時,我寫了一條評論。 這是代碼:

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

當日期更改為1/2/2008時,我會遇到錯誤,該如何解決?

謝謝

進行了以下更改(在下面的代碼中標記為“”)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM