VBA: Formatting Cells on Two Sheets

nickshep85

New Member
Joined
Mar 21, 2012
Messages
37
I have a code with which I am trying to format Cells A1:I2 on two sheets. I have got the code to add in the formulas and values into the cells, but the code only formats the cells in the first worksheet. Can anyone tell me where I am going wrong?

Code:
Sub Transfer_Data()

'Add New Workbook
Workbooks.Add template:=xlWorksheet
'Add New Sheet
ActiveWorkbook.Sheets.Add
'Name Sheets
Sheets("Sheet1").Name = "Tyres"
Sheets("Sheet2").Name = "Mechanical"

Dim ws As Worksheet
On Error Resume Next
For Each ws In Worksheets
    With ws
    .Range("A1").FormulaR1C1 = "=MID(CELL(""filename""),SEARCH(""["",CELL(""filename""))+1, SEARCH(""]"",CELL(""filename""))-SEARCH(""["",CELL(""filename""))-5)"

    .Range("B1").FormulaR1C1 = "=today()"
    .Range("B1").Select

    .Range("B1").Copy
    .Range("B1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    .Application.Goto Reference:="R1C1"
    .Application.CutCopyMode = False

    .Range("A2").Value = "CODE"
    .Range("B2").Value = "DESCRIPTION"
    .Range("C2").Value = "XXX"
    .Range("D2").Value = "XXX"
    .Range("E2").Value = "XXX"
    .Range("F2").Value = "XXX"
    .Range("G2").Value = "PRICE"
    .Range("H2").Value = "XXX"
    .Range("I2").Value = "XXX"

    .Range("A1:I2").Select
    With Selection
      .Font.Size = 14
      .Font.Bold = True
      .Font.Color = vbWhite
      .Columns.AutoFit
      .Interior.Color = vbBlue
    End With
    
    End With
    
    Next ws
    
        
Range("A1").Select


End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try

Code:
        Sub Transfer_Data()

        'Add New Workbook
        Workbooks.Add template:=xlWorksheet
        'Add New Sheet
        ActiveWorkbook.Sheets.Add
        'Name Sheets
        Sheets("Sheet1").Name = "Tyres"
        Sheets("Sheet2").Name = "Mechanical"

        Dim ws As Worksheet
        On Error Resume Next
        For Each ws In Worksheets
            With ws
            .Range("A1").FormulaR1C1 = "=MID(CELL(""filename""),SEARCH(""["",CELL(""filename""))+1, SEARCH(""]"",CELL(""filename""))-SEARCH(""["",CELL(""filename""))-5)"

            .Range("B1").FormulaR1C1 = "=today()"
            .Range("B1").Select

            .Range("B1").Copy
            .Range("B1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            .Application.Goto Reference:="R1C1"
            .Application.CutCopyMode = False

            .Range("A2").Value = "CODE"
            .Range("B2").Value = "DESCRIPTION"
            .Range("C2").Value = "XXX"
            .Range("D2").Value = "XXX"
            .Range("E2").Value = "XXX"
            .Range("F2").Value = "XXX"
            .Range("G2").Value = "PRICE"
            .Range("H2").Value = "XXX"
            .Range("I2").Value = "XXX"

            With .Range("A1:I2")
              .Font.Size = 14
              .Font.Bold = True
              .Font.Color = vbWhite
              .Columns.AutoFit
              .Interior.Color = vbBlue
            End With
            
            End With
            
            Next ws
            
                
        Range("A1").Select


        End Sub
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,613
Members
449,238
Latest member
wcbyers

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