MAcro Hide Range help

ExcelRoy

Well-known Member
Joined
Oct 2, 2006
Messages
2,540
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have the following code that only hides columns C:GM instead of C:GW

Has anyone any idea why this would be? Maybe a conflict?
Code:
Sub View_Work()
    Sheets("Estimate").Unprotect Password:="1234"
    Columns("C:IN").Select
    Selection.EntireColumn.Hidden = False
    Columns("C:GW").Select
    Selection.EntireColumn.Hidden = True
    Columns("IA:IN").Select
    Selection.EntireColumn.Hidden = True
    Columns("GX:HZ").Select
    Selection.EntireColumn.Hidden = False
    Range("A1").Select
    Sheets("Estimate").Protect Password:="1234"
End Sub
[code/]
Many thanks for any help
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
That code works for me, I'm getting C:GW and IA:IN hidden.

After recording the macro, one thing you can do to speed it up a little is to take out the .Select and the Selection. commands that follow it and concatenate the lines. In VBA, you don't have to select the columns to do anything with them, you can just tell them what to do. You also shouldn't need the last unhide after you did the unhide on the larger range in the first row.

Code:
[COLOR=#333333]Sub View_Work()[/COLOR]
[COLOR=#333333]     Sheets("Estimate").Unprotect Password:="1234"[/COLOR]
[COLOR=#333333]     Columns("C:IN")[/COLOR][COLOR=#333333].EntireColumn.Hidden = False[/COLOR]
[COLOR=#333333]     Columns("C:GW").[/COLOR][COLOR=#333333]EntireColumn.Hidden = True[/COLOR]
[COLOR=#333333]     Columns("IA:IN").[/COLOR][COLOR=#333333]EntireColumn.Hidden = True[/COLOR]
[COLOR=#333333]     Range("A1").Select[/COLOR]
[COLOR=#333333]     Sheets("Estimate").Protect Password:="1234"[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
I am afraid it also hides C:GW for me, do you have any other code in the workbook, especially Event code?

You also don't need to use EntireColumn as you are already referring to the entire column by using Columns.
So assuming that the hidden columns are on Sheets("Estimate") then the code would be...

Code:
Sub View_Work()
    With Sheets("Estimate")
        .Unprotect Password:="1234"
        .Columns("C:IN").Hidden = False
        .Columns("C:GW").Hidden = True
        .Columns("IA:IN").Hidden = True
         Application.Goto .Range("A1")
        .Protect Password:="1234"
    End With
End Sub

By the way when using code tags you need an / in the second Code tag i.e. [/code] or it is easier just to select the code and then click the # icon.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,392
Messages
6,119,255
Members
448,879
Latest member
oksanana

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