macro to hide # of columns based on value

G

Graeme

I am trying to write a macro which will hide certain columns based upon the
value of a cell. For example, if cell value = 1, hide columns A:Q, if value
= 2, hide columns B:Q, if value = 3, hide C:Q and so on. Is there a quick
way of doing this. Thank you.
 
G

Gary Keramidas

not much info to work with, i used cell T1 on sheet1 as the value for the
first column

Sub test()
Dim colnum As Long
Dim ws As Worksheet
Set ws = Worksheets("sheet1")
colnum = ws.Range("t1").Value
With ws
.Columns(colnum).Resize(, 18 - colnum).EntireColumn.Hidden = True
End With
End Sub
 
G

Graeme

Thank you.

Gary Keramidas said:
not much info to work with, i used cell T1 on sheet1 as the value for the
first column

Sub test()
Dim colnum As Long
Dim ws As Worksheet
Set ws = Worksheets("sheet1")
colnum = ws.Range("t1").Value
With ws
.Columns(colnum).Resize(, 18 - colnum).EntireColumn.Hidden = True
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top