Solved

Excel VBA to find data type of chart axis

Posted on 2011-09-08
2
632 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

861 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now