How to replace multiple macros with one by creating two new subs

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
Hi, I am using macros to go between sheets and I have 20 separate macros for going to different sheets and I have an additional 20 sheets to go back to the home sheet.

I would like some help with how to create two macro/subroutines that replaces these 40 macros with two new macros.

The first macro should take sheet name, scroll area, and tab color as arguments. The second macro should take sheet name and tab color as arguments.

Code:
Sub StartOption1()
    ActiveWorkbook.worksheets("Option1").Visible = True
    ActiveWorkbook.worksheets("Home").Visible = False
    worksheets("Option1").Activate
    Sheets("Option1").ScrollArea = "C1:X37"
    Sheets("Option1").Tab.Color = RGB(182, 173, 165)
End Sub


Sub StarOption1Back()
    ActiveWorkbook.worksheets("Home").Visible = True
    ActiveWorkbook.worksheets("Option1").Visible = False
    worksheets("Home").Activate
    Sheets("Home").Tab.Color = RGB(182, 173, 165)   
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I think I understand how to pass the parameters to the sub but not how to use them in the sub.

Something like this?

Code:
Private Sub GoToTest(sheetname as worksheet, optional scroll_area as range, optional tab_color as string)
worksheets(sheetname).Visible = True
worksheets(sheetname).ScrollArea = scroll_area
worksheets(sheetname).Tab.Color= tab_color
worksheets(sheetname).Activate
End sub

Code:
Public Sub GobackTest(sheetname as worksheet, optional tab_color as string)
worksheets(sheetname).Visible = True

worksheets(sheetname).Activate

End sub
 
Upvote 0
Try it like
Code:
Private Sub GoToTest(sheetname As Worksheet, Optional scroll_area As String, Optional tab_color As String)
sheetname.Visible = True
sheetname.ScrollArea = scroll_area
sheetname.Tab.Color = tab_color
sheetname.Activate
End Sub
Sub chk()
   Call GoToTest(Sheets("pcode"), "C1:X37", RGB(182, 173, 165))
End Sub
 
Upvote 0
Hi Fluff,

thank you very much for your example! I now better understand how to pass arguments to subs and how to try to remove unnecessary code!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
What I am asking is if I can use the same macro to hide a sheet?
 
Upvote 0
Yes you can but then you would not be able to make the sheet active.
 
Upvote 0
Hi Fluff,

thank you for your answer!

If I wanted to hide the activesheet to be xlVeryHidden and to activate the sheet called start, what would I change?
 
Upvote 0
Try
Code:
Private Sub GoToTest(NewWs As Worksheet, OldWs As Worksheet, Optional scroll_area As String, Optional tab_color As String)
NewWs.Visible = True
NewWs.ScrollArea = scroll_area
If tab_color <> "" Then NewWs.Tab.Color = tab_color
NewWs.Activate
OldWs.Visible = xlSheetVeryHidden
End Sub
Sub chk()
   Call GoToTest(Sheets("pcode"), ActiveSheet)
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,181
Messages
6,123,513
Members
449,101
Latest member
mgro123

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