Code to hide columns where row 3 is blank

WStockel

New Member
Joined
May 30, 2020
Messages
30
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
In a range from B:BU I'm trying to find the first blank in row 3 and hide that column to column BU. Below is my code. I used the same code on another sheet and it worked fine.

Private Sub CBExit_Click()
Dim col As Integer

For col = 2 To 72
If Worksheets("Results").Cells(3, col).Value = "" Then
Columns(col).Hidden = True
End If
Next

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,
see if this update to your code does what you want

VBA Code:
Private Sub CBExit_Click()
    Dim col As Long
    For col = 72 To 2 Step -1
     With Worksheets("Results").Cells(3, col)
            .EntireColumn.Hidden = CBool(Len(.Value) = 0)
     End With
    Next
End Sub

Dave
 
Upvote 0
Hi,
see if this update to your code does what you want

VBA Code:
Private Sub CBExit_Click()
    Dim col As Long
    For col = 72 To 2 Step -1
     With Worksheets("Results").Cells(3, col)
            .EntireColumn.Hidden = CBool(Len(.Value) = 0)
     End With
    Next
End Sub

Dave
VBA Code:
Thanks Dave. That worked. Never heard of CBool before. Guess that's what you get when you are self taught.
 
Upvote 0
VBA Code:
Thanks Dave. That worked. Never heard of CBool before. Guess that's what you get when you are self taught.

Its a Function that coerces the expression to the required data type (Boolean True / False)
Further info can be found in VBA helpfile.

Glad suggestion helped

Dave
 
Upvote 0
I'm trying to find the first blank in row 3 and hide that column to column BU
That description implies that as soon as you find the first blank cell (from B3 looking at your code) across then you hide that column and all the rest to column BU. If that is the case and those cells are truly blank (that is do not contain formulas returning "") then you could hide them all in one go without looping.

VBA Code:
Private Sub CBExit_Click()
  With Sheets("Results")
    On Error Resume Next
    .Range(.Range("B3:BU3").SpecialCells(xlBlanks).Cells(1), .Range("BU3")).EntireColumn.Hidden = True
    On Error GoTo 0
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,814
Messages
6,121,711
Members
449,049
Latest member
THMarana

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