我需要从动态数组的元素中声明变量变量。
我有一个数组(arrWS),其中包含手动填充的特定工作表的名称,我想声明" N"范围对象,其中" N"是工作表的数量。
序列: 1-声明包含一些工作表名称的数组==> varAAA = Array(" Sheet1"," Sheet3"," Sheet4",...)
2-获取此数组中元素的数量
3-如果可能,那就这样:
Dim varBBB(1 To N)
For i = 1 to N
varBBB(i) = Sheets(varAAA(i)).UsedRange
Next N
提前非常感谢你。
答案 0 :(得分:1)
您的主要问题是您必须Set
对象变量:
'1- Declare an array containing some worksheet names
' ==> varAAA = Array("Sheet1", "Sheet3", "Sheet4", ...)
Dim VarAAAA
varAAA = Array("Sheet1", "Sheet3", "Sheet4")
'2- Get the number of elements in this array
Dim N As Long
N = UBound(varAAA) - LBound(varAAA) + 1
'3- Then like that if possible:
'Dim varBBB(1 To N)
Dim varBBB() As Range
ReDim varBBB(1 To N)
'For i = 1 to N
For i = 1 To N
'varBBB(i) = Sheets(varAAA(i)).UsedRange
Set varBBB(i) = Sheets(varAAA(i - 1 + LBound(varAAA))).UsedRange
'Confirm that ranges have been correctly set
Debug.Print i, varAAA(i - 1 + LBound(varAAA)), varBBB(i).Address
'Next N
Next
答案 1 :(得分:0)
也许你正在寻找类似的东西,
Dim arrWS() As String
Dim iLoop As Integer
With ThisWorkbook
ReDim arrWS(1 To .Sheets.Count)
For iLoop = 1 To .Sheets.Count
arrWS(iLoop) = .Sheets(iLoop).Name
Next
End With
然后您可以使用Ubound(arrWWS)
来获取数组的最后一个索引。
现在,您可以使用此代码在工作表中存储每个已使用的范围
Dim arrBB() As Variant
Dim arrHolder() As Variant
ReDim arrBB(1 To UBound(arrWS))
For iLoop = LBound(arrWS) To UBound(arrWS)
arrHolder() = Sheets(arrWS(iLoop)).UsedRange.Value
arrBB(iLoop) = arrHolder()
Next
End Sub
答案 2 :(得分:0)
你可以这样做:
Dim varBBB()
Redim varBBB(LBound(varAAA) to UBound(varAAA))
For i = LBound(varAAA) to UBound(varAAA)
varBBB(i) = Sheets(varAAA(i)).UsedRange
Next i