Formatting every sheet in workbook

georgecomstock

New Member
Joined
May 27, 2011
Messages
18
I thought that the following code would apply formatting to every sheet in the current workbook, but it seems to be applying it only to the current active sheet. Could someone please explain why and provide a solution, many thanks.
Code:
Public Sub formatold()
Dim ws As Worksheet
For Each ws In Worksheets
 Range("A:af").Select
  Selection.Font.Size = 8
   Range("a:A").Select
   Selection.NumberFormat = "dd-mmm-yyyy"
    Range("p:p").Select
    Selection.NumberFormat = "0.00"
     Range("s:s").Select
    Selection.NumberFormat = "0.00"
     Range("t:t").Select
    Selection.NumberFormat = "0.00"
     Range("v:v").Select
    Selection.NumberFormat = "0.00"
     Range("y:y").Select
    Selection.NumberFormat = "0.00"
     Range("z:z").Select
    Selection.NumberFormat = "0.00"
     Range("ab:ab").Select
      Selection.NumberFormat = "0.00"
 
Next ws
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I'm away from my Excel program at the moment, but the WorkSheets object has a .CopyAcrossSheets (?) method that allows you to copy values, formulas or formatting across any Worksheets object, including ThisWorkbook.Worksheets.

Get one sheet, format it the way that you want and then use that method to transfer the formating to all sheets in the workbook.
 
Upvote 0
Try
Code:
Public Sub formatold()
Dim ws As Worksheet
For Each ws In Worksheets
With ws
 .Range("A:af").Font.Size = 8
   .Range("a:A").NumberFormat = "dd-mmm-yyyy"
    .Range("p:p").NumberFormat = "0.00"
     .Range("s:s").NumberFormat = "0.00"
     .Range("t:t").NumberFormat = "0.00"
     .Range("v:v").NumberFormat = "0.00"
     .Range("y:y").NumberFormat = "0.00"
     .Range("z:z").NumberFormat = "0.00"
     .Range("ab:ab").NumberFormat = "0.00"
 End With
Next ws
End Sub
 
Upvote 0
I believe the reason why it was happening was because you put the code in your activeworksheet module.

When that happens, if you do not specify which worksheet it is when you're calling "Range" or other stuff, it will always happen in that worksheet. So, you would have to specify the worksheets which I have done with "With ws" code.
 
Upvote 0
I'm glad you found a solution. As a follow up to my earlier post, the method is the .FillAcrossSheets method. This code uses that approach

Code:
Sub test()
With ThisWorkbook
    With .WorkSheets(1)
        .Range("A:af").Font.Size = 8
        .Range("a:A").NumberFormat = "dd-mmm-yyyy"
        .Range("p:p").NumberFormat = "0.00"
        .Range("s:s").NumberFormat = "0.00"
        .Range("t:t").NumberFormat = "0.00"
        .Range("v:v").NumberFormat = "0.00"
        .Range("y:y").NumberFormat = "0.00"
        .Range("z:z").NumberFormat = "0.00"
        .Range("ab:ab").NumberFormat = "0.00"
    End With
    .Worksheets.FillAcrossSheets Range:=.WorkSheets(1).Range("A:AF"), Type:=xlFillWithFormats
End With
End Sub
 
Upvote 0
Mike thank you for taking the trouble to provide a solution. I wish I could appreciate the merits of the two similar but subtely different solutions, but as I could not even fashion a solution for myself I am at a loss. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,334
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top