VBA Else IF statement

bluegold

Active Member
Joined
Jun 21, 2009
Messages
279
Hi guys, the following macro seems to run a little slow, is there anyway of making the code more efficient / run faster ?


Sub Adjust()
'
' Adjust Macro
'
' Keyboard Shortcut: Ctrl+a
'

Dim lr1 As Long
Dim lr2 As Long

Application.ScreenUpdating = False

Range("A2:AQ" & Range("A" & Rows.Count).End(xlUp).Row).Sort _
Key1:=Range("A2"), Order1:=xlAscending

lr1 = Cells(Rows.Count, "D").End(xlUp).Row
lr2 = Cells(Rows.Count, "AP").End(xlUp).Row

If lr1 < lr2 Then
Range("D2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ElseIf lr1 > lr2 Then
Range("AP2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ElseIf lr1 = lr2 Then
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
End If

ActiveSheet.Calculate


Application.ScreenUpdating = False


End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
The more versed persons here may see something more but I've added a few thoughts.

Code:
Sub Adjust()
'
' Adjust Macro
'
' Keyboard Shortcut: Ctrl+a
'

Dim lr1 As Long
Dim lr2 As Long

Application.ScreenUpdating = False
[B][COLOR=#ff0000]Application.Calculation = xlCalculationManual[/COLOR][/B]
Range("A2:AQ" & Range("A" & Rows.Count).End(xlUp).Row).Sort _
Key1:=Range("A2"), Order1:=xlAscending

lr1 = Cells(Rows.Count, "D").End(xlUp).Row
lr2 = Cells(Rows.Count, "AP").End(xlUp).Row

If lr1 < lr2 Then
Range("D2").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
ElseIf lr1 > lr2 Then
        Range("AP2").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Select
    ElseIf lr1 = lr2 Then
    Range("A2").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
End If

'ActiveSheet.Calculate  [B][COLOR=#ff0000] I turned this off earlier and back on again below[/COLOR][/B]


Application.ScreenUpdating = [B][COLOR=#800080]True    ' You also had this set to False[/COLOR][/B]
[B][COLOR=#ff0000]Application.Calculation = xlCalculationAutomatic[/COLOR][/B]

End Sub

Does any of that make a difference?
 
Upvote 0
Thanks Brian for your reply, your code did lead me to turning off active calculation and it made a massive difference as my sheet had over 1500 rows of data. I decided I can get away with just doing a full calculation end of day rather than update sheet by sheet all the time.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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