Recorded macro not working the way i thought?

Troy1979

New Member
Joined
Oct 27, 2017
Messages
2
I am looking to some help to understand why i am having an issue with a recorded macro.
I want to hide multiple columns and have recorded a macro as below. My problem becomes that when I run the macro, it hides all columns from R:AZ but there are specific column i want left un-hidden.

Sub HIDE_FY18_BUDGETS()
'
' HIDE_FY18_BUDGETS Macro
'


'
Range("R:R,S:S,U:U,V:V,X:X,Y:Y,AA:AA,AB:AB,AD:AD,AE:AE,AG:AG,AH:AH").Select
Range("AH1").Activate
ActiveWindow.SmallScroll ToRight:=17
Range( _
"R:R,S:S,U:U,V:V,X:X,Y:Y,AA:AA,AB:AB,AD:AD,AE:AE,AG:AG,AH:AH,AJ:AJ,AK:AK,AM:AM,AN:AN,AP:AP,AQ:AQ,AS:AS,AT:AT,AV:AV,AW:AW,AY:AY" _
).Select
Range("AZ1").Activate
ActiveWindow.ScrollColumn = 26
ActiveWindow.ScrollColumn = 15
ActiveWindow.ScrollColumn = 1
Selection.EntireColumn.Hidden = True
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Take a look at this.
I have combined certain columns, as pairs, that I assume you want to hide.
The green in the code won't be seen by VBA. I've also take out some other unnecessary lines.

In effect, all that I assume you need to hide is the columns within the range which is in black, yes?
Code:
Sub HIDE_FY18_BUDGETS()
[COLOR=#00ff00]'
' HIDE_FY18_BUDGETS Macro
'


'
'Range("R:S,U:V,X:Y,AA:AB,AD:AE,AG:AG,AH:AH").Select
'Range("AH1").Activate[/COLOR]

Range("R:S,U:V,X:Y,AA:AB,AD:AE,AG:AH,AJ:AK,AM:AN,AP:AQ,AS:AT,AV:AW, AY:AY").Select
[COLOR=#00ff00]'Range("AZ1").Activate
[/COLOR]
Selection.EntireColumn.Hidden = True
End Sub
 
Upvote 0
Hi Troy,

Welcome to MrExcel!!

Which columns do you want left visible (I think it's the first line of code but I'm not sure).

Thanks,

Robert
 
Upvote 0
Try this:
Code:
Sub Hide_Columns()
Application.ScreenUpdating = False
For i = 23 To 1 Step -1
    Cells(1, Choose(i, "R", "S", "U", "V", "X", "Y", "AA", "AB", "AD", "AE", "AG", _
    "AH", "AJ", "AK", "AM", "AN", "AP", "AQ", "AS", "AT", "AV", "AW", "AY")).EntireColumn.Hidden = True
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
In fact, all black code can be replaced with one simple line:

Range("R:S,U:V,X:Y,AA:AB,AD:AE,AG:AH,AJ:AK,AM:AN,AP:AQ,AS:AT,AV:AW, AY:AY").EntireColumn.Hidden = True
 
Upvote 0
If it's the columns BrianJN1 suggests, you do it in a single line like so:

Code:
Range("R:S,U:V,X:Y,AA:AB,AD:AE,AG:AH,AJ:AK,AM:AN,AP:AQ,AS:AT,AV:AW,AY:AY").EntireColumn.Hidden = True

You can of course change these column references to suit.

Robert
 
Upvote 0
Thanks guys, this has worked :)

Take a look at this.
I have combined certain columns, as pairs, that I assume you want to hide.
The green in the code won't be seen by VBA. I've also take out some other unnecessary lines.

In effect, all that I assume you need to hide is the columns within the range which is in black, yes?
Code:
Sub HIDE_FY18_BUDGETS()
[COLOR=#00ff00]'
' HIDE_FY18_BUDGETS Macro
'


'
'Range("R:S,U:V,X:Y,AA:AB,AD:AE,AG:AG,AH:AH").Select
'Range("AH1").Activate[/COLOR]

Range("R:S,U:V,X:Y,AA:AB,AD:AE,AG:AH,AJ:AK,AM:AN,AP:AQ,AS:AT,AV:AW, AY:AY").Select
[COLOR=#00ff00]'Range("AZ1").Activate
[/COLOR]
Selection.EntireColumn.Hidden = True
End Sub
 
Upvote 0
Trebor79's version will certainly work, it is actually tidier.
I did consider it but I left it as two lines to indicate to you what lines I was taking out of the macro.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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