Code to hide cell range is not working

rcirone

Active Member
Joined
Mar 12, 2009
Messages
483
Office Version
  1. 365
Platform
  1. Windows
I am try to get a code to hide cell range or Columns but I can not get it to work in Office 365. and trying to tide it to a button

VBA Code:
Sub DoIfNotEmpty()
    Dim ra As Range, re As Range


    With ThisWorkbook.Worksheets("Sheet1")
    Set ra = .Range("D4:F40")
        For Each re In ra
            If IsEmpty(re.Value) Or re.Value = vbNullString Then
                Columns("D:F").EntireColumn.Hidden = True
            End If
        Next re
    End With
End Sub

Private Sub CommandButton1_Click()

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Please try this

VBA Code:
Sub DoIfNotEmpty()
    Dim ra As Range, re As Range


    With ThisWorkbook.Worksheets("Sheet1")
    Set ra = .Range("D4:F40")
    For Each re In ra
      If IsEmpty(re.Value) Or re.Value = vbNullString Then
        ra.EntireColumn.Hidden = True
        Exit For
      End If
    Next re
    End With
End Sub

Private Sub CommandButton1_Click()

  DoIfNotEmpty

End Sub
 
Upvote 0
Please try this

VBA Code:
Sub DoIfNotEmpty()
    Dim ra As Range, re As Range


    With ThisWorkbook.Worksheets("Sheet1")
    Set ra = .Range("D4:F40")
    For Each re In ra
      If IsEmpty(re.Value) Or re.Value = vbNullString Then
        ra.EntireColumn.Hidden = True
        Exit For
      End If
    Next re
    End With
End Sub

Private Sub CommandButton1_Click()

  DoIfNotEmpty

End Sub
It is still not working for me How do I get this to work on a button
I may be doing that wrong just checking
 
Upvote 0
Creating a ActiveX Button and Adding Code

1706813648991.png



1706813574344.png

VBA Code:
Private Sub HideColumns_Btn_Click()
DoIfNotEmpty
End Sub
 
Upvote 0
Creating a ActiveX Button and Adding Code

View attachment 106157


View attachment 106156
VBA Code:
Private Sub HideColumns_Btn_Click()
DoIfNotEmpty
End Sub
Thank you I got it to hide the columns but when I click it again they are not coming back up they are staying hidden this is what I am using

Sub DoIfNotEmpty()
Dim ra As Range, re As Range


With ThisWorkbook.Worksheets("M1661104s")
Set ra = .Range("AD:AH")
For Each re In ra
If IsEmpty(re.Value) Or re.Value = vbNullString Then
ra.EntireColumn.Hidden = True
Exit For
End If
Next re
End With
End Sub

Private Sub CommandButton1_Click()

DoIfNotEmpty

End Sub
 
Upvote 0
Does this do the trick?

VBA Code:
Sub DoIfNotEmpty()
  Dim ra As Range, re As Range
  Dim Found As Boolean
  
  
  With ThisWorkbook.Worksheets("M1661104s")
    Set ra = .Range("AD:AH")
    For Each re In ra
      If IsEmpty(re.Value) Or re.Value = vbNullString Then
        ra.EntireColumn.Hidden = True
        Found = True
        Exit For
      End If
    Next re
    If Found = False Then ra.EntireColumn.Hidden = False
  End With
End Sub
 
Upvote 0
Does this do the trick?

VBA Code:
Sub DoIfNotEmpty()
  Dim ra As Range, re As Range
  Dim Found As Boolean
 
 
  With ThisWorkbook.Worksheets("M1661104s")
    Set ra = .Range("AD:AH")
    For Each re In ra
      If IsEmpty(re.Value) Or re.Value = vbNullString Then
        ra.EntireColumn.Hidden = True
        Found = True
        Exit For
      End If
    Next re
    If Found = False Then ra.EntireColumn.Hidden = False
  End With
End Sub
I get this error (Subscript out of range)
 
Upvote 0
Well, since I don't see any line in the code I provided like "Columns AC:AG", I don't know how to help.
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

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