Excel VBA:增量variables中的types不匹配

我对VBA相当陌生,而且我一直在为这一小段代码争取了一整天的时间,所以我转向你,我的知识上级。

我有以下情况:

最终目标是获得4个variables:ADR1,ADR2,ADR3和ADR4。 这些variables应该由IF函数创build,IF函数将4组单元与一个目标单元(在IF和ELSIF部分中find)进行比较。

当find这些单元格之间的匹配时,我想将一个特定的单元格值赋给variablesADR1。 一旦将值添加到variablesADR1中,循环将再次开始为ADR2分配一个值,依此类推直到ADR4。

为了创build四个variablesADR1-4,我使用了“for I = 1 to 4”语句。

当我执行这个代码时,我得到了'ADR(I)= Cells(1,“B”)行'types不匹配错误。'值'所以第一个ELSIF部分。

你们有没有人可以向我解释为什么我得到这个不匹配,应该如何解决?

Sub variable_incrementation() 'resetting variables ADR1 = 0 ADR2 = 0 ADR3 = 0 ADR4 = 0 Dim N As Integer N = 4 Dim ADR(1 To 4) As Long Dim I As Long For I = 1 To 4 Do 'N is supposed to increase by steps of four due to the target cells that contain the matching information N = N + 4 If Activesheet.Cells.(2, "A") = Activesheet.Cells.(N, "D") Or _ Activesheet.Cells.(3, "A") = Activesheet.Cells.(N, "D") Then ADR(I) = Activesheet.Cells.(1, "A").Value ElseIf Activesheet.Cells.(2, "B") = Activesheet.Cells.(N, "D") Or _ Activesheet.Cells.(3, "B") = Activesheet.Cells.(N, "D") Or _ Activesheet.Cells.(4, "B") = Activesheet.Cells.(N, "D") Then ADR(I) = Activesheet.Cells.(1, "B").Value ElseIf Activesheet.Cells.(2, "C") = Activesheet.Cells.(N, "D") Or _ Activesheet.Cells.(3, "C") = Activesheet.Cells.(N, "D") Or _ Activesheet.Cells.(4, "C") = Activesheet.Cells.(N, "D") Or _ Activesheet.Cells.(5, "C") = Activesheet.Cells.(N, "D") Then ADR(I) = Activesheet.Cells.(1, "C").Value ElseIf Activesheet.Cells.(2, "D") = Activesheet.Cells.(N, "D") Or _ Activesheet.Cells.(3, "D") = Activesheet.Cells.(N, "D") Or _ Activesheet.Cells.(4, "D") = Activesheet.Cells.(N, "D") Then ADR(I) = Activesheet.Cells.(1, "D").Value Else End If Loop Until (IsEmpty(ADR) = False) Or (N <= 43) Next I For J = 1 To 4 MsgBox (ADRJ) Next End Sub 

你已经声明数组为Long

 Dim ADR(1 To 4) As Long 

因此,当你尝试存储一个不是数字的值时,你会得到Type Mismatch错误。 您可能希望将其声明为Variant或进行其他检查以查看它是否为数字