hide other columns macro

orsm6

Active Member
Joined
Oct 3, 2012
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Hi all,
i've found this macro that will hide columns in a range based on a cell value. But how would the code be written to hide every OTHER column that doesn't contain that value in the row?

so as example, if i want to hide every other column EXCEPT the ones with X

Code:
Sub Hide_Columns_Containing_Value()
'Description: This macro will loop through a row and
'hide the column if the cell in row 1 of the column
'has the value of X.
'Author: Jon Acampora, Excel Campus
'Source:
   
Dim c As Range
 
    For Each c In Range("A1:G1").Cells
        If c.Value = "X" Then
            c.EntireColumn.Hidden = True
 
            'You can change the property above to False
            'to unhide the columns.
        End If
    Next c
 
End Sub

thanks
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
Sub Hide_Columns_NOT_Containing_Value()
Dim c As Range
Range("A1:G1").EntireColumn.Hidden = True
For Each c In Range("A1:G1").Cells
    If c.Value = "X" Then
        c.EntireColumn.Hidden = False
    End If
Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,915
Members
449,478
Latest member
Davenil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top