Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I am trying to run a Macro (which works perfectly) in All Worksheets of a
particular Workbook. I currently have seven Worksheets in the Workbook and have to select each Worksheet and manually run the Macro. I will eventually have 50 (maybe more) Worksheets where the Macro will need to be run. I have used the Private Sub code in VBA but can only get it to execute the Macro in the active Worksheet when the Workbook opens. Is there code that could/would run the Macro in All the Sheets? Sheet1, Sheet2, Sheet3, Sheet4 Any help would be greatly appreciated. -- SHD |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
For Each sh In ActiveWorkbook.Worksheets
Call MyMacro Next sh You might need to pass the sh object to the macro so that it can work on that, like this For Each sh In ActiveWorkbook.Worksheets Call MyMacro(sh) Next sh Sub MyMacro(pSheet as worksheet) With pSheet '... End With End Sub -- HTH Bob Phillips (remove nothere from email address if mailing direct) "YellowBird" wrote in message ... I am trying to run a Macro (which works perfectly) in "All" Worksheets of a particular Workbook. I currently have seven Worksheets in the Workbook and have to select each Worksheet and manually run the Macro. I will eventually have 50 (maybe more) Worksheets where the Macro will need to be run. I have used the "Private Sub" code in VBA but can only get it to execute the Macro in the active Worksheet when the Workbook opens. Is there code that could/would run the Macro in "All" the Sheets? Sheet1, Sheet2, Sheet3, Sheet4. Any help would be greatly appreciated. -- SHD |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Bob,
I'm not sure if you reviewed the reply I sent to Gary but basically I had the same result with your suggestion. The Marco ran 7 times which I assume was 1 for each Worksheet in the Workbook. It doesn't seem to be switching to the other Worksheets. Any other thoughts/suggestions are welcome. Thanks. -- SHD "Bob Phillips" wrote: For Each sh In ActiveWorkbook.Worksheets Call MyMacro Next sh You might need to pass the sh object to the macro so that it can work on that, like this For Each sh In ActiveWorkbook.Worksheets Call MyMacro(sh) Next sh Sub MyMacro(pSheet as worksheet) With pSheet '... End With End Sub -- HTH Bob Phillips (remove nothere from email address if mailing direct) "YellowBird" wrote in message ... I am trying to run a Macro (which works perfectly) in "All" Worksheets of a particular Workbook. I currently have seven Worksheets in the Workbook and have to select each Worksheet and manually run the Macro. I will eventually have 50 (maybe more) Worksheets where the Macro will need to be run. I have used the "Private Sub" code in VBA but can only get it to execute the Macro in the active Worksheet when the Workbook opens. Is there code that could/would run the Macro in "All" the Sheets? Sheet1, Sheet2, Sheet3, Sheet4. Any help would be greatly appreciated. -- SHD |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
It will run 7 time but you need to adjust your code for each individual run
to address the correct sheet. I showed the basic mechanism earlier, but without seeing your macro, I cannot absolutely state what would need adjusting. There is no need to activate each sheet, you can work on them without doing so. -- HTH Bob Phillips (remove nothere from email address if mailing direct) "YellowBird" wrote in message ... Bob, I'm not sure if you reviewed the reply I sent to Gary but basically I had the same result with your suggestion. The Marco ran 7 times which I assume was 1 for each Worksheet in the Workbook. It doesn't seem to be switching to the other Worksheets. Any other thoughts/suggestions are welcome. Thanks. -- SHD "Bob Phillips" wrote: For Each sh In ActiveWorkbook.Worksheets Call MyMacro Next sh You might need to pass the sh object to the macro so that it can work on that, like this For Each sh In ActiveWorkbook.Worksheets Call MyMacro(sh) Next sh Sub MyMacro(pSheet as worksheet) With pSheet '... End With End Sub -- HTH Bob Phillips (remove nothere from email address if mailing direct) "YellowBird" wrote in message ... I am trying to run a Macro (which works perfectly) in "All" Worksheets of a particular Workbook. I currently have seven Worksheets in the Workbook and have to select each Worksheet and manually run the Macro. I will eventually have 50 (maybe more) Worksheets where the Macro will need to be run. I have used the "Private Sub" code in VBA but can only get it to execute the Macro in the active Worksheet when the Workbook opens. Is there code that could/would run the Macro in "All" the Sheets? Sheet1, Sheet2, Sheet3, Sheet4. Any help would be greatly appreciated. -- SHD |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Bob,
Thank you for your response. While I know my way around Excel, I've just started trying my "Luck" With Macros. The following is the code for the Macro that I run. At this point, I can make it run after I open the Workbook using the ALT F8 command but only in the active Worksheet. I have to switch manually from Sheet to Sheet to execute the Macro in all Sheets. I would like to call the Macro once and have it run once in all Worksheets in the Workbook. The way I would like to run the Macro is manually, one time, after the Workbook is opened and have it run the Macro in each Worksheet in the Workbook. ' Update_Research_Data Macro ' Range("A10").Select Selection.EntireRow.Insert Range("A9:F9").Select Selection.Copy Range("A10").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False Range("G9:U9").Select Selection.Copy Range("G10").Select Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Range("A9").Select End Sub Thanking you in advance for any help you can provide. It is greatly appreciated. -- SHD "Bob Phillips" wrote: It will run 7 time but you need to adjust your code for each individual run to address the correct sheet. I showed the basic mechanism earlier, but without seeing your macro, I cannot absolutely state what would need adjusting. There is no need to activate each sheet, you can work on them without doing so. -- HTH Bob Phillips (remove nothere from email address if mailing direct) "YellowBird" wrote in message ... Bob, I'm not sure if you reviewed the reply I sent to Gary but basically I had the same result with your suggestion. The Marco ran 7 times which I assume was 1 for each Worksheet in the Workbook. It doesn't seem to be switching to the other Worksheets. Any other thoughts/suggestions are welcome. Thanks. -- SHD "Bob Phillips" wrote: For Each sh In ActiveWorkbook.Worksheets Call MyMacro Next sh You might need to pass the sh object to the macro so that it can work on that, like this For Each sh In ActiveWorkbook.Worksheets Call MyMacro(sh) Next sh Sub MyMacro(pSheet as worksheet) With pSheet '... End With End Sub -- HTH Bob Phillips (remove nothere from email address if mailing direct) "YellowBird" wrote in message ... I am trying to run a Macro (which works perfectly) in "All" Worksheets of a particular Workbook. I currently have seven Worksheets in the Workbook and have to select each Worksheet and manually run the Macro. I will eventually have 50 (maybe more) Worksheets where the Macro will need to be run. I have used the "Private Sub" code in VBA but can only get it to execute the Macro in the active Worksheet when the Workbook opens. Is there code that could/would run the Macro in "All" the Sheets? Sheet1, Sheet2, Sheet3, Sheet4. Any help would be greatly appreciated. -- SHD |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sub UpdateResearch()
' Update_Research_Data Macro ' Dim sh as Worksheet for each sh in Workbook.Worksheets sh.Select Range("A10").Select Selection.EntireRow.Insert Range("A9:F9").Select Selection.Copy Range("A10").Select Selection.PasteSpecial Paste:=xlPasteValues, _ Operation:=xlNone, _ SkipBlanks:=False, _ Transpose:=False Application.CutCopyMode = False Range("G9:U9").Select Selection.Copy Range("G10").Select Selection.PasteSpecial Paste:=xlPasteFormulas, _ Operation:=xlNone, _ SkipBlanks:=False, _ Transpose:=False Application.CutCopyMode = False Range("A9").Select Next Sh end sub or Sub UpdateResearch1() for each sh in Workbook.Worksheets sh.Select sh.Range("A9:F9").copy sh.Range("A10").PasteSpecial Paste:=xlPasteValues sh.Range("G9:U9").copy sh.Range("G10").PasteSpecial Paste:=xlPasteFormulas sh.Range("A9").Select Next End Sub -- Regards, Tom Ogilvy "YellowBird" wrote in message ... Bob, Thank you for your response. While I know my way around Excel, I've just started trying my "Luck" With Macros. The following is the code for the Macro that I run. At this point, I can make it run after I open the Workbook using the ALT F8 command but only in the active Worksheet. I have to switch manually from Sheet to Sheet to execute the Macro in all Sheets. I would like to call the Macro once and have it run once in all Worksheets in the Workbook. The way I would like to run the Macro is manually, one time, after the Workbook is opened and have it run the Macro in each Worksheet in the Workbook. ' Update_Research_Data Macro ' Range("A10").Select Selection.EntireRow.Insert Range("A9:F9").Select Selection.Copy Range("A10").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False Range("G9:U9").Select Selection.Copy Range("G10").Select Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Range("A9").Select End Sub Thanking you in advance for any help you can provide. It is greatly appreciated. -- SHD "Bob Phillips" wrote: It will run 7 time but you need to adjust your code for each individual run to address the correct sheet. I showed the basic mechanism earlier, but without seeing your macro, I cannot absolutely state what would need adjusting. There is no need to activate each sheet, you can work on them without doing so. -- HTH Bob Phillips (remove nothere from email address if mailing direct) "YellowBird" wrote in message ... Bob, I'm not sure if you reviewed the reply I sent to Gary but basically I had the same result with your suggestion. The Marco ran 7 times which I assume was 1 for each Worksheet in the Workbook. It doesn't seem to be switching to the other Worksheets. Any other thoughts/suggestions are welcome. Thanks. -- SHD "Bob Phillips" wrote: For Each sh In ActiveWorkbook.Worksheets Call MyMacro Next sh You might need to pass the sh object to the macro so that it can work on that, like this For Each sh In ActiveWorkbook.Worksheets Call MyMacro(sh) Next sh Sub MyMacro(pSheet as worksheet) With pSheet '... End With End Sub -- HTH Bob Phillips (remove nothere from email address if mailing direct) "YellowBird" wrote in message ... I am trying to run a Macro (which works perfectly) in "All" Worksheets of a particular Workbook. I currently have seven Worksheets in the Workbook and have to select each Worksheet and manually run the Macro. I will eventually have 50 (maybe more) Worksheets where the Macro will need to be run. I have used the "Private Sub" code in VBA but can only get it to execute the Macro in the active Worksheet when the Workbook opens. Is there code that could/would run the Macro in "All" the Sheets? Sheet1, Sheet2, Sheet3, Sheet4. Any help would be greatly appreciated. -- SHD |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Public Sub ProcessAllWorksheets()
Dim wk As Worksheet For Each wk In Application.Worksheets 'call your macro Next wk End Sub HTH, -- Gary Brown If this post was helpful, please click the ''Yes'' button next to ''Was this Post Helpfull to you?''. "YellowBird" wrote: I am trying to run a Macro (which works perfectly) in All Worksheets of a particular Workbook. I currently have seven Worksheets in the Workbook and have to select each Worksheet and manually run the Macro. I will eventually have 50 (maybe more) Worksheets where the Macro will need to be run. I have used the Private Sub code in VBA but can only get it to execute the Macro in the active Worksheet when the Workbook opens. Is there code that could/would run the Macro in All the Sheets? Sheet1, Sheet2, Sheet3, Sheet4 Any help would be greatly appreciated. -- SHD |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Gary,
I've tried the new code and it works except that instead of running the Macro in each sheet, it runs the Macro 7 times which is the number of sheets I currently have in the workbook. The following is the code with your suggested addition. Public Sub ProcessAllWorksheets() Dim wk As Worksheet For Each wk In Application.Worksheets ' Update_Research_Data Macro ' Range("A10").Select Selection.EntireRow.Insert Range("A9:F9").Select Selection.Copy Range("A10").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False Range("G9:U9").Select Selection.Copy Range("G10").Select Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Range("A9").Select Next wk -- SHD "Gary L Brown" wrote: Public Sub ProcessAllWorksheets() Dim wk As Worksheet For Each wk In Application.Worksheets 'call your macro Next wk End Sub HTH, -- Gary Brown If this post was helpful, please click the ''Yes'' button next to ''Was this Post Helpfull to you?''. "YellowBird" wrote: I am trying to run a Macro (which works perfectly) in All Worksheets of a particular Workbook. I currently have seven Worksheets in the Workbook and have to select each Worksheet and manually run the Macro. I will eventually have 50 (maybe more) Worksheets where the Macro will need to be run. I have used the Private Sub code in VBA but can only get it to execute the Macro in the active Worksheet when the Workbook opens. Is there code that could/would run the Macro in All the Sheets? Sheet1, Sheet2, Sheet3, Sheet4 Any help would be greatly appreciated. -- SHD |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
how do you calculate a running balance in multiple worksheets? | Excel Worksheet Functions | |||
Running a macro in several worksheets | Excel Discussion (Misc queries) | |||
Having a formula provide a running total from multiple worksheets | Excel Worksheet Functions | |||
VBA to consolidate multiple worksheets to 1 worksheet in running sequence | Excel Programming | |||
VBA to consolidate multiple worksheets to 1 worksheet in running sequence | Excel Programming |