Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default Get macro to run on any worksheet without renaming the sheet?

I'm trying to get a macro I've designed to run on any worksheet.

However, when I start the macro it errors and needs to be on a worksheet
that has the same name as the one I designed it on.

Is there any way to get the macro to run on a worksheet, regartdless of what
the name is?
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 180
Default Get macro to run on any worksheet without renaming the sheet?

Absolutely, it's possible. However, the ease of doing so depends heavily on
the specifics, such as how much worksheet switching did you record and
whether you've got formulas that reference other worksheets. Let's try the
simplest approach first. Open the workbook containing the macro and switch
to the VB Editor (alt + F11).
In the Project Explorer (probably at the top left; if it's not visible, View
Project Explorer), double click on Modules if necessary so that Module1 is

visible. Click on Module1 (or whichever module contains your macro). Then
seach the macro for a statement like:
Sheets("Sheet2").Select
Is there only one like that? If so, just comment it out by adding an
apostrophe before the statement:
'Sheets("Sheet2").Select
If there are multiple lines like that, with different sheets selected, we'll
have a little more work to do. And if there are formulas that reference the
worksheet (ex: =sum(Sheet2!A1), more work still.
If this simple approach doesn't cut it, please provide more detail,
especially about which sheet(s) you're switching between and what formulas in
the macro contain worksheet references.



"Andrew B" wrote:

I'm trying to get a macro I've designed to run on any worksheet.

However, when I start the macro it errors and needs to be on a worksheet
that has the same name as the one I designed it on.

Is there any way to get the macro to run on a worksheet, regartdless of what
the name is?

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Get macro to run on any worksheet without renaming the sheet?

I'm afraid the simple approach wasn't enough!

There are numerous references to the original sheet, mainly for switching to
copy data from the original sheet to a new sheet.

Basically, if I could get the macro to rename any sheet I apply it to to
"data" then the rest of the macro would run okay - if thats possible.

Thanks

Andrew

"bpeltzer" wrote:

Absolutely, it's possible. However, the ease of doing so depends heavily on
the specifics, such as how much worksheet switching did you record and
whether you've got formulas that reference other worksheets. Let's try the
simplest approach first. Open the workbook containing the macro and switch
to the VB Editor (alt + F11).
In the Project Explorer (probably at the top left; if it's not visible, View
Project Explorer), double click on Modules if necessary so that Module1 is

visible. Click on Module1 (or whichever module contains your macro). Then
seach the macro for a statement like:
Sheets("Sheet2").Select
Is there only one like that? If so, just comment it out by adding an
apostrophe before the statement:
'Sheets("Sheet2").Select
If there are multiple lines like that, with different sheets selected, we'll
have a little more work to do. And if there are formulas that reference the
worksheet (ex: =sum(Sheet2!A1), more work still.
If this simple approach doesn't cut it, please provide more detail,
especially about which sheet(s) you're switching between and what formulas in
the macro contain worksheet references.



"Andrew B" wrote:

I'm trying to get a macro I've designed to run on any worksheet.

However, when I start the macro it errors and needs to be on a worksheet
that has the same name as the one I designed it on.

Is there any way to get the macro to run on a worksheet, regartdless of what
the name is?

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Get macro to run on any worksheet without renaming the sheet?

Don't refer to the worksheet by name.

You can use activesheet instead.

activesheet.range("a1").value = "hi there"

worksheets("sheet9999").range("a1:b99").copy _
destination:=activesheet.range("C1")



Andrew B wrote:

I'm afraid the simple approach wasn't enough!

There are numerous references to the original sheet, mainly for switching to
copy data from the original sheet to a new sheet.

Basically, if I could get the macro to rename any sheet I apply it to to
"data" then the rest of the macro would run okay - if thats possible.

Thanks

Andrew

"bpeltzer" wrote:

Absolutely, it's possible. However, the ease of doing so depends heavily on
the specifics, such as how much worksheet switching did you record and
whether you've got formulas that reference other worksheets. Let's try the
simplest approach first. Open the workbook containing the macro and switch
to the VB Editor (alt + F11).
In the Project Explorer (probably at the top left; if it's not visible, View
Project Explorer), double click on Modules if necessary so that Module1 is

visible. Click on Module1 (or whichever module contains your macro). Then
seach the macro for a statement like:
Sheets("Sheet2").Select
Is there only one like that? If so, just comment it out by adding an
apostrophe before the statement:
'Sheets("Sheet2").Select
If there are multiple lines like that, with different sheets selected, we'll
have a little more work to do. And if there are formulas that reference the
worksheet (ex: =sum(Sheet2!A1), more work still.
If this simple approach doesn't cut it, please provide more detail,
especially about which sheet(s) you're switching between and what formulas in
the macro contain worksheet references.



"Andrew B" wrote:

I'm trying to get a macro I've designed to run on any worksheet.

However, when I start the macro it errors and needs to be on a worksheet
that has the same name as the one I designed it on.

Is there any way to get the macro to run on a worksheet, regartdless of what
the name is?


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 180
Default Get macro to run on any worksheet without renaming the sheet?

Then I'd create a variable to store the active sheet when the macro is
invoked, then change the select statements to use the variable rather than a
fixed name.
Right after the Sub line, and before any other statements add:
Dim StartSheet As String
StartSheet = ActiveSheet.Name
Then change lines such as
Sheets("Sheet2").Select
to read
Sheets(StartSheet).Select
Note that you only want to make this substitution when your starting sheet
is being selected, NOT when other sheets are selected. Also, I've kind of
assumed that whatever OTHER sheets your macro is using will exist in the
workbook, because those sheets' names are hard-coded in your macro as well.
--Bruce

"Andrew B" wrote:

I'm afraid the simple approach wasn't enough!

There are numerous references to the original sheet, mainly for switching to
copy data from the original sheet to a new sheet.

Basically, if I could get the macro to rename any sheet I apply it to to
"data" then the rest of the macro would run okay - if thats possible.

Thanks

Andrew

"bpeltzer" wrote:

Absolutely, it's possible. However, the ease of doing so depends heavily on
the specifics, such as how much worksheet switching did you record and
whether you've got formulas that reference other worksheets. Let's try the
simplest approach first. Open the workbook containing the macro and switch
to the VB Editor (alt + F11).
In the Project Explorer (probably at the top left; if it's not visible, View
Project Explorer), double click on Modules if necessary so that Module1 is

visible. Click on Module1 (or whichever module contains your macro). Then
seach the macro for a statement like:
Sheets("Sheet2").Select
Is there only one like that? If so, just comment it out by adding an
apostrophe before the statement:
'Sheets("Sheet2").Select
If there are multiple lines like that, with different sheets selected, we'll
have a little more work to do. And if there are formulas that reference the
worksheet (ex: =sum(Sheet2!A1), more work still.
If this simple approach doesn't cut it, please provide more detail,
especially about which sheet(s) you're switching between and what formulas in
the macro contain worksheet references.



"Andrew B" wrote:

I'm trying to get a macro I've designed to run on any worksheet.

However, when I start the macro it errors and needs to be on a worksheet
that has the same name as the one I designed it on.

Is there any way to get the macro to run on a worksheet, regartdless of what
the name is?



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Get macro to run on any worksheet without renaming the sheet?

Or even just use a variable to represent that worksheet:

Dim ActWks as worksheet
set ActWks = activesheet
....
'activate the workbook, just in case a different workbook is active.
actwks.parent.activate
'select the worksheet
actwks.select



bpeltzer wrote:

Then I'd create a variable to store the active sheet when the macro is
invoked, then change the select statements to use the variable rather than a
fixed name.
Right after the Sub line, and before any other statements add:
Dim StartSheet As String
StartSheet = ActiveSheet.Name
Then change lines such as
Sheets("Sheet2").Select
to read
Sheets(StartSheet).Select
Note that you only want to make this substitution when your starting sheet
is being selected, NOT when other sheets are selected. Also, I've kind of
assumed that whatever OTHER sheets your macro is using will exist in the
workbook, because those sheets' names are hard-coded in your macro as well.
--Bruce

"Andrew B" wrote:

I'm afraid the simple approach wasn't enough!

There are numerous references to the original sheet, mainly for switching to
copy data from the original sheet to a new sheet.

Basically, if I could get the macro to rename any sheet I apply it to to
"data" then the rest of the macro would run okay - if thats possible.

Thanks

Andrew

"bpeltzer" wrote:

Absolutely, it's possible. However, the ease of doing so depends heavily on
the specifics, such as how much worksheet switching did you record and
whether you've got formulas that reference other worksheets. Let's try the
simplest approach first. Open the workbook containing the macro and switch
to the VB Editor (alt + F11).
In the Project Explorer (probably at the top left; if it's not visible, View
Project Explorer), double click on Modules if necessary so that Module1 is
visible. Click on Module1 (or whichever module contains your macro). Then
seach the macro for a statement like:
Sheets("Sheet2").Select
Is there only one like that? If so, just comment it out by adding an
apostrophe before the statement:
'Sheets("Sheet2").Select
If there are multiple lines like that, with different sheets selected, we'll
have a little more work to do. And if there are formulas that reference the
worksheet (ex: =sum(Sheet2!A1), more work still.
If this simple approach doesn't cut it, please provide more detail,
especially about which sheet(s) you're switching between and what formulas in
the macro contain worksheet references.



"Andrew B" wrote:

I'm trying to get a macro I've designed to run on any worksheet.

However, when I start the macro it errors and needs to be on a worksheet
that has the same name as the one I designed it on.

Is there any way to get the macro to run on a worksheet, regartdless of what
the name is?


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Get macro to run on any worksheet without renaming the sheet?

Thanks Guys, That last Active Sheet thing worked a treat.

Andrew

"Dave Peterson" wrote:

Or even just use a variable to represent that worksheet:

Dim ActWks as worksheet
set ActWks = activesheet
....
'activate the workbook, just in case a different workbook is active.
actwks.parent.activate
'select the worksheet
actwks.select



bpeltzer wrote:

Then I'd create a variable to store the active sheet when the macro is
invoked, then change the select statements to use the variable rather than a
fixed name.
Right after the Sub line, and before any other statements add:
Dim StartSheet As String
StartSheet = ActiveSheet.Name
Then change lines such as
Sheets("Sheet2").Select
to read
Sheets(StartSheet).Select
Note that you only want to make this substitution when your starting sheet
is being selected, NOT when other sheets are selected. Also, I've kind of
assumed that whatever OTHER sheets your macro is using will exist in the
workbook, because those sheets' names are hard-coded in your macro as well.
--Bruce

"Andrew B" wrote:

I'm afraid the simple approach wasn't enough!

There are numerous references to the original sheet, mainly for switching to
copy data from the original sheet to a new sheet.

Basically, if I could get the macro to rename any sheet I apply it to to
"data" then the rest of the macro would run okay - if thats possible.

Thanks

Andrew

"bpeltzer" wrote:

Absolutely, it's possible. However, the ease of doing so depends heavily on
the specifics, such as how much worksheet switching did you record and
whether you've got formulas that reference other worksheets. Let's try the
simplest approach first. Open the workbook containing the macro and switch
to the VB Editor (alt + F11).
In the Project Explorer (probably at the top left; if it's not visible, View
Project Explorer), double click on Modules if necessary so that Module1 is
visible. Click on Module1 (or whichever module contains your macro). Then
seach the macro for a statement like:
Sheets("Sheet2").Select
Is there only one like that? If so, just comment it out by adding an
apostrophe before the statement:
'Sheets("Sheet2").Select
If there are multiple lines like that, with different sheets selected, we'll
have a little more work to do. And if there are formulas that reference the
worksheet (ex: =sum(Sheet2!A1), more work still.
If this simple approach doesn't cut it, please provide more detail,
especially about which sheet(s) you're switching between and what formulas in
the macro contain worksheet references.



"Andrew B" wrote:

I'm trying to get a macro I've designed to run on any worksheet.

However, when I start the macro it errors and needs to be on a worksheet
that has the same name as the one I designed it on.

Is there any way to get the macro to run on a worksheet, regartdless of what
the name is?


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic range name Jonathan Cooper Excel Discussion (Misc queries) 6 April 6th 06 09:58 PM
Search a worksheet, extract rows using a list from another sheet bobf Excel Discussion (Misc queries) 9 August 31st 05 04:56 AM
Macro to search for and display data in another worksheet Mark H Excel Worksheet Functions 0 June 14th 05 12:40 PM
Does excel recognise names rather than cells? Sue Excel Worksheet Functions 9 May 22nd 05 04:51 AM
Executing macro for all worksheet from a different worksheet Biti New Users to Excel 3 December 8th 04 10:05 AM


All times are GMT +1. The time now is 05:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"