Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
dynamic range name | Excel Discussion (Misc queries) | |||
Search a worksheet, extract rows using a list from another sheet | Excel Discussion (Misc queries) | |||
Macro to search for and display data in another worksheet | Excel Worksheet Functions | |||
Does excel recognise names rather than cells? | Excel Worksheet Functions | |||
Executing macro for all worksheet from a different worksheet | New Users to Excel |