HELP! - Hiding colums Macro

mobilesuit818

New Member
Joined
Sep 13, 2006
Messages
48
Hello,
I need to hide several columns (those columns are F, Q, R S, T, U, W, X, and Y) when im finished working a worksheet. There are several worksheets in my workbook.

Can someone help with a macro for this, please?
 
Based on this formula,
Sub foo()
ActiveSheet.Range("F:F, Q:Y").EntireColumn.Hidden = True
End Sub

I would like to see how I can revise this macro to add password protect. The end result is that in order to run this macro, I would like to password protect it, basically sending a spreadsheet out to my company, but need to protect a couple of columns, so no one can see it, at the same time, I need to be the only one who can hide/unhide those columns once i enter the correct password in.

Can anyone help me with this one?
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Just Unprotect/Reprotect it:

Code:
Sub foo() 
   With ActiveSheet
      .Unprotect "PasswordHere"
        .Range("F:F, Q:Y").EntireColumn.Hidden = True 
      .Protect "PasswordHere"
   End With
End Sub

Smitty
 
Upvote 0
Smiity,
Thank.

Also need to see if you can change the macro, to run right after the user saves as. Is this possible.?

Also, I have the first macro that you gave me entered with this one, and it wont allow me to run both of them,

Can you please advise.?
 
Upvote 0
Also need to see if you can change the macro, to run right after the user saves as. Is this possible.?

Check out the BeforeClose/Save events in the VB helpfile. You can test for the old WB name in your code for validation.

Also, I have the first macro that you gave me entered with this one, and it wont allow me to run both of them

If you got an "Ambiguous name..." error then that's because both subs are named the same & VBA doesn't like that (ambiguity), so you've got you change one or the other.

Smitty
 
Upvote 0
Check out the BeforeClose/Save events in the VB helpfile. You can test for the old WB name in your code for validation.
I did a search on this, and found out this code, but I did know where to enter the formula in.

The formula that I saw was:
Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)

Can you post a structure format for future reference and how where do I enter the formula in? Do I put in under the SUB?


If you got an "Ambiguous name..." error then that's because both subs are named the same & VBA doesn't like that (ambiguity), so you've got you change one or the other.
I actually a message that read "400", and I am assuming it is the same thing that you mention. Im going to go ahead and just use a set not both macros.
 
Upvote 0
Do you want to force the user to save or let them choose when?

It might be a better idea to force the save and incorporate your code there.

Smitty
 
Upvote 0
Smitty,
I dont know if it will be difficult, but for my type of application, the user will need to chose when they need to save, and right when they do that, it would hide the columns as defined, and password protect that so no one else can see that, except if you had a password.

Thanks again.
 
Upvote 0
OK,

You can try something like this:

<font face=Tahoma><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Workbook_BeforeSave(<SPAN style="color:#00007F">ByVal</SPAN> SaveAsUI <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN>, Cancel As <SPAN style="color:#00007F">Boolean</SPAN>)
    <SPAN style="color:#00007F">With</SPAN> Sheets("Sheet1")
      .Unprotect "PasswordHere"
        .Range("F:F, Q:Y").EntireColumn.Hidden = <SPAN style="color:#00007F">True</SPAN>
      .Protect "PasswordHere"
   <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Note I used a specific sheet name reference instead of ActiveSheet, so you'll need to adjust that to the actual sheet name that you're trying to use this on.

The code goes in the ThisWorkbook module.

Smitty
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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