+ Reply to Thread
Results 1 to 4 of 4

Same chart, multiple worksheets, dynamic data ranges

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    2

    Same chart, multiple worksheets, dynamic data ranges

    Hi Everyone,

    I have an Excel workbook of 30 worksheets that I would like to put the same type of chart on. However, the number of columns in each of the worksheet differs. Do you know a way I can use a VBA macro to create the same type of chart, for each of the 30 sheets, while allowing the number of columns to differ in each of the worksheets? I have the VBA code to do the chart already and it does work, given that my data dimensions are static. Unfortunately, my data is not that way though.

    Thanks,

    tiredone

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Same chart, multiple worksheets, dynamic data ranges

    Post your sample workbook with 2-3 pages in it and the code you're using currently, I'll see if I can adapt it for you to be dynamic.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    05-08-2015
    Location
    United States
    MS-Off Ver
    2010
    Posts
    2

    Re: Same chart, multiple worksheets, dynamic data ranges

    sample code:

    Sub GRAPHS()
    '
    ' GRAPHS Macro
    '
    
    '
    
    Dim J As Integer
    
    
    For J = 2 To Sheets.Count ' from sheet 2 to last sheet
    Sheets(J).Activate ' make the sheet active
    
    
    With ActiveSheet
    sWord = ActiveSheet.Name
        Range("A1:D4").Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.ChartType = xlCylinderColClustered
        ActiveChart.SetSourceData Source:=Sheets(sWord).Range("$A$1:$D$4")
        ActiveChart.ApplyLayout (1)
        ActiveChart.ChartTitle.Select
        ActiveChart.ChartTitle.Text = "Colors by Year"
        Selection.Format.TextFrame2.TextRange.Characters.Text = "Colors by Year"
        With Selection.Format.TextFrame2.TextRange.Characters(1, 14).ParagraphFormat
            .TextDirection = msoTextDirectionLeftToRight
            .Alignment = msoAlignCenter
        End With
        With Selection.Format.TextFrame2.TextRange.Characters(1, 6).Font
            .BaselineOffset = 0
            .Bold = msoTrue
            .NameComplexScript = "+mn-cs"
            .NameFarEast = "+mn-ea"
            .Fill.Visible = msoTrue
            .Fill.ForeColor.RGB = RGB(0, 0, 0)
            .Fill.Transparency = 0
            .Fill.Solid
            .Size = 18
            .Italic = msoFalse
            .Kerning = 12
            .Name = "+mn-lt"
            .UnderlineStyle = msoNoUnderline
            .Strike = msoNoStrike
        End With
        With Selection.Format.TextFrame2.TextRange.Characters(7, 8).Font
            .BaselineOffset = 0
            .Bold = msoTrue
            .NameComplexScript = "+mn-cs"
            .NameFarEast = "+mn-ea"
            .Fill.Visible = msoTrue
            .Fill.ForeColor.RGB = RGB(0, 0, 0)
            .Fill.Transparency = 0
            .Fill.Solid
            .Size = 18
            .Italic = msoFalse
            .Kerning = 12
            .Name = "+mn-lt"
            .UnderlineStyle = msoNoUnderline
            .Strike = msoNoStrike
        End With
        ActiveChart.PlotArea.Select
        ActiveChart.SetElement (msoElementPrimaryValueAxisTitleVertical)
        ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "%"
        Selection.Format.TextFrame2.TextRange.Characters.Text = "%"
        With Selection.Format.TextFrame2.TextRange.Characters(1, 1).ParagraphFormat
            .TextDirection = msoTextDirectionLeftToRight
            .Alignment = msoAlignCenter
        End With
        With Selection.Format.TextFrame2.TextRange.Characters(1, 1).Font
            .BaselineOffset = 0
            .Bold = msoTrue
            .NameComplexScript = "+mn-cs"
            .NameFarEast = "+mn-ea"
            .Fill.Visible = msoTrue
            .Fill.ForeColor.RGB = RGB(0, 0, 0)
            .Fill.Transparency = 0
            .Fill.Solid
            .Size = 10
            .Italic = msoFalse
            .Kerning = 12
            .Name = "+mn-lt"
            .UnderlineStyle = msoNoUnderline
            .Strike = msoNoStrike
        End With
        End With
        Next
    End Sub
    sample workbook, worksheet 1 'GROUP 1'

    YEAR RED GREEN YELLOW
    2012 30 30 40
    2013 40 30 20
    2014 30 35 35


    WORKSHEET2 'GROUP 2'
    YEAR RED GREEN YELLOW ORANGE
    2012 30 30 30 10
    2013 20 20 30 30
    2014 30 35 35 0


    WORKSHEET 3 'GROUP 3'

    YEAR RED GREEN YELLOW ORANGE PURPLE
    2012 0 0 30 20 50
    2013 20 20 30 30 0
    2014 20 35 10 0 35
    Attached Files Attached Files
    Last edited by JBeaucaire; 05-16-2015 at 12:08 AM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Same chart, multiple worksheets, dynamic data ranges

    Like so:
    Sub GRAPHS()
    Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
        With ws
            .Activate
            sWord = .Name
            Range("A1").CurrentRegion.Select
            ActiveSheet.Shapes.AddChart.Select
            ActiveChart.ChartType = xlCylinderColClustered
        '    ActiveChart.SetSourceData Source:=Sheets(sWord).Range("$A$1:$D$4")
            ActiveChart.SetSourceData Source:=Sheets(sWord).Range(Range("$A$1").CurrentRegion.Address)
            ActiveChart.ApplyLayout (1)
            ActiveChart.ChartTitle.Select
            ActiveChart.ChartTitle.Text = "Colors by Year"
            Selection.Format.TextFrame2.TextRange.Characters.Text = "Colors by Year"
            With Selection.Format.TextFrame2.TextRange.Characters(1, 14).ParagraphFormat
                .TextDirection = msoTextDirectionLeftToRight
                .Alignment = msoAlignCenter
            End With
            With Selection.Format.TextFrame2.TextRange.Characters(1, 6).Font
                .BaselineOffset = 0
                .Bold = msoTrue
                .NameComplexScript = "+mn-cs"
                .NameFarEast = "+mn-ea"
                .Fill.Visible = msoTrue
                .Fill.ForeColor.RGB = RGB(0, 0, 0)
                .Fill.Transparency = 0
                .Fill.Solid
                .Size = 18
                .Italic = msoFalse
                .Kerning = 12
                .Name = "+mn-lt"
                .UnderlineStyle = msoNoUnderline
                .Strike = msoNoStrike
            End With
            With Selection.Format.TextFrame2.TextRange.Characters(7, 8).Font
                .BaselineOffset = 0
                .Bold = msoTrue
                .NameComplexScript = "+mn-cs"
                .NameFarEast = "+mn-ea"
                .Fill.Visible = msoTrue
                .Fill.ForeColor.RGB = RGB(0, 0, 0)
                .Fill.Transparency = 0
                .Fill.Solid
                .Size = 18
                .Italic = msoFalse
                .Kerning = 12
                .Name = "+mn-lt"
                .UnderlineStyle = msoNoUnderline
                .Strike = msoNoStrike
            End With
            ActiveChart.PlotArea.Select
            ActiveChart.SetElement (msoElementPrimaryValueAxisTitleVertical)
            ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "%"
            Selection.Format.TextFrame2.TextRange.Characters.Text = "%"
            With Selection.Format.TextFrame2.TextRange.Characters(1, 1).ParagraphFormat
                .TextDirection = msoTextDirectionLeftToRight
                .Alignment = msoAlignCenter
            End With
            With Selection.Format.TextFrame2.TextRange.Characters(1, 1).Font
                .BaselineOffset = 0
                .Bold = msoTrue
                .NameComplexScript = "+mn-cs"
                .NameFarEast = "+mn-ea"
                .Fill.Visible = msoTrue
                .Fill.ForeColor.RGB = RGB(0, 0, 0)
                .Fill.Transparency = 0
                .Fill.Solid
                .Size = 10
                .Italic = msoFalse
                .Kerning = 12
                .Name = "+mn-lt"
                .UnderlineStyle = msoNoUnderline
                .Strike = msoNoStrike
            End With
        End With
    Next ws
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 08-05-2013, 11:34 AM
  2. Replies: 2
    Last Post: 07-17-2012, 09:18 PM
  3. Replies: 0
    Last Post: 07-06-2006, 12:00 AM
  4. Looping through dynamic ranges from multiple worksheets
    By TechWrangler in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-08-2006, 03:20 PM
  5. [SOLVED] copy dynamic ranges in multiple workbooks based on a data in 1 col
    By jbsand1001 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-18-2005, 03:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1