2017-01-05 57 views
1

我想知道是否可以通過for..next循環定義範圍變量?VBA - 通過循環定義範圍變量

類似的東西

Public Sub DefineRanges() 
Dim i As Long 
Dim rngLine1, rngLine2 As Range 

For i = 1 To 2 
    Set Replace("rngLinex", "x", i) = ThisWorkbook.Sheets("Sheet1").Range("A" & i) 
Next i 
End Sub 

或類似的

Public Sub DefineRanges() 
Dim i As Long 
Dim rngLine1, rngLine2 As Range 

For i = 1 To 2 
    Set rngLine & i = ThisWorkbook.Sheets("Sheet1").Range("A" & i) 
Next i 
End Sub 

預先感謝您。

回答

4

由於@JohnColeman說明使用範圍的數組:

Sub defineranges() 
Dim i As Long 
Dim RngArr(1 To 2) As Range 

For i = 1 To 2 
    Set RngArr(i) = ThisWorkbook.Worksheets("Sheet1").Range("A" & i) 
Next i 

For i = 1 To 2 
    Debug.Print RngArr(i).Value 
Next i 
End Sub 
+0

這是要走的路。我的第二個建議實際上並不奏效(至少不是很明顯),所以我刪除了這個評論。 –

+0

完美..非常感謝你 – user3286479

+0

當我嘗試應用相同的thig字符串它並沒有真正的工作..任何建議嗎? Dim strArr(1 To 2)As String似乎適用? – user3286479