Combine 2 Macros

jpeters7635

New Member
Joined
Jul 28, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I have 2 macros - (Show_Hide) which hides and unhides the section by clicking a button, and another macro (BudgtComm) that only shows 2 non-contiguous rows within the section and only hides.

I'd like to combine these into one macro so that when I hit the button with all rows showing, the budgtcomm macro becomes the view, then click again and it hides the entire section, and then click again and it unhides to the budgtcomm view, and then click again and it unhides the whole section. I appreciate any help!

This is the one that shows and hides the whole section with one button:
_______________________________________________________________

Sub show_hide()
Rows("139:146").Select
If Selection.EntireRow.Hidden = True Then
Selection.EntireRow.Hidden = False
Else
Selection.EntireRow.Hidden = True
End If
End Sub

__________________________________________________________________

This one only shows only rows 139, 143, and 145 - 146
__________________________________________________________________

Sub BudgtComm()
Range("140:142,144:144").EntireRow.Hidden = True
End Sub

___________________________________________________________________

Is there a way to step the budgtcomm section so that the budgtcomm shows between the show/hide?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I'm not really sure what you are trying to do, but if I understand correctly, I think using what you have provided, would look something like:

VBA Code:
Sub show_hide()
'
    Rows("139:146").Select
'
    If Selection.EntireRow.Hidden = False Then              ' If all rows are showing then ...
        Range("140:142,144:144").EntireRow.Hidden = True    '   Show just the budgtcomm rows, 139, 143, and 145 - 146
        budgtcommFlag = "True"
'
    ElseIf budgtcommFlag = "True" Then                      ' If we have gone from all rows showing to just showing budgtcomm then ..
        Selection.EntireRow.Hidden = True                   '   Hide all the rows
        budgtcommFlag = "False"
'
    ElseIf Selection.EntireRow.Hidden = True Then           ' If all rows are hidden then ...
        Range("139,143,145:146").EntireRow.Hidden = False   '   Show just the budgtcomm rows, 139, 143, and 145 - 146
'
    Else
        Selection.EntireRow.Hidden = False                  ' UnHide all the rows
    End If
End Sub
 
Upvote 0
Overall I'm trying to turn 2 buttons into one, and your code only got me to the budgetcomm view and would not unhide when clicking same button again (which the unhide/hide button does).
What I'm trying to do is to step the views of the rows within the section and I'll try to make it clearer:
First View has all rows hidden.

Step 1: Click the Unhide / Hide button so rows in the section unhide to show historical information. Currently, the unhide / hide button is working fine to hide and unhide the seciton but I have to have a second button to collapse to just the budget + comment view).
1629682394025.png

1629682420235.png

