Toggle Button to Remove Empty Spaces

Daylon

New Member
Joined
Nov 15, 2023
Messages
13
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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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