hide other columns macro

orsm6

Well-known Member
Joined
Oct 3, 2012
Messages
515
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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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