CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2003
    Posts
    4

    How to change an excel chart axis text orientation in VB.Net?

    Hi, I am developing a VB.Net application that will populate data from database to excel and create an excel chart base on the data. I already got excel data and chart, but I need to change one axis text orientation to 45 degree. How can I do that in VB code? Thanks for the help!
    Last edited by mrtxu; September 22nd, 2003 at 10:59 AM.

  2. #2
    Join Date
    Dec 2002
    Posts
    305
    With Range("G1")
    .Orientation = 45
    End With

  3. #3
    Join Date
    Sep 2003
    Posts
    4
    Thanks Gizmo001! But what I need is to change the axis lable text orientation on the chart to 45 degree, not the text orientation in excel. Any idea about that?

  4. #4
    Join Date
    Dec 2002
    Posts
    305
    Try this (for x-axis) with appropriate changes to the first line (depending on your Excel chart name):

    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.Axes(xlCategory).AxisTitle
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .ReadingOrder = xlContext
    .Orientation = 45
    End With

    and for y-axis

    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.Axes(xlValue).AxisTitle
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .ReadingOrder = xlContext
    .Orientation = 45
    End With

  5. #5
    Join Date
    Sep 2003
    Posts
    4
    Where is With Statement? or With what? chart? sheet? or range?

    Thanks!

  6. #6
    Join Date
    Dec 2002
    Posts
    305
    Sorry, with statement should begin with the Activechart line. For example if your active chart is named xlchart

    With xlchart.Axes(xlvalue).axistitle
    .orientation =45
    end with


    By the way, go to Excel, turn macro tool on, and record the macro as you rotate the axis label on the chart. After rotation is complete, stop recording macro. Then edit macro to see the visual basic code created. You can bring this code into VB.Net with minor variations with respect to the object names: Excel sheet name, chart name, etc. For any Excel command/function that you want to implement in VB, this is a good way to find the syntax.

    Good luck.

  7. #7
    Join Date
    Sep 2003
    Posts
    4
    Sorry to bother again Gizmo001! My VB.Net won't recognize Axes(xlCategory) saying xlCategory not defined. And also ActiveChart, ActiveSheet, etc. I am thinking maybe I need to import some special classes. But I can get regular chart on the sheet already. I am so confused. Since the X axis label is now showing horizontally its not showing all on the chart when taking larger numbers of rows from excel. That's the reason I want to change its orientation to 45 degree or 90 degree. Although I can manually change that in excel I still think there should be a way in VB.Net coding.

  8. #8
    Join Date
    Dec 2002
    Posts
    305
    Sorry, I tried to check it out, and it didn't work either. It used to work with earlier verisions of Excel reference library (8.0 Object library), but with 10.0, it does not work. I have not been able to figure out the equivalent syntax for the new Excel library.

  9. #9
    Join Date
    Nov 2012
    Posts
    1

    Re: How to change an excel chart axis alignmen in VB.Net?

    You need to reference the Microsoft.Office.Core namespace, ie.
    .ReadingOrder = Microsoft.Office.Core.XlReadingOrder.xlContext

  10. #10
    Join Date
    Jan 2021
    Posts
    8

    Re: How to change an excel chart axis alignmen in VB.Net?

    Change category label text on the worksheet

    On the worksheet, click the cell that contains the name of the label that you want to change.

    Type the new name, and then press ENTER.

    Note Changes that you make on the worksheet are automatically updated in the chart.

    Change the label text in the chart

    In the chart, click the horizontal axis, or do the following to select the axis from a list of chart elements:

    Click anywhere in the chart.

    This displays the Chart Tools, adding the Design and Format tabs.

    On the Format tab, in the Current Selection group, click the arrow in the Chart Elements box, and then click the horizontal (category) axis.

    On the Design tab, in the Data group, click Select Data.

    In the Select Data Source dialog box, under Horizontal (Categories) Axis Labels, click Edit.

    In the Axis label range box, do one of the following:

    Specify the worksheet range that you want to use as category axis labels.

    Type the labels that you want to use, separated by commas — for example, Division A, Division B, Division C.

    Click OK.

    I hope this information will be helpful!
    Tim Paine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured