Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA to find data type of chart axis

Posted on 2011-09-08
2
Medium Priority
?
721 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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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