我是VBA的新手,我已经用这一小段代码进行了一天以上的战斗,所以我转向你,我的知识上司。
我有以下情况:
最终目标是获得4个变量:ADR1,ADR2,ADR3和ADR4。这些变量应该由IF函数创建,该函数将4组单元格与一个目标单元格(在IF和ELSIF部分中找到)进行比较。
当找到这些单元格之间的匹配时,我想将某个单元格值分配给变量ADR1。一旦将值添加到变量ADR1,循环将再次开始为ADR2分配值,依此类推,直到ADR4。
为了创建四个变量ADR1-4,我使用了一个'用于I = 1到4'言。
当我执行此代码时,我会收到类型不匹配错误'在线' ADR(I)=细胞(1," B")。值'所以第一个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
答案 0 :(得分:1)
您已将数组声明为Long
Dim ADR(1 To 4) As Long
因此,当您尝试存储不是数字的值时,您将收到Type Mismatch
错误。您可能希望将其声明为Variant
或进行其他检查以查看它是否为数字