Running the same code for multiple worksheets/series automatically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yalebulldog05
    New Member
    • Feb 2007
    • 1

    Running the same code for multiple worksheets/series automatically

    Watsup everyone,
    I'm having 2 problems, both dealing with similar themes of having code work for all possible series/worksheets, so i'm wondering if there's a macro that deals with this:
    1) I'd like to have the code I'm writing run for every worksheet in the workbook without having to manually change the sheetname "SPECIFIC WORKSHEET" each time or copy the same code 8 times with different sheetnames. how can i do this?
    code example:
    Charts.Add
    ActiveChart.Cha rtType = xlLineMarkers
    ActiveChart.Set SourceData Source:=Sheets( "SPECIFIC WORKSHEET").Ran ge("A1:L7"), _
    PlotBy:=xlRows
    ActiveChart.Loc ation Where:=xlLocati onAsNewSheet, Name:="SPECIFIC WORKSHEET Chart"
    With ActiveChart
    .HasTitle = True
    .ChartTitle.Cha racters.Text = "BLAH BLAH SPECIFIC WORKSHEET"
    End With

    2) I'd like the changes within a chart I'm building in vba to apply to all of the series, not just series1 or series2, etc.
    code example:
    ActiveChart.Ser iesCollection(1 ).Select
    With Selection.Borde r
    .Weight = xlThick
    End With

    or

    ActiveChart.Ser iesCollection(2 ).Select
    With Selection.Borde r
    .Weight = xlThick
    End With
    .....etc.
    Any help would be much appreciated. I am new to vba, so if these answers are trivial I apologize.
    thanks,
    dave
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I think that the answer is probably very similar for all of your questions - use the For Each...Next construct to loop through the appropriate collection. For example, this very simple code lists the names of all the worksheets in the current workbook.
    Code:
    Sub ListSheets()
      Dim s As Worksheet
      [B]For Each s In Worksheets[/B]
        Debug.Print s.Name
      [B]Next[/B]
    End Sub

    Comment

    Working...