Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default For Each...Dynamically formatting multiple worksheets

I am creating multiple worksheets in a single workbook.
The workbook is being created by another application (Data
Junction). Each worksheet has the same columns, however
the number of worksheets (and their corrsponding sheet
names) will vary on a monthly basis.

I have been able to succssfully format a single worksheet,
but am having trouble applying the same
formatting/functions to multiple sheets in the same
workbook.

Example code is provided below...How might I apply the For
Each...Next syntax around this so that all worksheets are
formatted the same?

Thanks

*******

'Define the Excel Application Object
Dim xlApp As Object

'Create the Excel Object
Set xlApp = CreateObject("Excel.Application")

'Open the desired Excel File
xlApp.Application.Workbooks.Open "c:sample.xls"

'Insert Rowsfor Control Totals
xlApp.Application.Rows("1:3").Select
xlApp.Application.Selection.Insert Shift = "xlDown"

'Calculate Totals
xlApp.Application.Range("K2").Select
xlApp.Application.ActiveCell.FormulaR1C1 = "=SUM(R[3]C:R
[65534]C)"

'Format Totals
xlApp.Application.Range("K2:AD2").Select
xlApp.Application.Selection.Style = "Comma"

'Freeze Pane
xlApp.Application.Range("D5").Select
xlApp.Application.ActiveWindow.FreezePanes = TRUE

'Format field to 3 decimal places
xlApp.Application.Columns("H:H").Select
xlApp.Application.Selection.NumberFormat

'Autofit Columns
xlApp.Application.Cells.Select

xlApp.Application.Cells.EntireColumn.AutoFit



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default For Each...Dynamically formatting multiple worksheets

To loop though all sheets in a workbook use something like the following

Sub MultiSheet()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets
sht.Range("A1").Value = 10
Next sht
End Sub

Precede each command with sht. so that it is performed on each worksheet as
the code loops through all worksheets


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default For Each...Dynamically formatting multiple worksheets

Sub tester3()
'Define the Excel Application Object
Dim xlApp As Object

'Create the Excel Object
Set xlApp = CreateObject("Excel.Application")

'Open the desired Excel File
Set xlbk = xlApp.Workbooks.Open("c:sample.xls")

For Each sh In xlbk.Worksheets
'Insert Rowsfor Control Totals
With sh
.Activate
.Rows("1:3").Insert

'Calculate Totals
..Range("K2").FormulaR1C1 = _
"=SUM(R[3]C:R[65534]C)"

'Format Totals
..Range("K2:AD2").Style = "Comma"

'Freeze Pane
..Range("D5").Select
..Parent.Windows(1).FreezePanes = True

'Format field to 3 decimal places
..Columns("H:H").NumberFormat = "#,###.000"

'Autofit Columns
..Cells.EntireColumn.AutoFit
End With
Next sh
End Sub


--
Regards,
Tom Ogilvy

Robert wrote in message
...
I am creating multiple worksheets in a single workbook.
The workbook is being created by another application (Data
Junction). Each worksheet has the same columns, however
the number of worksheets (and their corrsponding sheet
names) will vary on a monthly basis.

I have been able to succssfully format a single worksheet,
but am having trouble applying the same
formatting/functions to multiple sheets in the same
workbook.

Example code is provided below...How might I apply the For
Each...Next syntax around this so that all worksheets are
formatted the same?

Thanks

*******

'Define the Excel Application Object
Dim xlApp As Object

'Create the Excel Object
Set xlApp = CreateObject("Excel.Application")

'Open the desired Excel File
xlApp.Application.Workbooks.Open "c:sample.xls"

'Insert Rowsfor Control Totals
xlApp.Application.Rows("1:3").Select
xlApp.Application.Selection.Insert Shift = "xlDown"

'Calculate Totals
xlApp.Application.Range("K2").Select
xlApp.Application.ActiveCell.FormulaR1C1 = "=SUM(R[3]C:R
[65534]C)"

'Format Totals
xlApp.Application.Range("K2:AD2").Select
xlApp.Application.Selection.Style = "Comma"

'Freeze Pane
xlApp.Application.Range("D5").Select
xlApp.Application.ActiveWindow.FreezePanes = TRUE

'Format field to 3 decimal places
xlApp.Application.Columns("H:H").Select
xlApp.Application.Selection.NumberFormat

'Autofit Columns
xlApp.Application.Cells.Select

xlApp.Application.Cells.EntireColumn.AutoFit





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
Copy worksheets and save files dynamically BabyMc Excel Discussion (Misc queries) 3 March 19th 10 03:26 PM
Dynamically linking subserviant worksheets to a master worksheet jcontrer Excel Worksheet Functions 2 October 5th 07 05:34 PM
formatting multiple worksheets Sylvia Excel Worksheet Functions 1 February 24th 06 11:22 PM
Linking and formatting multiple worksheets kellyc Excel Worksheet Functions 2 July 14th 05 04:26 PM
Eliminate formatting dulication in multiple worksheets jcorboy Excel Worksheet Functions 1 June 18th 05 11:43 PM


All times are GMT +1. The time now is 01:54 AM.

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"