If Statement Optomization, Macro takes too long...

cicconmr

Board Regular
Joined
Jul 5, 2011
Messages
90
I'm assuming this is just a perpetual problem with all programs, but particularly worse with Visual Basic and the nature of excel.

I have the below code which tests a cell with a basic IF statement...if it is of a certain value, it sets values in other sheets. The problem is when iterating through 500+ Cells this code takes forever.

Any recomendations for speeding this up??

Code:
For Each Cell In ThisWorkbook.Worksheets("Portfolio Drop").Range("AB2:AB1000")
    
        If Cell.Value > mcLarge Then
        
            i = i + 1
            
            ThisWorkbook.Worksheets("Large Cap").Range("A" & i).Value = ThisWorkbook.Worksheets("Portfolio Drop").Range("A" & Cell.Row).Value
            ThisWorkbook.Worksheets("Large Cap").Range("C" & i).Value = ThisWorkbook.Worksheets("Portfolio Drop").Range("B" & Cell.Row).Value
        
        
        ElseIf Cell.Value > mcMidB And Cell.Value < mcLarge Then
        
            k = k + 1
            
            ThisWorkbook.Worksheets("Mid Cap").Range("A" & k).Value = ThisWorkbook.Worksheets("Portfolio Drop").Range("A" & Cell.Row).Value
            ThisWorkbook.Worksheets("Mid Cap").Range("C" & k).Value = ThisWorkbook.Worksheets("Portfolio Drop").Range("B" & Cell.Row).Value
        
        
        ElseIf Cell.Value < mcMidB And Cell.Value > 0 Then
        
            j = j + 1
            
            ThisWorkbook.Worksheets("Small & Micro Cap").Range("A" & j).Value = ThisWorkbook.Worksheets("Portfolio Drop").Range("A" & Cell.Row).Value
            ThisWorkbook.Worksheets("Small & Micro Cap").Range("C" & j).Value = ThisWorkbook.Worksheets("Portfolio Drop").Range("B" & Cell.Row).Value
        
        ElseIf Cell.Value = 0 Then
            
            tol = tol + 1
            
            If tol = 10 Then
                Exit Sub
            End If
        
        End If
    
    Next
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I suspect you probably need to turn off calculation at the beginning and turn it back on at the end.
 
Upvote 0
Ingenious!

What is the way to do this via VBA? I'm assuming it's something like...application.calucation = manual or something?

Matt
 
Upvote 0
Code:
Application.calculation = xlcalculationmanual
' your code here
Application.calculation = xlcalculationautomatic
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,762
Members
452,940
Latest member
rootytrip

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