X and Y-Axis Labels in Excel Using VB.NET

Hi All,

I’m trying to automate plotting of an excel graph using VB.NET. So far everything seems smooth except for placing the x and y-axis labels. Here’s the relevant excerpt from the code I’m using:

Imports office = Microsoft.Office.Interop.Excel
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices

. . .

Dim ws As Excel.Worksheet
        Dim chartRange As office.Range
        Dim charts As Office.ChartObjects
        Dim chartObj As Office.ChartObject
        Dim seriesCollection As Office.SeriesCollection
        Dim series As office.Series

       
        ws = objectWorkBook.ActiveSheet()

        charts = ws.ChartObjects()
        chartObj = charts.Add(450, 20, 280, 250)
        chartRange = ws.Range("E1..E20")
        chartObj.Chart.SetSourceData(chartRange)
        chartObj.Chart.ChartType = office.XlChartType.xlLineMarkers
        chartObj.Chart.HasTitle = True
        chartObj.Chart.ChartTitle.Text = "Title"
        chartObj.Chart.HasLegend = False

        seriesCollection = chartObj.Chart.SeriesCollection()
        series = seriesCollection.Item(seriesCollection.Count)

Up to here everything is fine until I try to have text labels along the x and y-axis. I’m trying this code and variations of it, judging from the earlier format of the functioning code, to no avail:

        chartObj.Chart.HasAxis = True
        axis = chartObj.Chart.Axes()
        axis.HasTitle = True
        axis.AxisTitle.Text = "X-Axis Text"

How can this problem be solved? Thanks in advance.
judicoAsked:
Who is Participating?
 
Neil FlemingConsultant and developerCommented:
I think you just need to specify the axis in each case.

See example below, or indeed set your "axis" object to .axes(xlvalue) etc..
With chartobj.chart

.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Caption = "thousand tonnes/year"
.Axes(xlValue).AxisTitle.Font.Bold = True
.Axes(xlValue).AxisTitle.Font.Size = 10
.Axes(xlValue).TickLabels.NumberFormat = "0"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxistTitle.Caption = "Year"
end With

Open in new window

0
 
judicoAuthor Commented:
How are xlValue and xlCategory declared?
0
 
Neil FlemingConsultant and developerCommented:
They are built-in Excel constants, with values as follows:

xlCategory:  1    --Axis displays categories.
xlSeriesAxis: 3   --Axis displays data series.
xlValue:        2   -- Axis displays values.

So if you are building the chart from somewhere which has no knowledge of the Excel constants, you can just use those values -- ie .axes(2) for value axis.

You can also access primary/secondary axes on charts with multiple value axes, eg as follows:

With xlChart.Chart.Axes(xlValue, xlPrimary)
.MinimumScaleIsAuto = True
.MaximumScaleIsAuto = True
.HasTitle = True
.AxisTitle.Text = "local prices"
End With

xlPrimary has value 1, xlSecondary has a value of 2
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
judicoAuthor Commented:
Still something's wrong. I'm using the first code you gave:

but there's still an error message appearing, reading:

"Public member 'AxisTitle' on type 'Axis' not found"

I see the graph with the x and y-axis labels but the above error message appers as well.
Dim xlValue As Integer
        xlValue = 2
        Dim xlCategory As Integer
        xlCategory = 1


        With chartObj.Chart
            .Axes(xlValue).HasTitle = True
            .Axes(xlValue).AxisTitle.Caption = "thousand tonnes/year"
            .Axes(xlValue).AxisTitle.Font.Bold = True
            .Axes(xlValue).AxisTitle.Font.Size = 10
            .Axes(xlValue).TickLabels.NumberFormat = "0"
            .Axes(xlCategory).HasTitle = True
            .Axes(xlCategory).AxistTitle.Caption = "Year"
        End With

Open in new window

0
 
Neil FlemingConsultant and developerCommented:
may be my typo?

 I see I wrote "AxistTitle" instead of "AxisTitle" on what is now your line 14... there is an extra "t" in there, which is probably what is triggering the eror. Sorry.
0
 
judicoAuthor Commented:
Works great now. Indeed, that typo was the problem.

Now, may I ask you another question here to avoid opening a new instance? Of course, I'll award you more points for that.

I'm trying to overlay curves in a graph coming from data in two non-adjacent columns. I tried
chartRange = ws.Range("$E$1:$E$20", "$I$1:$I$20") which only gives me the second graph. If I type chartRange = ws.Range("$E$1:$E$20"; "$I$1:$I$20") or chartRange = ws.Range("$E$1:$E$20; $I$1:$I$20") or anything else I get an error message. There must be some specific way to define chartRange in such a case.
0
 
Neil FlemingConsultant and developerCommented:
You need to use the "Union" function to combine non-adjacent ranges.

From the excel help file:

Dim r1 As Range, r2 As Range, myMultiAreaRange As Range
Worksheets("sheet1").Activate
Set r1 = Range("A1:B2")
Set r2 = Range("C3:D4")
Set myMultiAreaRange = Union(r1, r2)
myMultiAreaRange.Select

Meanwhile does "chartrange" work at all? I always use SetSourceData.. as per the sample below (constants: xlColumns = 2, xlRows=1)



With xlChart.Chart
.ChartType = xlLineMarkers
.SetSourceData sourcerange, xlColumns
end With

Open in new window

0
 
judicoAuthor Commented:
Range and Union cannot work because:

'System.Data.Range' is not accessible in this context because it is 'Private'.

How do you make it public?
0
 
judicoAuthor Commented:
I resorted to a palliative solution. I made the two columns with data to be plotted adjacent to each other and added a prior empty column. So, we have an empty column H and two columns I and J filled with the data points.  Now the chartRange = ws.Range("H1..J20") renders the two curves properly overlaid. Unfortunately, the x-axis values are not the ones in column A but are just the consecutive numbers of the data points. Don't know how to place the actual x-axis numbers correctly but that's as much as a palliative solution can do, I guess.
0
 
judicoAuthor Commented:
Thanks a lot for your help, @NeilFleming1. I'll appreciate it if can also instruct me how to transform the Range and Union from Private to Public. All the best.
0
 
judicoAuthor Commented:
NeilFleming1, I'm sorry, I just saw I've awarded you 125 points while I wanted to give you more. How can I correct that?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.