I want to be able to click the Unhide / Hide button again and it goes to the budget + comment view (This is currently the second macro and requires the second button.
1629682659947.png

Click again and it unhide back to historical view.
Click again and it collapses the entire section back to view one.

Hope this makes more sense.
 
Upvote 0
Overall I'm trying to turn 2 buttons into one, and your code only got me to the budgetcomm view and would not unhide when clicking same button again (which the unhide/hide button does).
Your original post said that you want to go from all rows shown to just showing the budgetcomm view. You said the code I offered did that. Your original post said step two was to hide entire section. So why are you now saying unhide is the second step? Am I misunderstanding your original post?
 
Upvote 0
Ok going off of these assumptions:

Starting From All Hidden:
Click 1 will show all rows
Click 2 will show just budgetcomm
click 3 will show all rows
click 4 will hide all rows

This is the code I came up with to do that:

VBA Code:
Private Sub CommandButton1_Click()
'
    Rows("139:146").Select
'
    If CommandButton1.caption = "Show/Hide 1" Then          ' If all rows are hidden then ...
        Selection.EntireRow.Hidden = False                  ' UnHide all the rows
        CommandButton1.caption = "Show/Hide 2"
'
    ElseIf CommandButton1.caption = "Show/Hide 2" Then      ' If we have gone from all hidden rows to showing all rows then ..
        Range("140:142, 144:144").EntireRow.Hidden = True   '   Hide all but the budgtcomm rows, 139, 143, and 145 - 146
        CommandButton1.caption = "Show/Hide 3"
'
    ElseIf CommandButton1.caption = "Show/Hide 3" Then      ' If only budgtcomm rows are showing then ...
        Selection.EntireRow.Hidden = False                  ' UnHide all the rows
        CommandButton1.caption = "Show/Hide 4"
'
    Else
        Selection.EntireRow.Hidden = True                   '   Hide all the rows
        CommandButton1.caption = "Show/Hide 1"
    End If
End Sub
 
Upvote 0
Here it is with more meaningful names:

VBA Code:
Private Sub CommandButton1_Click()
'
    Rows("139:146").Select
'
    If CommandButton1.caption = "UnHide All" Then               ' If all rows are hidden then ...
        Selection.EntireRow.Hidden = False                      '   UnHide all the rows
        CommandButton1.caption = "Show Just BudgetComm"
'
    ElseIf CommandButton1.caption = "Show Just BudgetComm" Then ' If we have gone from all hidden rows to showing all rows then ..
        Range("140:142, 144:144").EntireRow.Hidden = True       '   Hide all but the budgtcomm rows, 139, 143, and 145 - 146
        CommandButton1.caption = "UnHide All Again"
'
    ElseIf CommandButton1.caption = "UnHide All Again" Then     ' If only budgtcomm rows are showing then ...
        Selection.EntireRow.Hidden = False                      '   UnHide all the rows
        CommandButton1.caption = "Hide All"
'
    Else
        Selection.EntireRow.Hidden = True                       ' Hide all the rows
        CommandButton1.caption = "UnHide All"
    End If
End Sub

In case it needs to be said, this is code for the 'CommandButton1'
Adjust accordingly if need be. ;)
 
Upvote 0
Here it is with more meaningful names:

VBA Code:
Private Sub CommandButton1_Click()
'
    Rows("139:146").Select
'
    If CommandButton1.caption = "UnHide All" Then               ' If all rows are hidden then ...
        Selection.EntireRow.Hidden = False                      '   UnHide all the rows
        CommandButton1.caption = "Show Just BudgetComm"
'
    ElseIf CommandButton1.caption = "Show Just BudgetComm" Then ' If we have gone from all hidden rows to showing all rows then ..
        Range("140:142, 144:144").EntireRow.Hidden = True       '   Hide all but the budgtcomm rows, 139, 143, and 145 - 146
        CommandButton1.caption = "UnHide All Again"
'
    ElseIf CommandButton1.caption = "UnHide All Again" Then     ' If only budgtcomm rows are showing then ...
        Selection.EntireRow.Hidden = False                      '   UnHide all the rows
        CommandButton1.caption = "Hide All"
'
    Else
        Selection.EntireRow.Hidden = True                       ' Hide all the rows
        CommandButton1.caption = "UnHide All"
    End If
End Sub

In case it needs to be said, this is code for the 'CommandButton1'
Adjust accordingly if need be. ;)
I'll check it out. Just concerned because the buttons in my workbook are just form control buttons, not active controls. Do you think it's easier to use active form controls?
 
Upvote 0
So you are saying you have 'buttons'. Is the button that you are using 'button 1' or a different button?
 
Upvote 0
Hi,

Try following & see if does what you want

Not sure fully understood but have only coded the sequence on a Tri-state

1 – Budget + Comment

2 – Comment

3 – Hide All

VBA Code:
Sub show_hide()
 Static State As Integer
 
  State = IIf(State = xlVisible, xlHidden, IIf(State = xlHidden, xlAll, xlVisible))
    Rows("139:146").EntireRow.Hidden = State = xlHidden
    Range("140:142,144:144").EntireRow.Hidden = State = xlHidden Or Not State = xlAll
   
    ActiveSheet.Buttons(Application.Caller).Caption = IIf(State = xlHidden, "Budget + Comment", _
                                                        IIf(State = xlVisible, "Hide All", "Comment"))
 
End Sub

Code should work with your existing Forms Control button

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

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