user3644671
user3644671

Reputation: 11

X axis values Visual Basic

I found this code in StackOverflow to create dynamic excel charts

ThisWorkbook.Sheets("Sheet1").ChartObjects(1).Chart.SetSourceData _
         Source:=ThisWorkbook.Sheets("Sheet1").Range("MyRange")

It works fine, the only issue is the x-axis values are 1,2,3,4,5 etc..

I would like to select another range for the x-values. How to achieve it?

Upvotes: 1

Views: 186

Answers (1)

Joe Farrell
Joe Farrell

Reputation: 3542

You can use Chart.Axes(xlCategory) to get a reference to the x-axis (see documentation for this method here), then set whatever properties you want on that object. For instance:

Dim theChart As Chart
Set theChart = ThisWorkbook.Sheets("Sheet1").ChartObjects(1).Chart

theChart.Axes(xlCategory).MinimumScale = 0.1
theChart.Axes(xlCategory).MaximumScale = 1.1
theChart.Axes(xlCategory).MajorUnit = 0.25
theChart.Axes(xlCategory).MinorUnit = 0.05

This page shows the properties that are available on an Axis object.

Upvotes: 1

Related Questions