Forum

hide columns for th...
 
Notifications
Clear all

hide columns for the selected worksheets

6 Posts
3 Users
0 Reactions
125 Views
(@carzur)
Posts: 9
Active Member
Topic starter
 

Hi,

I am trying to hide columns for the worksheets I selected by using this code but it only hides on the Active worksheet

Range("A:C").Select
Selection.EntireColumn.Hidden = True

I used the same coding for changing the width of the columns and it worked
Range("D:D").Select
Selection.ColumnWidth = 18

 

So I am not sure what I am doing wrong

Any help would be great

 

thanks

 
Posted : 22/03/2021 4:56 pm
(@questvba)
Posts: 125
Estimable Member
 

Hi Chantal,

The easiest way is to use a single line of code and mention the sheet concerned :

Sheets("Sheet1").Range("A:C").EntireColumn.Hidden = True

BR,

Lionel

 
Posted : 23/03/2021 6:58 am
(@carzur)
Posts: 9
Active Member
Topic starter
 

thanks but I won't know the sheets name until my macro creates them.

How can I find a code that hide the columns for the selected sheets?

 

Thanks

 
Posted : 28/03/2021 1:09 pm
(@questvba)
Posts: 125
Estimable Member
 

Hi Chantal,

I must admit that I'm wondering because you say that you won't know the name of your sheet since it will be created by the macro. It is then enough to memorise the name of this creation. Here's some code for the context: I create a sheet and ask the user for its name as long as it doesn't exist, then I hide the columns.

Sub TestSheetCreate()
Dim newSheetName As String
Dim checkSheetName As String
newSheetName = Application.InputBox("Input Sheet Name:", "Create Sheet", _
"sheet4", , , , , 2)
On Error Resume Next
checkSheetName = Worksheets(newSheetName).Name
If checkSheetName = "" Then
Worksheets.Add.Name = newSheetName
MsgBox "The sheet named ''" & newSheetName & _
"'' doesn't exist in this workbook but it has been created now.", _
vbInformation, "Create Sheet"

Else
MsgBox "The sheet named ''" & newSheetName & _
"'' exist in this workbook.", vbInformation, "Create Sheet"
End If
Sheets(newSheetName).Range("A:C").EntireColumn.Hidden = True
End Sub

If this doesn't solve your problem, you need to tell us more about the existing code.

BR,

Lionel

 
Posted : 29/03/2021 11:50 pm
(@debaser)
Posts: 837
Member Moderator
 

If they are selected, you can simply loop:

 

For each ws in activewindow.selectedsheets

ws.Range("A:C").EntireColumn.Hidden = True

next ws

 
Posted : 30/03/2021 3:43 am
(@carzur)
Posts: 9
Active Member
Topic starter
 

thank you for all your replies

 
Posted : 05/04/2021 1:29 pm
Share: