Range used for loops based off criteria

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
843
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I need some help creating a line of code that will help me optimize my current VBA. I am running into performance issues because of running my loop off of each individual range but I can improve it by combining ranges. I have to apply each due to the size of my data. I have since improved it but need help to further optimize. Below is my dilemma and what I would need.

I can extract from my core system by identifier to limit the data by doing so I still reach my excel row limit. So I loop through using a named range (column B below), to avoid that and transfer just the data I need over, clearing and starting over. I can combine those ranges using variables and looking for a way to do that dynamically. A way to run as many combined without reaching my excel row limit. In my prep work I can get how many rows each range would generate (column C below). In this example I could run the first 12. Then the last 2. So I could cut my loop from 14 to 2. The NEW ranges would need to combine the records separated by a colon like this:

Loop 1 - G########:H########:M########:N########:0########:1########:2########:3########:4########:5########:6########:7########
Loop 2 - 8########:9########

WsSec where I extract from system
WsCus (sheet)
Book1
ABCD
1GG########36961374956
2HH########135
3MM########380
4NN########370
500########259320
611########186070
722########21008
833########16295
944########21225
1055########210450
1166########18517
1277########241000
1388########256900
1499########139590
Sheet1
Cell Formulas
RangeFormula
D1D1=SUM(C1:C14)


VBA Code:
'check if row limit will be exceeded if so then proceed with running per starting value of indentifier transfering to sheet
If TOTAL > 1048576 Then

'set for lot runs and clear then run it
With WsSec
    .Range("ITEM") = "I"
    .Range("OP") = "="
End With

t = Timer
' Run loop for range, clear, run, copy and paste into its respective sheet
  i = 0
  Do Until WsCus.Range("FILTER").Offset(i, 0) = ""
    FILTER = WsCus.Range("FILTER").Offset(i, 0)

'apply filter to start loop and activate sheet
With WsSec
    .Range("VALUE") = FILTER
    .Application.Calculation = xlManual
    .Range("A10").CurrentRegion.Delete
    .Activate
End With
Debug.Print "Worksheets(WsSec)Delete... :" & Format(Timer - t, "0.00") & " seconds"

t = Timer
    Call Sec2
Debug.Print "Worksheets(WsSec)... :" & Format(Timer - t, "0.00") & " seconds"

'Other code to transfer data, apply formulas, and etc...
 
Last edited:
So your saying where I have insert here?

VBA Code:
Sub CreateFilterArrays()
    Dim WsSec As Worksheet, WsCus As Worksheet
    
    Set WsSec = Worksheets("Sec")                                        ' Change to your sheet name
    Set WsCus = Worksheets("Check")                                        ' Change to your sheet name
    
    Dim cusRng As Range, cusLastRow As Long, cusArr As Variant
    Dim cusCollection As Collection, sCodes As String, lCodeSum As Long
    Dim i As Long
    
    With WsCus
        cusLastRow = .Cells(rows.count, "B").End(xlUp).row
        Set cusRng = .Range(.Cells(1, "A"), .Cells(cusLastRow + 1, "C"))    ' Add 1 row to simplify loop
        cusArr = cusRng.Value
    End With
    
    ' Create an item in a collection for each concatenated string which makes
    ' up a group totaling < Max allowed ie 1048576
    Set cusCollection = New Collection
    For i = 1 To UBound(cusArr) - 1
        sCodes = sCodes & ":" & cusArr(i, 2)
        lCodeSum = lCodeSum + cusArr(i, 3)

        If lCodeSum + cusArr(i + 1, 3) > 1048576 Or (i = UBound(cusArr) - 1) Then
            sCodes = Right(sCodes, Len(sCodes) - 1)
            cusCollection.Add sCodes
            sCodes = ""
            lCodeSum = 0
        End If
    Next i
    
    Dim collCodes As Variant
    Dim splitCode As Variant
    
    ' Loop through each collection of concatenated strings
    For Each collCodes In cusCollection
'Insert here?
    Next collCodes
    
End Sub
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Ok trying to do the below but not getting it how I would like it I want it to start at row 1. if I do the below Row 1 becomes blank. if I modify it to remove the +1 then the 2nd loop just overrides the E1. What may i be doing wrong?

VBA Code:
    ' Loop through each collection of concatenated strings
    For Each collCodes In cusCollection
 nrCus = WsCus.Range("E" & rows.count).End(xlUp).row + 1
 WsCus.Range("E" & nrCus).Value = collCodes
    Next collCodes
 
Upvote 0
On my mobile.

' Loop through each collection of concatenated strings
nrCus = WsCus.Range("E" & rows.count).End(xlUp).row
If nrCus = 1 and WsCus.Range("E" & nrCus).Value = "" then
nrCus = 0
Endif
For Each collCodes In cusCollection
nrCus = nrCus + 1
WsCus.Range("E" & nrCus).Value = collCodes
Next collCodes
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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