Forum Discussion

Elbucki's avatar
Elbucki
Copper Contributor
Feb 25, 2025
Solved

VBA charting with two rows of labels

Hello, I have old VBA code that used to make graphs from this data look like this.   .ChartWizard Source:=Worksheets(strCalcSht).Range(Cells(grpRow - 1, colsStats), Cells(grpRow + num...
  • HansVogelaar's avatar
    HansVogelaar
    Feb 27, 2025

    Thanks! Here is a different approach:

    Sub CreateChartCorrect()
        Dim strResults As String
        Dim grpRow As Integer
        Dim colsStats As Integer
        Dim numGroupsThisSheet As Integer
        Dim colsStatsEnd As Integer
        Dim ws As Worksheet
        Dim ch As Chart
    
        strResults = "Results"
        grpRow = 2
        colsStats = 2 ' 1
        numGroupsThisSheet = 5
        colsStatsEnd = 5
    
        Set ws = Worksheets(strResults)
        Set ch = ws.ChartObjects.Add(100, 300, 500, 700).Chart
    
        With ch
            .SetSourceData Source:=ws.Range(ws.Cells(grpRow - 1, colsStats), ws.Cells(grpRow + numGroupsThisSheet, colsStatsEnd))
    
            .ChartType = xlLineMarkers
            .PlotBy = xlColumns
    
            ' I will leave it to you to generalize this
            .FullSeriesCollection(1).XValues = ws.Range(ws.Cells(grpRow + 1, colsStats - 1), ws.Cells(grpRow + numGroupsThisSheet, colsStats - 1))
    
            .HasLegend = True
    
            With .Axes(xlCategory)
                .HasTitle = True
                .AxisTitle.Caption = "Core Crimp Ht. (mm)"
            End With
    
            With .Axes(xlValue)
                .HasTitle = True
                .AxisTitle.Caption = "Resistance (m" & ChrW(937) & ")"
            End With
    
             '/// Format title.
             .HasTitle = True
             .ChartTitle.Caption = "Test Results - Current Cycling" & Chr(10) & "Average Current = " & Str(Round(currentAvg, 1)) & " Amps"
             With .ChartTitle.Font
                .Name = "Test"
                .FontStyle = "Bold"
                .Size = 12
                .Underline = xlUnderlineStyleSingle
             End With
        End With
    End Sub

     

Resources