Changing code from populating real tables in worksheets to arrays to save time

jayse

New Member
Joined
Jun 21, 2017
Messages
1
My VBA code populates 3 different sheets in loops by column by column. The first column of first sheet populates from an existing table. Next, the first column of the second sheet looks at the first column of the first sheet and uses that data to make a decision for what it will populate with. Finally, the first column of the third sheet looks at the past two sheets and itself and then populates itself based on that info. The loop begins again, and 200+ columns on 3 sheets are populated.

I want to change this to calculate using arrays instead of physically populating the columns one at a time to speed up processing time.

I was having success until I reached this part of my code:

For Days= 2 To 200
For SerialNum = 2 To 200

If WorksheetFunction.CountA(OrdersSheet.Range(OrdersSheet.Cells(SerialNum, Day), OrdersSheet.Cells(SerialNum, OrderDays))) = 0 Then
OrdersSheet.Cells(SerialNum, OrderDays) = OrderQty

Else
OrdersSheet.Cells(SerialNum OrderDays) = Empty

End If

It looks at certain cells in the sheet to determine whether an order has already been placed using CountA
if an order has not been placed, it populated the cell with the order qty, if it has been placed (if any of the cells are nonzero), then it doesn't place an order

My question is how can I do this with an array instead of range in a worksheet? From my understanding you cannot use countA on an array.

Thanks in advanced
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Here's an article that may help understand how they work better: The Complete Guide to Using Arrays in Excel VBA - Excel Macro Mastery

Long story short, you can assign a range to an array & loop through the array to drastically improve code performance.

Code:
Sub test()
Dim rng As Range
Dim Arr As Variant
Dim R&, C&  'declared as Long


'define the range (many methods to do so)
Set rng = [B2].CurrentRegion
Arr = rng


'array is a matrix of rows and columns now.


For C = LBound(Arr, 2) To UBound(Arr, 2)
    For R = LBound(Arr, 1) To UBound(Arr, 1)
        'do something
    Next R
Next C


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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