Tidying up VBA

Colleen45

Active Member
Joined
Jul 22, 2007
Messages
495
How would this VBA be tidied up?

ActiveWindow.ScrollRow = 960
ActiveWindow.ScrollRow = 957
ActiveWindow.ScrollRow = 955
ActiveWindow.ScrollRow = 950
ActiveWindow.ScrollRow = 946
ActiveWindow.ScrollRow = 942
ActiveWindow.ScrollRow = 939
ActiveWindow.ScrollRow = 933
ActiveWindow.ScrollRow = 928
ActiveWindow.ScrollRow = 921
ActiveWindow.ScrollRow = 915
ActiveWindow.ScrollRow = 909
ActiveWindow.ScrollRow = 904
ActiveWindow.ScrollRow = 897
ActiveWindow.ScrollRow = 891
ActiveWindow.ScrollRow = 884
ActiveWindow.ScrollRow = 877
ActiveWindow.ScrollRow = 871
ActiveWindow.ScrollRow = 863
ActiveWindow.ScrollRow = 858
ActiveWindow.ScrollRow = 854
ActiveWindow.ScrollRow = 846
ActiveWindow.ScrollRow = 841
ActiveWindow.ScrollRow = 834
ActiveWindow.ScrollRow = 829
ActiveWindow.ScrollRow = 821
ActiveWindow.ScrollRow = 809
ActiveWindow.ScrollRow = 793
ActiveWindow.ScrollRow = 786
ActiveWindow.ScrollRow = 777
ActiveWindow.ScrollRow = 768
ActiveWindow.ScrollRow = 758
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
With the Delete button?:)

Seriously, that's all you should need as far as I can see.
 
Upvote 0
Ok.... Thankyou
I never know what I can safely delete and what not
I know that anything that is equal to zero or false, I can delete
The rest I am concerned I may erase some of my recorded code
 
Upvote 0
You can delete all those lines of code, they serve no purpose in the code.
 
Upvote 0
Colleen

Sorry about appearing flippant but it really is as simple as that.

Any code like that can be deleted, the macro recorder records everything you do (well almost anything).

For example, I just recorded this macro.
Code:
Sub Macro3()
'
' Macro3 Macro
'
'
    ActiveWindow.LargeScroll Down:=0
    ActiveWindow.ScrollRow = 80
    ActiveWindow.ScrollRow = 81
    ActiveWindow.ScrollRow = 82
    ActiveWindow.LargeScroll Down:=1
    ActiveWindow.ScrollRow = 109
    ActiveWindow.ScrollRow = 110
    ActiveWindow.ScrollRow = 111
    ActiveWindow.ScrollRow = 112
    ActiveWindow.ScrollRow = 113
    ActiveWindow.ScrollRow = 114
    ActiveWindow.ScrollRow = 115
    ActiveWindow.ScrollRow = 116
    ActiveWindow.LargeScroll Down:=1
End Sub
All I did when the recorder was running was scroll about a bit, nothing else.

I thought I might have needed to do something, eg put a value in a cell, but I didn't.:)

So you can just delete lines like that in the recorded code, and concentrate on tidying up the rest with the help of the forum.:)
 
Upvote 0
This is what I mean by deleting lines equal to Zero or false, so that I may tidy it up, to make it more readable
Code:
 With Selection
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Font.Bold = True
    With Selection.Font
        .Name = "Calibri"
        .Size = 16
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With Selection.Font
        .Name = "Calibri"
        .Size = 14
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
 
Upvote 0
Well, no, you can't delete those lines every time you see them as they may be doing something. For example, if those properties were set to True before the macro was recorded, then the macro sets them to False. If you removed those lines from the macro, it wouldn't do that any longer and the properties would remain True.

So you can't apply the rules as simplictically as that.

Look for things which genuinely don't do anything, for example if you have:-
Code:
Range("A1").Select
Selection.Font.Bold = False
you could safely replace that with:-
Code:
Range("A1").Font.Bold = False
(provided your macro wasn't relying on A1 being selected in the code which followed)

In fact, deleting Selection.Font.Bold = False would be the wrong thing to do as it would alter the effect of the macro completely.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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