Toggle Button to Remove Empty Spaces

Daylon

New Member
Joined
Nov 15, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hey everyone, I'm currently trying to create a toggle button that will hide all empty rows in the range of A5:A41 (including formulas). Once I get that sorted out I would like to name the toggle button "Remove empty Cells" and "Open Empty Cells"

Here is a picture of the spreadsheet I'm using and the code that I currently have, but is not working.

1700078909415.png



1700078937394.png
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi @Daylon. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Try this:
VBA Code:
Private Sub ToggleButton10_Click()
  Dim myrange As Range, c As Range
  
  Set myrange = Range("A5:A41")
  With ToggleButton10
    If .Caption = "Remove empty Cells" Then
      .Caption = "Open Empty Cells"
      For Each c In myrange
        c.EntireRow.Hidden = c.Value = ""
      Next
    Else
      .Caption = "Remove empty Cells"
      myrange.EntireRow.Hidden = False
    End If
  End With
End Sub


Note Code Tag:
In future please use code tags when posting code, instead of an image.​
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.​


----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Solution
Hi @Daylon. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Try this:
VBA Code:
Private Sub ToggleButton10_Click()
  Dim myrange As Range, c As Range
 
  Set myrange = Range("A5:A41")
  With ToggleButton10
    If .Caption = "Remove empty Cells" Then
      .Caption = "Open Empty Cells"
      For Each c In myrange
        c.EntireRow.Hidden = c.Value = ""
      Next
    Else
      .Caption = "Remove empty Cells"
      myrange.EntireRow.Hidden = False
    End If
  End With
End Sub


Note Code Tag:
In future please use code tags when posting code, instead of an image.​
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.​


----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Dante, Your code worked well. I appreciate your assistance. The only small thing that I'm trying to tweak is that the toggle button is light gray (depressed). I'm trying to make it so that the first state of the button is the darker gray color (un-depressed). Please let me know your thoughts.
 
Upvote 0
Update: I was able to figure out the color differences. Thanks for the help.
 
Upvote 0
The only small thing that I'm trying to tweak is that the toggle button is light gray (depressed). I'm trying to make it so that the first state of the button is the darker gray color (un-depressed). Please let me know your thoughts.


Try:

VBA Code:
Private Sub ToggleButton10_Click()
  Dim myrange As Range, c As Range
  
  Set myrange = Range("A5:A41")
  With ToggleButton10
    If .Caption = "Remove empty Cells" Then
      .Caption = "Open Empty Cells"
      .BackColor = &H8000000F   'light gray
      For Each c In myrange
        c.EntireRow.Hidden = c.Value = ""
      Next
    Else
      .Caption = "Remove empty Cells"
      .BackColor = &H808080     'darker gray
      myrange.EntireRow.Hidden = False
    End If
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,067
Members
449,090
Latest member
fragment

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