Excel VBA: how to solve Index and Match function type mismatch error

冷暖自知 提交于 2020-01-15 09:32:13

问题


I encounter error in index and match functions when the counter of date changes. I wrote a comment when I face with error. I uploaded a sample of my data if it needed. sample : http://s000.tinyupload.com/?file_id=00243748825638974221

here is the code :

Sub regionalAverage()

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

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

'create WorkSheet

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

date_ini = #1/1/2008#
date_fin = #1/2/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)
                   'error appeas here
                    aname(i) = .Index(Worksheets(i).Range("H2:H23393"), col, rw)  



               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(" & aname(1) & "," & aname(2) & "))/" & area &  ""

    Next conteo
Next j

End Sub

When the date changes to 1/2/2008 I face with error, how can I solve it ?!

Thank you


回答1:


Since you're using Application.Match and Variant data type, the error will not raise during the call to .Match, but those variables like col and rw will contain an Error type, if the value is not found in the search range/array.

This Error value will cause a TypeMismatch error when attempting to assign it to the String array, aname().

So, you're halfway there, you just need error handling:

col = .Match(j, Worksheets(i).Range("F2:F23393"), 0)
rw = .Match(CLng(conteo), Worksheets(i).Range("D2:D23393"), 0)

If Not IsError(col) and Not IsError(rw) Then
    aname(i) = .Index(Worksheets(i).Range("H2:H23393"), col, rw)
Else
    ' Do something else if there was an error

End If

Alternatively, Dim aName() as Variant, but then you would probably need the error handling further/elsewhere in your code, to handle the error values you're putting in to the array.

I also observe that Index seems to be the source of the error, and it is totally not needed here, because:

Index(range_object, row_num, col_num) is literally the same as range_object.Cells(row_num, col_num)

So instead, I did:

aname(i) = CStr(Worksheets(i).Range("H2:H23393").Cells(rw, col).Value)

NOTE: I also assumed that you originally have rw and col in the wrong position for the Index function, and that rw is a ROW number and col is a COLUMN number.



来源:https://stackoverflow.com/questions/38330849/excel-vba-how-to-solve-index-and-match-function-type-mismatch-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!