Slow Macro

jameshunt83

Board Regular
Joined
Oct 2, 2010
Messages
149
Hi, I'm not very good with VBA really, but learning! Have written the below code, however its very slow! I'm sure there must be a better way of doing this

Code:
Sub basic_quote_rows()
Dim i As Long
Dim j As Long
i = 6
j = 19
Do While j < 95 And j > 19
If Sheets("material").Rows(i).Hidden = True Then Sheets("quotebasic").Rows(j).Hidden = True Else Sheets("quotebasic").Rows(j).Hidden = False
i = i + 1
j = j + 1
Loop

Dim x As Long
Dim y As Long
i = 92
j = 102
Do While j < 126 And j > 101
If Sheets("material").Rows(i).Hidden = True Then Sheets("quotebasic").Rows(j).Hidden = True Else Sheets("quotebasic").Rows(j).Hidden = False
i = i + 1
j = j + 1
Loop

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
See if this is any faster?

Rich (BB code):
Sub basic_quote_rows()
Dim i As Long
Dim j As Long
application.screenupdating = false
application.calculation = xlcalculationmanual
i = 6
j = 19
Do While j < 95 And j > 19
If Sheets("material").Rows(i).Hidden = True Then Sheets("quotebasic").Rows(j).Hidden = True Else Sheets("quotebasic").Rows(j).Hidden = False
i = i + 1
j = j + 1
Loop

Dim x As Long
Dim y As Long
i = 92
j = 102
Do While j < 126 And j > 101
If Sheets("material").Rows(i).Hidden = True Then Sheets("quotebasic").Rows(j).Hidden = True Else Sheets("quotebasic").Rows(j).Hidden = False
i = i + 1
j = j + 1
Loop

application.calculation = xlcalculationautomatic
application.screenupdating = true
End Sub
Hope that helps.
 
Upvote 0
I mean it looks like a basic loop and don't really see much else to speed it up. I like to use for statements rather than do statements, but everyone has their own preferences.

Rich (BB code):
Sub basic_quote_rows()
Dim i As Long
Dim j As Long
application.screenupdating = false
application.calculation = xlcalculationmanual

for j = 20 to 95
i = j - 13
If Sheets("material").Rows(i).Hidden = True Then Sheets("quotebasic").Rows(j).Hidden = True Else Sheets("quotebasic").Rows(j).Hidden = False
next j

for j = 102 to 125
i = j-10
If Sheets("material").Rows(i).Hidden = True Then Sheets("quotebasic").Rows(j).Hidden = True Else Sheets("quotebasic").Rows(j).Hidden = False
next j

application.calculation = xlcalculationautomatic
application.screenupdating = true
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,844
Members
452,948
Latest member
UsmanAli786

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