Macro to hide empty columns

Jenaustin29

New Member
Joined
Oct 12, 2023
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to add to my macro. Can you help me hid columns that c3through S3 = "-"
It's changes for customers so I cant sat hide N-S. The code I was give is not working.

'Hide empty columns from end
Dim i As Long, rng As Range
Set rng = Range("C3:S3")
For i = rng.Cells.Count To 1 Step -1
If rng.Item(i).Value = "-" Then
Columns(rng.Item(i).Column).Hidden = True
Else
Exit For
End If
Next

macro .jpg
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I'm trying to add to my macro. Can you help me hid columns that c3through S3 = "-"
It's changes for customers so I cant sat hide N-S. The code I was give is not working.
*** just the ones on the end are to be hidden if L3 was a - I don't want that hidden ****

'Hide empty columns from end
Dim i As Long, rng As Range
Set rng = Range("C3:S3")
For i = rng.Cells.Count To 1 Step -1
If rng.Item(i).Value = "-" Then
Columns(rng.Item(i).Column).Hidden = True
Else
Exit For
End If
Next

View attachment 100240
*** just the ones on the end are to be hidden if L3 was a - I don't want that hidden ****
 
Upvote 0
Maybe something like this.
VBA Code:
    'Hide empty columns in defined range
    Dim i As Long, rng As Range, rngHide As Range, R As Range
    
    Set rng = Range("C3:S3")

    For Each R In rng
        If R.Value = "-" Then
            If rngHide Is Nothing Then
                Set rngHide = R
            Else
                Set rngHide = Application.Union(rngHide, R)
            End If
        End If
    Next R
    
    If Not rngHide Is Nothing Then
        rngHide.EntireColumn.Hidden = True
    End If
 
Upvote 0

Forum statistics

Threads
1,215,741
Messages
6,126,591
Members
449,320
Latest member
Antonino90

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