[英]Excel VBA: how to solve Index and Match function type mismatch error
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
范例: 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 ?! 当日期更改为1/2/2008时,我会遇到错误,该如何解决?
Thank you 谢谢
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. 由于您使用的是
Application.Match
和Variant
数据类型,因此在调用.Match
不会出现Error
,但是如果在搜索范围/数组中找不到该值,则col
和rw
等变量将包含Error
类型。 。
This Error
value will cause a TypeMismatch
error when attempting to assign it to the String
array, aname()
. 尝试将其分配给
String
数组aname()
时,此Error
值将导致TypeMismatch
错误。
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. 或者,将
Dim aName() as Variant
,但是您可能需要在代码中的其他地方进一步处理错误,以处理放入数组中的错误值。
I also observe that Index
seems to be the source of the error, and it is totally not needed here , because: 我还观察到
Index
似乎是错误的根源, 这里完全不需要它 ,因为:
Index(range_object, row_num, col_num)
is literally the same as range_object.Cells(row_num, col_num)
Index(range_object, row_num, col_num)
实际上与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. 注意:我还假设您最初将
rw
和col
放在Index
函数的错误位置,并且rw
是ROW号,而col
是COLUMN号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.