Hide columns based on IF

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I'm having trouble figuring out where to start with this and could use your guidance.

First off, the total rows are ever changing and so I would need to use "last row" in my formula. The columns in question will always begin at O and end at AS.

My goal is to hide all columns that do not have an "N" in any particular cell within that specific column.

I had thought of a loop within a loop, beginning looping through the columns and then looping through the rows, but I'm not sure how to pull this off.

Thank you
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Please try the following on a copy of your workbook. Place the code in the sheet module of the sheet in question - right click the sheet tab name, select View Code, and put the code in the window that appears on the right of the screen.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("O:AS"), Target) Is Nothing Then
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Dim i As Long, r As Range
        For i = 15 To 45
            Set r = Columns(i).Find("N")
            If r Is Nothing Then Columns(i).Hidden = True
        Next i
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Please try the following on a copy of your workbook. Place the code in the sheet module of the sheet in question - right click the sheet tab name, select View Code, and put the code in the window that appears on the right of the screen.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("O:AS"), Target) Is Nothing Then
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Dim i As Long, r As Range
        For i = 15 To 45
            Set r = Columns(i).Find("N")
            If r Is Nothing Then Columns(i).Hidden = True
        Next i
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
I did as you said, right click on sheet tab name, pasted the code. When I try to run it I get a pop up looking for a macro to run.

One important detail I failed to include, the file I'm running the macro on is generated by a database, I would need the macro to be on another file where I would run it. As an example, I have a file simply called Misc.xlsb that houses all my macros. I open that when I need to run one of my macros.

On a side note, I'm unfamiliar with the following line, how does it work?

VBA Code:
If Not Intersect(Range("O:AS"), Target) Is Nothing Then
Thank you
 
Upvote 0
I came up with this rather rudimentary macro.

VBA Code:
Sub hide_zero()
    LR = Cells(Rows.Count, 1).End(xlUp).Row
    Range("O" & LR + 1) = "=COUNTIF(O4:O" & LR & ",""N"")"
    Range("O" & LR + 1).Copy Range("P" & LR + 1 & ":AS" & LR + 1)
    For x = 15 To 45
        If Range("A" & LR + 1).Offset(0, x) = 0 Then Range("A" & LR + 1).Offset(0, x).EntireColumn.Hidden = True
    Next x
End Sub
 
Upvote 0
When I try to run it
Worksheet change event code does not require any actions to run it - it runs automatically when a change occurs, in this case in columns O:AS. Being a worksheet level code, it won't appear on the macro list.
One important detail I failed to include
Yes, that does make a considerable difference. If your "rudimentary macro" in post #4 works for you, then I could suggest the following, which is run with the relevant sheet active:
VBA Code:
Option Explicit
Sub Hide_Some_Columns()
    Application.ScreenUpdating = False
    Dim i As Long, r As Range
    For i = 15 To 45
        Set r = Columns(i).Find("N")
        If r Is Nothing Then Columns(i).Hidden = True
    Next i
    Application.ScreenUpdating = False
End Sub

I'm unfamiliar with the following line, how does it work?
In simple terms, it says only run the macro if a change occurs in the range O:AS - in other words, if the cell (Target) where the change occurs intersects with range O:AS.
 
Upvote 0
Yes, that does make a considerable difference. If your "rudimentary macro" in post #4 works for you, then I could suggest the following, which is run with the relevant sheet active:
VBA Code:
Option Explicit
Sub Hide_Some_Columns()
    Application.ScreenUpdating = False
    Dim i As Long, r As Range
    For i = 15 To 45
        Set r = Columns(i).Find("N")
        If r Is Nothing Then Columns(i).Hidden = True
    Next i
    Application.ScreenUpdating = False
End Sub
I understand this, but I believe the Find is looking for an "N" anywhere in the cell, I need it to only look for cells with "N" and nothing else. As an example, "N/A" in a cell is recognized as an "N", is that right?

Thank you.
 
Upvote 0
Understood. Try this instead:

VBA Code:
Option Explicit
Sub Hide_Some_Columns()
    Application.ScreenUpdating = False
    Dim i As Long, r As Range
    For i = 15 To 45
        Set r = Columns(i).Find("N", LookAt:=xlWhole)
        If r Is Nothing Then Columns(i).Hidden = True
    Next i
    Application.ScreenUpdating = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,659
Members
449,114
Latest member
aides

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