Jul 26 2022 01:34 AM
Hello,
I found a macro that almost fits my needs, can someone help me out to complete?
I want to hide columns between range I:BH based on what is not in the cell in row 11
Sub HideCols() Dim cell As Range For Each cell In ActiveWorkbook.ActiveSheet.Rows("8").Cells If cell.Value = "X" Then cell.EntireColumn.Hidden = True End If Next cell End Sub
Where do I set my range I:BH?
And I suppose I just change If cell.Value = "X" Then to If cell.Value <> "X" Then
Thanks!
Jul 26 2022 02:25 AM
Jul 26 2022 03:38 AM
Jul 26 2022 03:43 AM
SolutionSub HideCols()
Dim cell As Range
For Each cell In Range("I11:BH11")
cell.EntireColumn.Hidden = (cell.Value <> "X")
Next cell
End Sub
Jul 26 2022 03:54 AM
Jul 26 2022 04:23 AM
Aug 01 2022 10:32 PM
Aug 02 2022 12:35 AM
Using Option Explicit in all your code modules is always a good idea - see The importance of 'Option Explicit'
If you want to define a variable that will be available in all modules in a workbook, you have to declare the variable at the top of a module (but below Option Explicit) as Public:
Public MyRange As Range
But you also have to assign a value to this variable. You might do this in the ThisWorkbook module, in the Workbook_Open event procedure. This will automatically be executed each time the workbook is opened:
Private Sub Workbook_Open()
Set MyRange = Worksheets("Sheet1").Range("I11:BH11")
End Sub
Change Sheet1 to the name of the relevant worksheet.