Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default macro to execute code on all worksheets in workbook

I am trying to execute the following code on multiple sheets in my workbook.
However, when I run the macro, it only executes on the active sheet. How do
I pass the focus to each next sheet? Something is missing...

Thanks!
Gretta

Dim sh As Worksheet
Dim DestSh As Worksheet
Dim shLast As Long
Dim Last As Long

Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Worksheets
Columns("B:C").Select
Range("B16").Activate
Selection.Unmerge
Columns("B:F").Select
With Selection
.HorizontalAlignment = xlLeft
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Range("B1").Activate
Selection.EntireColumn.Hidden = False
Columns("B:C").Select
Selection.Insert Shift:=xlToRight
Range("D3").Select
Selection.Copy
Range("B5").Select
ActiveSheet.Paste
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Next sh
Application.ScreenUpdating = True
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default macro to execute code on all worksheets in workbook

You are already looping through the worksheets in the workbook. So, without
looking much at what the code is doing inside the loop, I think a quick fix
would be to activate the sheet before executing the code inside the loop. In
the code below, the only line that I added is the one that says "sh.Activate".



Dim sh As Worksheet
Dim DestSh As Worksheet
Dim shLast As Long
Dim Last As Long

Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Worksheets
sh.Activate 'Activate the sheet
Columns("B:C").Select
Range("B16").Activate
Selection.Unmerge
Columns("B:F").Select
With Selection
.HorizontalAlignment = xlLeft
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Range("B1").Activate
Selection.EntireColumn.Hidden = False
Columns("B:C").Select
Selection.Insert Shift:=xlToRight
Range("D3").Select
Selection.Copy
Range("B5").Select
ActiveSheet.Paste
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Next sh
Application.ScreenUpdating = True
End Sub



--
Hope that helps.

Vergel Adriano


"Gretta" wrote:

I am trying to execute the following code on multiple sheets in my workbook.
However, when I run the macro, it only executes on the active sheet. How do
I pass the focus to each next sheet? Something is missing...

Thanks!
Gretta

Dim sh As Worksheet
Dim DestSh As Worksheet
Dim shLast As Long
Dim Last As Long

Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Worksheets
Columns("B:C").Select
Range("B16").Activate
Selection.Unmerge
Columns("B:F").Select
With Selection
.HorizontalAlignment = xlLeft
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Range("B1").Activate
Selection.EntireColumn.Hidden = False
Columns("B:C").Select
Selection.Insert Shift:=xlToRight
Range("D3").Select
Selection.Copy
Range("B5").Select
ActiveSheet.Paste
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Next sh
Application.ScreenUpdating = True
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default macro to execute code on all worksheets in workbook

Try something like this

Dim sh As Worksheet
Dim DestSh As Worksheet
Dim shLast As Long
Dim Last As Long

Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Worksheets
sh.Columns("B:C").Select
sh.Range("B16").Activate
Selection.UnMerge
sh.Columns("B:F").Select
With Selection
.HorizontalAlignment = xlLeft
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
sh.Cells.Select
sh.Range("B1").Activate
Selection.EntireColumn.Hidden = False
sh.Columns("B:C").Select
Selection.Insert Shift:=xlToRight
sh.Range("D3").Select
Selection.Copy
sh.Range("B5").Select
ActiveSheet.Paste
sh.Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
sh.Range("C6").Select
ActiveSheet.Paste 'Is this Activesheet or sh?
Next sh

I'd step through it line by line to ensure it's doing what you want.



"Gretta" wrote:

I am trying to execute the following code on multiple sheets in my workbook.
However, when I run the macro, it only executes on the active sheet. How do
I pass the focus to each next sheet? Something is missing...

Thanks!
Gretta

