I am trying to put a range in an array so I can just loop them. Since there are lot of sheets that has the same range. But excel is returning an error "Object Variable or With block variable not set" on the assigning of values. The one with("**"). How can I fix the error and make it work?
Dim TableOldData(0 To 6) As Range
**TableOldData(0) = Range("F6:P8")**
TableOldData(1) = Range("F30:P32")
TableOldData(2) = Range("F54:P56")
TableOldData(3) = Range("F78:P80")
TableOldData(4) = Range("F102:P104")
TableOldData(5) = Range("F126:P128")
TableOldData(6) = Range("F150:P152")
Dim ColumnClear(0 To 6) As Range
ColumnClear(0) = Range("P6:P8")
ColumnClear(1) = Range("P30:P32")
ColumnClear(2) = Range("P54:P56")
ColumnClear(3) = Range("P78:P80")
ColumnClear(4) = Range("P102:P104")
ColumnClear(5) = Range("P126:P128")
ColumnClear(6) = Range("P150:P152")
Dim NewValue(0 To 6) As Range
NewValue(0) = Range("B6:B8")
NewValue(1) = Range("B30:B32")
NewValue(2) = Range("B54:B56")
NewValue(3) = Range("B78:B80")
NewValue(4) = Range("B102:B104")
NewValue(5) = Range("B126:B128")
NewValue(6) = Range("B150:B152")
Dim i As Integer
i = 0
Dim RowPaste As Integer
RowPaste = 6
While i <> 6
TableOldData(i).Select
Selection.Copy
Cells(RowPaste, 5).Select
ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, _
IconFileName:=False
ColumnClear(i).Select
Selection.ClearContents
NewValue(i).Select
Selection.Copy
Cells(RowPaste, 16).Select
ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, _
IconFileName:=False
i = i + 1
RowPaste = RowPaste + 24
Wend
Do this:
Dim Buf() As Variant ' only Variant type is accepted
Buf = Range("F5:H10")
This method results in a 3x6 array. This way you put the data into an array that you can loop. There is another way for managing range references that is more complicated:
Dim rData as Range, r as Range
Set rData = Range("F5:H10")
For Each r In rData
Debug.Print r.Value
Next r
Data is taken left to right then down order.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.