+ Reply to Thread
Results 1 to 4 of 4

How do you add axis labels and titles to a Chart via VBA?

  1. #1
    Registered User
    Join Date
    07-29-2013
    Location
    California, United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    How do you add axis labels and titles to a Chart via VBA?

    I want to use the name that is on the sheet as a title. Same thing with the axis. I have no idea how to even label the axis.

    Please Login or Register  to view this content.
    I get an error with this. When I change it to .ChartTitle.Text = "Apples" , it works fine. I was hoping it would work because my dataTitle was declared as a String.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How do you add axis labels and titles to a Chart via VBA?

    I'm not sure why it errors on your chart title. The code you have should work. What text is in sheet2.Range("B1") ?

    This should add the X and Y axis labels.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    07-29-2013
    Location
    California, United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: How do you add axis labels and titles to a Chart via VBA?

    Thanks AlphaFrog. For some odd reason, I found out that the code works when I am not active in sheet2. I have to be in another tab. Otherwise, if I selected a cell from sheet2 and then run the macro, I get this error:

    Run-time error '-2147024809 (80070057)':

    This object has no title.


    Do you have any ideas of why this may be?

    Also, thanks for the code regarding the axes labels.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: How do you add axis labels and titles to a Chart via VBA?

    Sheet2 is a reserved keyword in VBA.
    Try using a different variable name other than sheet2 (try sh2).

    Alternatively, you don't really need the worksheet variable.

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 08-28-2014 at 06:05 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to Center Titles and Horizontal Axis Labels on All Charts in Sheet
    By avr5iron in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2014, 09:46 AM
  2. Excel dropping titles, axis labels when printing chart
    By uptix in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 10-29-2013, 04:20 PM
  3. Add labels/titles to vertical axis of scatter chart
    By verwater in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 05-23-2013, 07:19 PM
  4. Formulas in Chart Titles & Axis Titles
    By Bobbbo in forum Excel General
    Replies: 1
    Last Post: 05-20-2010, 02:02 PM
  5. chart axis Titles
    By seedbankstudy in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 10-05-2005, 01:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1