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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.