Using multiple macros in one module

Rusty315

Board Regular
Joined
Sep 13, 2012
Messages
54
Hi

I'm new to VBA so apologies if this is a silly question.

I found a useful bit of code that, upon opening the workbook, hides rows on a sheet when they contain no info:

Private Sub Workbook_Open()
Dim myRng As Range
Dim myCell As Range
With Worksheets("League Table")
Set myRng = .Range("B3:B83")
For Each myCell In myRng.Cells
myCell.EntireRow.Hidden = CBool(myCell.Value = "")
Next myCell
End With
End Sub

I also need to apply this to another sheet in the same workbook when the cells contain 0 values but (and here comes the potentially stupid question) I'm not sure how to add that bit of code to the existing bit. The extra bit on its own would look like this:

Private Sub Workbook_Open()
Dim myCell As Range
With Worksheets("Team Summaries")
Set myRng = .Range("B14:B34")
For Each myCell In myRng.Cells
myCell.EntireRow.Hidden = CBool(myCell.Value = "0")
Next myCell
End With
End Sub

How can I make this one macro that will run upon opening?

Appreciate any help anyone can give me!

Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You can just add them to the one Sub like this

Rich (BB code):
Private Sub Workbook_Open()

Dim myRng As Range
Dim myCell As Range
With Worksheets("League Table")
Set myRng = .Range("B3:B83")
For Each myCell In myRng.Cells
myCell.EntireRow.Hidden = CBool(myCell.Value = "")
Next myCell
End With

Dim myCell As Range
With Worksheets("Team Summaries")
Set myRng = .Range("B14:B34")
For Each myCell In myRng.Cells
myCell.EntireRow.Hidden = CBool(myCell.Value = "0")
Next myCell
End With

End Sub


That should run the two functions in one macro
 
Upvote 0
Hi DeusXv

Thanks for your help, it's much appreciated! I pasted in the code but got the error message 'Duplicate declaration in current scope' with the second instance of myCell As Range highlighted. How do I fix that?

Cheers!
 
Upvote 0
Hi again

I figured it out, just changed the name slightly to myCell1.

Thanks heaps for your help, I genuinely appreciate it!!

Cheers
 
Upvote 0
For future reference you can also use the term "call" to refer to functions in different modules

so if you had the two codes above in two different modules and had to call on then over and over you could make one module like this

Code:
Sub Master()

Call PrivateSubWorkbook
Call PrivateSubWorkbook2

End Sub
 
Last edited:
Upvote 0
Excellent, learned something new! I'm new to VBA but this site is really helping me along. Thanks DeusXv!
 
Upvote 0
Excellent, learned something new! I'm new to VBA but this site is really helping me along. Thanks DeusXv!

No problem happy to help and yeah this site is class to help you with any issues that you have, I've found that I have learned so much more from reading threads here and looking at other peoples solutions that reading up on VBA.
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,743
Members
449,094
Latest member
dsharae57

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