I have a variant like these
var = sheet1.Range("A1:P3600").Value
I have done some operarions and pushed the unwanted rows to the top in the variant. Now I have to copy the var variant to the other sheet from a certain range.
sheet1.range("A3444:P" & i).value = var(range(cells(r,"A").cells(l,"P"))
that is say var(350 to end of var) should be copied to the other sheet. Is it possible ? can we do like that ?
One way is to dump the reduced array to a second array, then the second array to your range
The code below makes a variant array with 3600 rows by 16 columns (ie A:P), data is dumped into the array for sample data (note you already have this array as Var), then a variable is used as a marker to reduce the array to a second array, the second array is then written to the range.
Updated to match your exact data locations. In your case you have Var1 already (your Var), so you just need the second portion of the code that starts at
lngStop = 350 and make my code
Sub TestME() Dim Var1 Dim Var2 Dim lngCnt As Long Dim lngCnt2 As Long Dim lngCnt3 As Long Dim lngCnt4 As Long Dim lngStop As Long Var1 = Sheet1.Range([a1], [p3600]).Value2 For lngCnt = 1 To UBound(Var1, 1) For lngCnt2 = 1 To 16 Var1(lngCnt, lngCnt2) = "I am row " & lngCnt & " column " & lngCnt2 Next lngCnt2 Next lngCnt lngStop = 350 ReDim Var2(1 To UBound(Var1, 1) - lngStop + 1, 1 To UBound(Var1, 2)) For lngCnt3 = lngStop To UBound(Var1, 1) For lngCnt4 = 1 To UBound(Var1, 2) Var2(lngCnt3 - lngStop + 1, lngCnt4) = Var1(lngCnt3, lngCnt4) Next lngCnt4 Next lngCnt3 Sheet1.[a3444].Resize(UBound(Var2, 1), UBound(Var2, 2)).Value2 = Var2 End Sub
You can slap only a portion of your array onto the sheet, but only if that portion is at the top left of your array, i.e. only the first
n columns and the first
m rows. There is no straightforward way of slapping the the last
n columns and the last
m rows. In this case, you have to resort to transferring stuff to a second, smaller array, and then dump that onto the sheet, as in @brettdj’s answer — this works fine, but it’s a bit roundabout and too much coding for my taste.
Instead, if you could push your “unwanted rows” down to the bottom of your array, then it would be a one-liner to slap the top rows of that array onto a sheet (omitting the last 350).
Here’s an example where a 4 x 3 array is read in, and only the top-left 3 x 2 is slapped back onto the sheet. The secret is to make the target range smaller than the entire array.
Dim v v = Range("A2:C5") Range("E2:F4") = v