Hide columns with criteria VBA

ronald54457874

New Member
Joined
Sep 26, 2021
Messages
17
Office Version
  1. 2013
Hello, I need help to hide the column "O", but it can only be hidden IF it has the value "XYZ" in one of the cells of the column "K" AND if the cells of the column "M" has nothing or i.e. be different from 0.

I tried this but it didn't work;



VBA Code:
Dim C As Range
Dim ABC As Range
Dim DEF As Range
Set C = Range("O:O")
Set DEF = Range("M:M").Select
 For Each ABC In Range("L:L").Cells
 If ABC.Value = "XYZ" And DEF.Value <> 0 Then
     C.EntireColumn.Hidden = False
 ElseIf ABC.Value <> "XYZ" Then     
     C.EntireColumn.Hidden = True
 End If
 Next

My Logic
VBA Code:
If Columns("K").Value = "XYZ" And Columns("M").Value <> 0 Then
        Columns("o:o").Select
        Selection.EntireColumn.Hidden = False
End If

Apparently it's not difficult but I don't have the skills yet to make this code. Thanks to anyone who can help.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
If you want to hide the whole column if just 1 row meets your criteria then you don't want to be unhiding the column when it doesn't meet the criteria.
If you do that then the column will only be hidden if the very last row meets your criteria.
In fact you can exit the for as soon as you hit the first match.

Note: your description refers to column K but your code refers to column L. In the below I have used Column L.
Replace the 3 references to Column L in the line Set rng = Range("L1:L" & Cells(Rows.Count, "L").End(xlUp).Row), if you did mean column K


VBA Code:
Sub HideColumn()
    
    Dim C As Range
    Dim ABC As Range
    Dim rng As Range
    
    Set C = Range("O:O")
    Set rng = Range("L1:L" & Cells(Rows.Count, "L").End(xlUp).Row)
    
     For Each ABC In rng.Cells
            If ABC.Value = "XYZ" And ABC.Offset(, 1).Value <> 0 Then
                C.EntireColumn.Hidden = True
                Exit For
            End If
        Next
 
 End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

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