VBA Question

davidlants

New Member
Joined
Aug 24, 2011
Messages
4
Hi,

Here is my code, I just want VBA to run through all the worksheets in my workbook and complete the code in it but it won't work. The strange part is that it works fine if I take the code out of the worksheet loop and use it on one individual worksheet. Any help is greatly appreciated!!!!

Sub LoopThroughSheets()

Dim Current As Worksheet

For Each Current In Worksheets

Range("E4:AS4,E6:AS6,E8:AS8").Select
Selection.Style = "Percent"
Selection.NumberFormat = "0.0%"
Range("A1").Select
ActiveCell.FormulaR1C1 = "INDEX"
Range("A1").Select
With Selection.Font
.Name = "Calibri"
.Size = 6
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Activesheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="INDEX!A1", TextToDisplay:="INDEX"


Next


End Sub
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
try this (note: untested)

Code:
Sub LoopThroughSheets()
Dim Current As Worksheet
For Each Current In Worksheets
   With Current
    Range("E4:AS4,E6:AS6,E8:AS8").Select
    Selection.Style = "Percent"
    Selection.NumberFormat = "0.0%"
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "INDEX"
    Range("A1").Select
    With Selection.Font
        .Name = "Calibri"
        .Size = 6
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
End With
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="INDEX!A1", TextToDisplay:="INDEX"

Next
End Sub
 
Upvote 0
Welcome to the Board!

One way is to activate each sheet as you loop through them, i.e. after your "For..." statment, add this line:
Code:
For Each Current In Worksheets
    Current.Activate
    ...
 
Upvote 0
Or do it without selecting the sheets:
Code:
Sub LoopThroughSheets()
    Dim wks         As Worksheet
 
    For Each wks In Worksheets
        With wks.Range("E4:AS4,E6:AS6,E8:AS8")
            .Style = "Percent"
            .NumberFormat = "0.0%"
 
            With wks.Range("A1")
                .Value = "INDEX"
                wks.Hyperlinks.Add Anchor:=.Cells, Address:="", SubAddress:="INDEX!A1", TextToDisplay:="INDEX"
                With .Font
                    .Name = "Calibri"
                    .Size = 6
                    .ThemeColor = xlThemeColorLight1
                    .TintAndShade = 0
                    .ThemeFont = xlThemeFontMinor
                End With
            End With
        End With
    Next wks
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,572
Members
452,927
Latest member
whitfieldcraig

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