Solved

Excel VBA to find data type of chart axis

Posted on 2011-09-08
2
662 Views
Last Modified: 2012-08-14
Hi Experts

I have some VBA and an Excel chart, I want some syntax so if the axis is a date type then it formats that date.

Currently I have

If Instr(selection.ticklabels.Numberformat,"yyyy") then
    'change the number format

this works fine , but if a users runs the code and has a different time local settings it may not work, is there some code like

If selection.ticklabel.numberformat  = xldate then
  'change the number format

Many thanks
0
Comment
Question by:MrDavidThorn
  • 2
2 Comments
 
LVL 17

Expert Comment

by:andrewssd3
ID: 36503142
I don't think you can do this a completely foolproof way, but I have used this in the past to check that the category axis is a date axis:

Sub Macro3()
    
    Dim s As Series
    With ActiveChart
        Set s = .SeriesCollection(1)
        With s
            If IsDate(CDate(.XValues(1))) Then
               MsgBox "It's a date axis"
            End If
        End With
    End With


End Sub

Open in new window


This obviously looks at the first series of the chart and checks that the first value is a date.  If you wanted to be very careful, you could also check some more of theXValues.  If your chart was quite complex and had some series plotted on secondary axes, you might have to check that the first series was relevant by checking which axis it was plotted on, but if you're incontrol of the chart and can guarantee its structure, this will be fine. You have to do the CDate because the XValues show up as numbers (e.g. 40544 = 1st Jan 2011), and you need to convert this to a date first.  There's obviously a small risk that the value really is a number in the 40,000's which is why I say it's hard to be totally sure.
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 36504984
Just realised revisiting this that the code will only work if the chart axis is date-based - you will get a run time error on the CDate if it isn't. This is better:

Sub Macro3()
    
    Dim s As Series
    Dim v As Variant
    With ActiveChart
        Set s = .SeriesCollection(1)
        With s
            On Error Resume Next
            v = CDate(.XValues(1))
            If Err.Number = 0 Then
               MsgBox "It's a date axis"
            End If
            On Error GoTo 0
        End With
    End With


End Sub

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question