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
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
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
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
If they are selected, you can simply loop:
For each ws in activewindow.selectedsheets
ws.Range("A:C").EntireColumn.Hidden = True
next ws
thank you for all your replies