cellMatrix.net

Spreadsheet Modeling and Related Topics

Create X Axis Labels from Array

Recently I was working on a chart that required text entries in the Category (X) axis label input as opposed to a worksheet range as the source. In other words, in the chart source data dialog box the Category (X) axis label input normally looks like this:

image

but I needed it to look like this:

image

Rather than inputting all of the the data by hand I had hoped that the process could be automated. My thought was to use a macro to load the data from a worksheet range into an array and then load the array as Category (X) axis values. Below is that macro:

Sub Create_Axis_Values_From_Array()
    Dim X As Long
    Dim R As Range
    Dim N As String
    Set R = ActiveSheet.Range("B2:B13")
    N = "={"
    For X = 1 To R.Count
        N = N & """" & R(X).Value & ""","
    Next
    N = Left(N, Len(N) - 1) & "}"
    ActiveChart.SeriesCollection(1).XValues = N
End Sub

The same macro works to create a named range by changing the second-to-last statement in the procedure.

Sub Create_Named_Range_From_Array()
    Dim X As Long
    Dim R As Range
    Dim N As String
    Set R = ActiveSheet.Range("B2:B13")
    N = "={"
    For X = 1 To R.Count
        N = N & """" & R(X).Value & ""","
    Next
    N = Left(N, Len(N) - 1) & "}"
    Names.Add Name:="Months", RefersTo:=N
End Sub

A special thanks to Rick Rothstein - Excel MVP - who helped me set up the array.


Posted on Monday, July 13, 2009 | Comments (0) | Permalink

Comment Entry Form

User Information

Name: (Required)

E-Mail Address: (Optional)

Location: (Optional)

Web Site Address: (Optional)

Comment Formatting Reference
Add VBA Code:
<pre>some code here</pre>
Linking:
[url]http://www.example.com/[/url]
[url=http://www.example.com/]my site[/url]
Text Formatting
[size=4]Some larger text[/size]
[color=green]Some green text[/color]
[b]Some bold text[/b]
[i]Some italic text[/i]
Enter Comment Below

Personalization Options
Remember my personal information
Notify me of follow-up comments?

Next entry: Chart vs. Table Dilemma

Previous entry: Calculating Consistent Rate Spreads

<< Back to main

Statistics

  • Total Entries - 136
  • Current Viewers - 23

Categories

Entries by Day

Sep - 2010
S M T W T F S
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2

Recent Comments

Syndicate