Dim sh As Worksheet
Dim DestSh As Worksheet
Dim shLast As Long
Dim Last As Long

Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Worksheets
Columns("B:C").Select
Range("B16").Activate
Selection.Unmerge
Columns("B:F").Select
With Selection
.HorizontalAlignment = xlLeft
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Range("B1").Activate
Selection.EntireColumn.Hidden = False
Columns("B:C").Select
Selection.Insert Shift:=xlToRight
Range("D3").Select
Selection.Copy
Range("B5").Select
ActiveSheet.Paste
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Next sh
Application.ScreenUpdating = True
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default macro to execute code on all worksheets in workbook

Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Worksheets
sh.select '<-- added

Might work.



Gretta wrote:

I am trying to execute the following code on multiple sheets in my workbook.
However, when I run the macro, it only executes on the active sheet. How do
I pass the focus to each next sheet? Something is missing...

Thanks!
Gretta

Dim sh As Worksheet
Dim DestSh As Worksheet
Dim shLast As Long
Dim Last As Long

Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Worksheets
Columns("B:C").Select
Range("B16").Activate
Selection.Unmerge
Columns("B:F").Select
With Selection
.HorizontalAlignment = xlLeft
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Range("B1").Activate
Selection.EntireColumn.Hidden = False
Columns("B:C").Select
Selection.Insert Shift:=xlToRight
Range("D3").Select
Selection.Copy
Range("B5").Select
ActiveSheet.Paste
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Next sh
Application.ScreenUpdating = True
End Sub


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default macro to execute code on all worksheets in workbook

Alternatively, you could use

for each sh in activeworkbook.worksheets
sh.activate
...


"Gretta" wrote:

I am trying to execute the following code on multiple sheets in my workbook.
However, when I run the macro, it only executes on the active sheet. How do
I pass the focus to each next sheet? Something is missing...

Thanks!
Gretta

Dim sh As Worksheet
Dim DestSh As Worksheet
Dim shLast As Long
Dim Last As Long

Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Worksheets
Columns("B:C").Select
Range("B16").Activate
Selection.Unmerge
Columns("B:F").Select
With Selection
.HorizontalAlignment = xlLeft
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Range("B1").Activate
Selection.EntireColumn.Hidden = False
Columns("B:C").Select
Selection.Insert Shift:=xlToRight
Range("D3").Select
Selection.Copy
Range("B5").Select
ActiveSheet.Paste
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Next sh
Application.ScreenUpdating = True
End Sub



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default macro to execute code on all worksheets in workbook

Thank you to all!

Sh.Activate worked great!

/grt

"Barb Reinhardt" wrote:

Alternatively, you could use

for each sh in activeworkbook.worksheets
sh.activate
...


"Gretta" wrote:

I am trying to execute the following code on multiple sheets in my workbook.
However, when I run the macro, it only executes on the active sheet. How do
I pass the focus to each next sheet? Something is missing...

Thanks!
Gretta

Dim sh As Worksheet
Dim DestSh As Worksheet
Dim shLast As Long
Dim Last As Long

Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Worksheets
Columns("B:C").Select
Range("B16").Activate
Selection.Unmerge
Columns("B:F").Select
With Selection
.HorizontalAlignment = xlLeft
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Range("B1").Activate
Selection.EntireColumn.Hidden = False
Columns("B:C").Select
Selection.Insert Shift:=xlToRight
Range("D3").Select
Selection.Copy
Range("B5").Select
ActiveSheet.Paste
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Range("C6").Select
ActiveSheet.Paste
Next sh
Application.ScreenUpdating = True
End Sub

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
Check My Code Please - Execute Access Macro PapaDos Excel Programming 1 December 7th 06 03:08 PM
How can I use VB code to execute macro when double-clicking cell? JDay01 Excel Programming 2 June 13th 06 03:12 PM
macro don't execute a step in the code Shadok Excel Programming 3 December 19th 05 09:52 AM
how to execute macro from vb 6.0 code? B Deepak Excel Programming 4 September 30th 05 07:57 AM
execute macro in other workbook Sylvain Excel Programming 3 August 22nd 05 09:24 AM


All times are GMT +1. The time now is 04:35 PM.

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

About Us

"It's about Microsoft Excel"