Manipulate data in array and insert into worksheet

Stevenn

Active Member
Joined
Feb 8, 2012
Messages
259
I'm loading data from a list object into memory with

Code:
Dim vtArray as Variant

Set vtArray = ActiveSheet.ListObjects(1).DataBodyRange

Can I choose a more efficient data type than Variant?

I need to set the value of one of the columns to 0 in all rows. I can do this with

Code:
Let ActiveSheet.ListObjects(1).ListColumns(7).Value = 0

before loading the range into the array. But isn't it faster to operate directly in the array rather than the worksheet?

When I'm done comparing and modifying the array, I want to insert the 7th column in the array into the 7th column in the sheet. How can I do this efficiently?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
In your example, since you're using the Set statement, you're actually assigning a range to your variable instead of an array. Once you've transferred the values to your variable, you can loop through the 7th column to set your values to zero. However, since you'll be transferring the calculated values from your 7th column of the array back to your worksheet, it would be faster to set the values in your worsheet to zero in the first place, as you've already shown. So maybe something like this...

Code:
[COLOR=darkblue]Dim[/COLOR] vtArray [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]

ActiveSheet.ListObjects(1).ListColumns(7).DataBodyRange.Value = 0

vtArray = ActiveSheet.ListObjects(1).DataBodyRange.Value

[COLOR=green]'Do stuff[/COLOR]
'
'

ActiveSheet.ListObjects(1).ListColumns(7).DataBodyRange.Value = Application.Index(vtArray, 0, 7)

Note that there's a limit of 65,536 rows when using the Index method of the Application object to transfer an array to the worksheet.

Hope this helps!
 
Upvote 0
I receive a type mismatch error even though I haven't exceeded the limit of 65k rows.

I'm doing exactly as you wrote and UBound(vtArray) returns 1775, so the limit has not been hit.

What can cause this error?
 
Upvote 0
Can you post the complete and exact code that you're using?

Also, can you specify which line of code causes the error?
 
Upvote 0
Can you post the complete and exact code that you're using?

Also, can you specify which line of code causes the error?

I have specified my array with

Code:
Dim vtArray As Variant

vtArray = Sheets(1).ListObjects(1).DataBodyRange.Value2

I'm modifying values in my array and finally inserting the new values from the array to the sheet

Code:
Let Sheets(1).ListObjects(1).ListColumns(7).DataBodyRange.Value2 = Application.WorksheetFunction.Index(vtArray, 0, 7)

It works with fewer rows so I wonder why I am hitting the limit even though I got so few rows.
 
Upvote 0
Can you confirm which line of code causes the error? When you hit Debug, which line gets highlighted?
 
Upvote 0

Forum statistics

Threads
1,215,312
Messages
6,124,197
Members
449,147
Latest member
sweetkt327

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top