Select Variable Number of Columns

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to select a variable number of columns. I know I want to start in
Column C (3) and I know how many columns out I wish to go but I can't seem to
figure out the right syntax for selecting these columns. After selection I
wish to hide them.
 
Hi, try this. If you want to unhide all columns just unrem
Columns.Hidden = False and run the macro.

Sub Hide_cols()
Application.ScreenUpdating = False
Dim col As Integer, i As Integer
col = Val(InputBox("Give the last column number"))
For i = 3 To col
Columns(i).Hidden = True
Next i
Application.ScreenUpdating = True
'Columns.Hidden = False
End Sub

regards
 
Just wondering, would the hiding routine go faster if I were to select a
range of columns to hide, rather than just one column at a time? That is
what I was originally trying to do and couldn't get it right. If you don't
think so, use what you gave me.

Another issue:
Do you know if I have cells within the column protected, I can't seem to
hide the column. Do you know how to allow protected cells to be hidden and
still be protected?
 
Just wondering, would the hiding routine go faster if I were to select a
range of columns to hide, rather than just one column at a time? That is
what I was originally trying to do and couldn't get it right. If you don't
think so, use what you gave me.

Another issue:
Do you know if I have cells within the column protected, I can't seem to
hide the column. Do you know how to allow protected cells to be hidden and
still be protected?

You are right, bad habit of using iteration ;). I think this will wokr
much faster

Sub Hide_cols()
Dim col As String, i As Integer
col = CStr(InputBox("Give the last column letter"))
Range("c:" & col).Columns.Hidden = True
'Columns.Hidden = False
End Sub

as for your other question try to choose tools>protection>protect
worksheet> and set alowance to format columns, that should do.
 
I want to select a variable number of columns.

Hi. One way:

Sub Demo()
Dim n
n = 5

Columns(3).Resize(, n).Hidden = True
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

Back
Top