VBA Hide Column based on cell values

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
169
Office Version
  1. 365
Hi ,

I need some code to hide a column if all the cells have been reported as n/a

My data always starts on row 3 , the first two rows are headers and the number of rows would need to be dynamic as it could change.,

Example , from the below table the macro would hide column C

ABC
Test 1Test 2Test 3
TestTestn/a
TestTestn/a
Testn/an/a
Testn/an/a
Testn/an/a

I have been looking online and there are a lot of examples of hiding rows based on cell value but I cant find anything to hide columns based on the cell value of all the cells in that column.

Any help is greatly appreciated

Thanks.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
VBA Code:
Option Explicit

Sub ATest()
    Dim lr As Long, i As Long, lc As Long, x As Integer, j As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 2 To lc
        x = 0
        For j = 3 To lr
            If Cells(j, i) = "n/a" Then
                x = x + 1
            End If
        Next j
        If x = lr - 2 Then
            Columns(i).Hidden = True
        End If
    Next i
End Sub
 
Upvote 0
Solution
Hello, this one is similar...
VBA Code:
Sub HideErrorColumns()
 
    Dim varWS As Worksheet
    Dim varNLoops As Integer, _
        varNRows As Long, varNColumns As Integer
    Dim varNErrors As Integer
 
    Application.ScreenUpdating = False
    Set varWS = Worksheets("YourSheetName")
    varNRows = varWS.UsedRange.Rows.Count
    varNColumns = varWS.UsedRange.Columns.Count
 
    For varNLoops = 1 To varNColumns
        varNErrors = WorksheetFunction.CountIf(varWS.Range(Cells(3, varNLoops), _
            Cells(varNRows, varNLoops)), "#N/A")
        If varNErrors > 0 Then varWS.Columns(varNLoops).Hidden = True
    Next
    Application.ScreenUpdating = True
 
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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