VBA code for hide/unhide separate rows

mare23

New Member
Joined
Oct 28, 2020
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hi everyone!

Is there any VBA code for hide/unhide separate rows. for example
When I click - button once hide all rows from 23-31 and when I click the - button a second time hide rows 14-22
When I click + button once unhide all rows from 14 -22 and when I click the + button a second time hide rows 23-31


Thx
Mare
 

Attachments

  • example.jpg
    example.jpg
    39.1 KB · Views: 17

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi Mare23, welcome to Mr.Excel!

The code below does as required. Put it in the module of the worksheet on which the code is supposed to act on.

VBA Code:
Option Explicit

Private bToggleMINUS as Boolean
Private bTogglePLUS  as Boolean

Public Sub Btn_MINUS()
    If Not bToggleMINUS Then
        Me.Rows("23:31").Hidden = True
    Else
        Me.Rows("14:22").Hidden = True
    End If
    bToggleMINUS = Not bToggleMINUS
End Sub

Public Sub Btn_PLUS()
    If Not bTogglePLUS Then
        Me.Rows("14:22").Hidden = False
    Else
        Me.Rows("23:31").Hidden = False
    End If
    bTogglePLUS = Not bTogglePLUS
End Sub
 
Upvote 0
Hi GWteB, thank you for your help. I'm new here and I don't know VBA so well. I tried to insert this code but it doesn't work. How can I send an excel example?

When I clicked on minus icon the model1 opened, when I enter this code it reports errors :(
 
Upvote 0
Not sure what you did but I could have explained it better. Here we go ...

1. Right click on the sheet tab upon which your buttons are and click on View Code ...

ScreenShot157.png


2. The Visual Basic Editor (VBE) opens with a pane of a module in which the code is to be put.
The name of the current module apears on the title bar of the VBE, eg [Sheet1 (Code)].
To switch between different code modules press Ctrl R to open Project Explorer's pane.
For this moment stay in Sheet1's module and paste the code I posted ...

ScreenShot158.png


3. Switch to Excel to your worksheet and right click your button or shape and click Assign Macro ...

ScreenShot159.png


4. Choose the desired macro for that particular button and confirm pressing OK. Repeat the latter regarding the other button.

ScreenShot160.png


Hope I've put you on the right track. If not, let me know.
 
Upvote 0
ars on the title bar of the VBE,
Great, we moved one step. Next problem is when I click MINUS button it hides only "23:31" and second time when I click MINUS button nothing happenes or when I click PLUS button also nothing happens.
 
Upvote 0
It works for me so I've no idea why it doesn't work for you...
Did you perform the steps exactly as described? If so, it should work.
 
Upvote 0
A problem with using public variables for something like this, is they get destroyed when you close the workbook.
Another option
VBA Code:
Sub HideRws()
   If Rows("23:31").Hidden Then
      Rows("14:22").Hidden = True
   Else
      Rows("23:31").Hidden = True
   End If
End Sub
Sub UnHideRws()
   If Rows("14:22").Hidden Then
      Rows("14:22").Hidden = False
   Else
      Rows("23:31").Hidden = False
   End If
End Sub
 
Upvote 0
Yeeeeaaah!!! That's it! Great... I'm so happy... thank you, thank you, thank you :)
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
A problem with using public variables for something like this, is they get destroyed when you close the workbook.
Have never realized that, thanks for pointing that out !! (y)
 
Upvote 0

Forum statistics

Threads
1,215,346
Messages
6,124,417
Members
449,157
Latest member
mytux

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