Auto hide column based on multiple cell values

slam

Well-known Member
Joined
Sep 16, 2002
Messages
831
Office Version
  1. 365
  2. 2019
Hi all,

I am looking for some VBA magic to achieve a solution to my problem. I've tried a few similar examples I've found online, but without success.

I am looking to auto hide a column (C through AH) if every row in that respective column from Row 3 to Row 42 has a - The hyphen is produced by a formula and is not typed directly in the cells. The hyphen can change to another value, via the formula, and I would then want the column to auto unhide upon worksheet activate.

Not sure if it makes a difference, but the worksheet is protected.

Thanks very much.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:

Code:
Private Sub Worksheet_Calculate()

  Dim i As Integer
  Dim r As Range
  Dim iCounter As Integer
  
  For i = 3 To 34
    
    iCounter = 0
    For Each r In Range(Me.Cells(3, i), Me.Cells(42, i))
      If InStr(r.Value, "-") > 0 Then iCounter = iCounter + 1
    Next r
    
    If iCounter = 40 Then
      Me.Columns(i).Hidden = True
    Else
      Me.Columns(i).Hidden = False
    End If
    
  Next i

End Sub
 
Upvote 0
Thank you! This worked perfectly. However, I forgot I had some existing code which caused some of these same columns to auto fit. I currently have that occurring under worksheet_activate where the password is required to allow the column resizing. Is it possible to include this in your code where it will only auto fit for columns that it isn't hiding, i.e. where there is something other than a dash in every cell in that column?

Thanks
 
Upvote 0
Try this:

Code:
Private Sub Worksheet_Calculate()

  Dim i As Integer
  Dim r As Range
  Dim iCounter As Integer
  
  For i = 3 To 34
    
    iCounter = 0
    For Each r In Range(Me.Cells(3, i), Me.Cells(42, i))
      If InStr(r.Value, "-") > 0 Then iCounter = iCounter + 1
    Next r
    
    If iCounter = 40 Then
      Me.Columns(i).Hidden = True
    Else
      Me.Columns(i).Hidden = False
    End If
    
  Next i

End Sub

Hi gpeacock,

I've encountered a problem with this, and it's my fault for not explaining something more clearly.

It should only hide the column if the ONLY value in it is "-". However, it's hiding the column any time there's a - in it. For instance "Mary-Jane" would cause the column to be hidden and I don't want that.

Is there an easy change that can be made?

Kind regards
 
Upvote 0

Forum statistics

Threads
1,203,350
Messages
6,054,900
Members
444,760
Latest member
TeckTeck

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