Help: Excel Macro Code

Hi All,

I have an Excel dataset and manually created a graph. Here are the steps I followed:

  1. Selected the first four columns in Excel.
  2. Created a 2D line graph using the chosen columns.
  3. Set the CastSpeed column as the secondary axis.

I’m seeking a macro code that generate this graph. Additionally, I’d like to use this code on different worksheets.

{I have tried everything i am not getting desired output using other macro codes}

Test.xlsx (83.7 KB)

Regards,
Vinit Mhatre

@Vinit_Mhatre

Try this

Sub 2DGraphTry()
    Dim chartObj As ChartObject
 
    Set chartObj = ThisWorkbook.Sheets("Sheet1").ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225)
    
    chartObj.Chart.ChartType = xlLine
    
    chartObj.Chart.SetSourceData Source:= ThisWorkbook.Sheets("Sheet1").Range("A:D")
    
    With chartObj.Chart.Axes(xlValue, xlSecondary)
        .HasTitle = True
        .AxisTitle.Text = "Secondary Axis Title" ' Customize the title as needed
    End With
End Sub

Modify as needed

Also just record if you need a macro and perform steps so that macro is auto created and can modify on top

Cheers

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.