Combine Macros to One Command Button

shapeshiftingkiwi

New Member
Joined
Mar 31, 2021
Messages
33
Office Version
  1. 365
Platform
  1. Windows
I'm trying to combine two macros to one command button but am getting Compile Error: Expected: Expression as an error. Here's what I'm trying to run...

Private Sub RunAllMacros()
CammandButton1_Click
HideRows_Based_On_Values
End Sub

Private Sub CommandButton1_Click()
Sheets("Form").Range("D7").Value = ""
Sheets("Form").Range("D9:D10").Value = ""
Sheets("Form").Range("F14:F29").Value = ""
Sheets("Form").Range("G14:G29").Value = ""
Sheets("Form").Range("C32:C39").Value = ""
Sheets("Form").Range("D32:D39").Value = ""
Sheets("Form").Range("F32:F39").Value = ""
Sheets("Form").Range("D43:D44").Value = ""
Sheets("Form").Range("H43:H44").Value = ""
Sheets("Form").Range("K43:K44").Value = ""
Sheets("Form").Range("D48").Value = ""
Sheets("Form").Range("I48").Value = ""
Sheets("Form").Range("D5").Value = ""
Sheets("Form").Range("D56").Value = ""
Sheets("Form").Range("I56").Value = ""
Sheets("Form").Range("D51:D52").Value = ""
Sheets("Form").Range("H51:H52").Value = ""
Sheets("Form").Range("K51:K52").Value = ""
Sheets("Form").Range("H5").Value = ""
Sheets("Form").Range("J5").Value = ""

MsgBox "Form has been reset"

End Sub

Private Sub HideRows_Based_On_Values()

For Each cell In Range("D14:D33")
If cell.Value = "-" Then cell.EntireRow.Hidden = True
If cell.Value <> "-" Then cell.EntireRow.Hidden = False
End Sub

End Sub


If this code looks like absolute trash, please forgive me, this is all very new to me!

Thanks for any help!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I noticed 3 things right away.
  1. CammandButton1_Click is spelled wrong. The second letter is "a" when it should be "o"
  2. There is an extra "End Sub" at the end
  3. There is no "Next" to match the For
 
Upvote 0
I noticed 3 things right away.
  1. CammandButton1_Click is spelled wrong. The second letter is "a" when it should be "o"
  2. There is an extra "End Sub" at the end
  3. There is no "Next" to match the For
I had spotted the "cammand" typo, I've removed the extra end sub. What is point 3 referring to on the Next? I'm following this guide for hiding the rows and the macro works on it's own.

 
Upvote 0
A For statement requires a Next for the loop.

VBA Code:
For Each cell In Range("D14:D33")
   If cell.Value = "-" Then cell.EntireRow.Hidden = True
   If cell.Value <> "-" Then cell.EntireRow.Hidden = False
Next
 
Upvote 0
I missed that each IF also needs an End IF

VBA Code:
For Each cell In Range("D14:D33")
   If cell.Value = "-" Then cell.EntireRow.Hidden = True End If
   If cell.Value <> "-" Then cell.EntireRow.Hidden = False End If
Next
 
Upvote 0
Ohh jeez somehow I copied the wrong code. I had that in there as well. So just the duplicate end sub which I changed and still no luck...

Private Sub RunAllMacros()
CommandButton1_Click
HideRows_Based_On_Values
End Sub

Private Sub CommandButton1_Click()
Sheets("Form").Range("D7").Value = ""
Sheets("Form").Range("D9:D10").Value = ""
Sheets("Form").Range("F14:F29").Value = ""
Sheets("Form").Range("G14:G29").Value = ""
Sheets("Form").Range("C36:C51").Value = ""
Sheets("Form").Range("D36:D51").Value = ""
Sheets("Form").Range("F36:F51").Value = ""
Sheets("Form").Range("D43:D44").Value = ""
Sheets("Form").Range("H43:H44").Value = ""
Sheets("Form").Range("K43:K44").Value = ""
Sheets("Form").Range("D48").Value = ""
Sheets("Form").Range("I48").Value = ""
Sheets("Form").Range("D5").Value = ""
Sheets("Form").Range("D56").Value = ""
Sheets("Form").Range("I56").Value = ""
Sheets("Form").Range("D51:D52").Value = ""
Sheets("Form").Range("H51:H52").Value = ""
Sheets("Form").Range("K51:K52").Value = ""
Sheets("Form").Range("H5").Value = ""
Sheets("Form").Range("J5").Value = ""

MsgBox "Form has been reset"

End Sub

Private Sub HideRows_Based_On_Values()

For Each cell In Sheets("Form").Range("D14:D33")
If cell.Value = "-" Then cell.EntireRow.Hidden = True
Next cell
End Sub
 
Upvote 0
It worked perfectly for me. The only change I made was from Private Sub RunAllMacros() to Public Sub RunAllMacros() so that I could run it from the Macros button.

Before
Book3
ABCDE
2020
2121
2222
23-23
24-24
2525
2626
2727
2828
2929
3030
3131
Form


You can see lines 23 and 24 are visible. Run the macro and they are gone
Book3
ABCDE
2020
2121
2222
2525
2626
2727
2828
2929
3030
3131
Form
 
Upvote 0
Solution

Forum statistics

Threads
1,223,791
Messages
6,174,605
Members
452,574
Latest member
hang_and_bang

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