Arrays 创建具有可变元素数的数组变量
我想定义一个数组变量,根据搜索返回的m个结果,它的元素数是可变的。我获得了以下项所需的错误常量表达式:Arrays 创建具有可变元素数的数组变量,arrays,vba,variables,dynamic,constants,Arrays,Vba,Variables,Dynamic,Constants,我想定义一个数组变量,根据搜索返回的m个结果,它的元素数是可变的。我获得了以下项所需的错误常量表达式: Dim cmodels(0 To m) As String 这是我的完整代码 Dim cmodels(0 To m) As String Dim foundRange As Range Dim rangeToSearch As Range Set rangeToSearch = Selection Set foundRange = rangeToSearch.Find(What:="Lig
Dim cmodels(0 To m) As String
这是我的完整代码
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
请参阅以下代码注释:
Dim cmodels(0 To m) As String
Sub redimVsRedimPreserve()
'I generally declare arrays I know I will resize
'without a fixed size initially..
Dim cmodels() As String
Dim m As Integer
m = 3
'this will wipe out all data in the array
ReDim cmodels(0 To m) As String
'you can also use this if you want to save all information in the variable
cmodels(2) = "test"
ReDim Preserve cmodels(0 To m) As String
'this will still keep "test"
Debug.Print "With redim preserve, the value is: " & cmodels(2)
'using just 'redim'
ReDim cmodels(0 To m) As String
Debug.Print "with just redim, the value is: " & cmodels(2)
End Sub
还请注意,经常使用Redim PREVICE(如循环)可能需要一些时间。请参阅以下代码注释:
Dim cmodels(0 To m) As String
Sub redimVsRedimPreserve()
'I generally declare arrays I know I will resize
'without a fixed size initially..
Dim cmodels() As String
Dim m As Integer
m = 3
'this will wipe out all data in the array
ReDim cmodels(0 To m) As String
'you can also use this if you want to save all information in the variable
cmodels(2) = "test"
ReDim Preserve cmodels(0 To m) As String
'this will still keep "test"
Debug.Print "With redim preserve, the value is: " & cmodels(2)
'using just 'redim'
ReDim cmodels(0 To m) As String
Debug.Print "with just redim, the value is: " & cmodels(2)
End Sub
还请注意,经常使用Redim Preserve(如循环)可能需要一些时间。Dim cmodels as String Dim cmodels as String会导致错误,Dim cmodels as String会产生与所问问题相同的问题……好的,现在请注意,这是VBA。。。与VB.Dim cmodels as StringDim cmodels as Strings不同,Dim cmodels as Strings会导致错误,Dim cmodelsm as Strings会给出与被询问的问题相同的问题…对,现在请注意,这是VBA。。。与VB不同,这已经成为一个流行的问题;这已经成为一个流行的问题;恩德兰的反应非常好。
Dim cmodels(0 To m) As String