Macro to hide/unhide columns

JEB85

Board Regular
Joined
Aug 13, 2010
Messages
238
Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
All I’m wanting to do is hide columns X:Y if d7 = ""<o:p></o:p>
If it’s not = "" I would like the columns to be visible<o:p></o:p>
<o:p></o:p>
Here’s the code I have created;<o:p></o:p>
<o:p></o:p>
Sheet6.Select<o:p></o:p>
If Range("D7") = "" Then<o:p></o:p>
Columns("X:Y").Select<o:p></o:p>
Selection.EntireColumn.Hidden = True<o:p></o:p>
If Range("D7") <> "" Then<o:p></o:p>
Columns("X:Y").Select<o:p></o:p>
Selection.EntireColumn.Hidden = False<o:p></o:p>
End If<o:p></o:p>
End If<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
<o:p></o:p>
It hides the columns ok but doesn’t unhide them when necessary.<o:p></o:p>
<o:p></o:p>
Can anyone help?<o:p></o:p>
<o:p></o:p>
Thanks<o:p></o:p>
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try

Code:
With Sheet6
    .Columns("X:Y").Hidden = .Range("D7").Value = ""
End With
 
Upvote 0
Thanks, works great.

Is it possible to get it to run automatically without the need to press 'run'?
 
Upvote 0
D7 houses: sum(e7:f22)

If the sum is 0 then I'd like column x:y to be hidden

I the sum is >0 then I'd like columns x:y to be unhidden

The reason I put "" before was because I was unsure on what I was going to house in D7 but I think the best wasy to go is using the sum (as opposed to showing a blank if the sum is = 0)

Thanks
 
Upvote 0
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Calculate()
Columns("X:Y").Hidden = Range("D7").Value = 0
End Sub
 
Upvote 0
Hey Vog,

I've tried protecting the worksheet with a password and as a consequence have oboviously encountered problems with getting the code to work.

What do I need to do to unprotect the sheet, get the code to run, and protect the sheet again

I tried....

With ActiveSheet<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
.Unprotect Password:="xxx"<o:p></o:p>
End With

before your code and ...

With ActiveSheet<o:p></o:p>
.Unprotect Password:="xxx"<o:p></o:p>
End With

after your code without any luck.

Thanks
 
Upvote 0
Perhaps like this

Code:
Private Sub Worksheet_Calculate()
Me.Unprotect Password:="xxx"
Columns("X:Y").Hidden = Range("D7").Value = 0
Me.Protect Password:="xxx"
End Sub
 
Upvote 0
Works a charm Vog, thanks.

The only slight downside is that the screen blinks everytime i enter a new value. Is there anyway to get around this happening?
 
Upvote 0

Forum statistics

Threads
1,224,617
Messages
6,179,914
Members
452,949
Latest member
beartooth91

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