I'm generating an automated script for work that searches for data stored in multiple CSV files within a single directory, then consolidates those data into a single workbook. I'm able to extract the data without issues ( ie assign a range of values to an array), but what I can't seem to figure out is how to do the reverse operation.
The error that I always get when executing that part of my script is:
Run-time error '438': Object doesn't support this property or method.
What I've boiled the cause down to is the combination of .Range
and .Cells
(either works just fine by themselves if I change the code to assign a single array value to a single worksheet cell value). The error only arises once I try to assign an array to a range of cells. So, my question is: what method is supported by the object-oriented approach that I'm pursuing for the simplified script I've written below?
Dim testarray(1 To 2, 1 To 2) As Integer
testarray(1, 1) = 1
testarray(1, 2) = 2
testarray(2, 1) = 3
testarray(2, 2) = 4
Set Target = Workbooks("_ConsolidatedData.xlsm")
Set wb = Target
Set ws = wb.Worksheets(1)
ws.Range(.Cells(1, 1), .Cells(2, 2)).Value = testarray(2, 2)
ws.Range(.Cells(1, 1), .Cells(2, 2)).Value = testarray(2, 2)
This line is missing a With Block
With ws
.Range(.Cells(1, 1), .Cells(2, 2)).Value = testarray 'no (2 ,2)
End with
Either of these will do what you want though, without needing adjustment if your array size changes:
ws.Range("A1").Resize(ubound(testarray, 1), ubound(testarray, 2)).Value = testarray
ws.Cells(1, 1).Resize(ubound(testarray, 1), ubound(testarray, 2)).Value = testarray
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.