我想定义一个数组变量,使其具有可变数量的元素,具体取决于从搜索返回的 m 个结果。我在以下位置收到错误“需要常量表达式”:
Dim cmodels(0 To m) As String
这是我的完整代码
Dim foundRange As Range
Dim rangeToSearch As Range
Set rangeToSearch = Selection
Set foundRange = rangeToSearch.Find(What:="Lights", After:=ActiveCell,
LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False) 'First Occurrence
m = WorksheetFunction.CountIf(Selection, "Lights")
Dim secondAddress As String
If (Not foundRange Is Nothing) Then
Dim count As Integer: count = 0
Dim targetOccurrence As Integer: targetOccurrence = 2
Dim found As Boolean
z = 1
Dim cmodels(0 To m) As String
Do Until z = m
z = z + 1
foundRange.Activate
Set foundRange = rangeToSearch.FindNext(foundRange)
If Not foundRange.Next Is Nothing Then
z(m) = ActiveCell(Offset(0, 2))
End If
Loop
End If
End Sub