Changing the Active sheet in a macro

bearcub

Well-known Member
Joined
May 18, 2005
Messages
701
Office Version
  1. 365
  2. 2013
  3. 2010
  4. 2007
Platform
  1. Windows
Hi,

I asked a question last week about filtering out a number of items in a list. However, the sheet that is used for this prois set to the Activesheet:

/
Code:
    'master sheet
    Set ws1Master = ActiveSheet  ' --> [B]Can this be changed to a new created sheet?[/B]
[code]/

I am running a series of macros where I am making a copy of the data that was just extracted from out system and removing blanks rows and certain leadership positions. This macro is run when I only have one sheet in the file called Extract. This new sheet, in this case, would be January 2018 since the data is from January.

The next macro I'm running should take the data from the January 2018 sheet and then creates several more sheets - one sheet for each item in a particular column (in this instance it would be all our local offices).

However, this macro, for now, is attached to a button on the Extract sheet. In the code snippet above, is there a way to dynamically change Activesheet to the January 2018. It has to be dynamic because the Extract copy in Feb will be called Feb 2018, etc. 

Is there a way to SetMaster sheet from Activesheet to the newly created copy (e.g. Jan 2018, January 2018, 1/2018, etc) so Excel will know to use this sheet as the source instead of the Extract sheet)?

I was thinking of recording a macro to create a button and attach the macro but I was wondering if there was a more efficient way of doing this.

Thank you for your help.

Michael
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I'm a bit confused at what you are asking, but I think what you want is this:

Code:
Dim SheetName As String
SheetName = Format(Now, "MMMM") & " " & Format(Now, "YYYY")

Set ws1Master = Sheets(SheetName)

This will set the Sheet to "January 2018" if the current month is Jan, "February 2018" if the month is Feb, etc.
 
Upvote 0
I forgot to mention that this second macro uses an input box to specify the column to filter:

Set objRange = Application.InputBox("Select Field Name To Filter", "Range Input", , , , , , 8)

Is there a way of mapping the objRange to Set wslMaster. Could I add a new line right below this to set the objRange to wsMaster?
Set wsMaster = objRange
 
Upvote 0
Is there a way of linking the input box to wsl variable?
 
Upvote 0
You can try this:
Code:
Dim SheetName As String
Dim ws1Master As Worksheet
Dim objRange As Range

SheetName = Format(Now, "MMMM") & " " & Format(Now, "YYYY")
Set ws1Master = Sheets(SheetName)
Set objRange = ws1Master.Range(Application.InputBox("Select Field Name To Filter", "Range Input", , , , , , 8).Address)
 
Upvote 0
thank you Max, that might work. I'll try it and let you know.
 
Upvote 0

Forum statistics

Threads
1,214,654
Messages
6,120,758
Members
448,991
Latest member
Hanakoro

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