VBA code If statement to check if chart x-axis is formatted as Date

gmcgough600

New Member
Joined
Nov 21, 2017
Messages
33
Office Version
  1. 365
Hi, looking for some help writing some VBA code.

I need to write an If statement to check if the x-axis of a chart is formatted as a date (doesn't matter if its dd-mmm-yy or dd/mm/yyyy, mm/dd/yyyy etc) then run some other code. Here's what I've got so far:

VBA Code:
If worksheet_charts = 1 Then
    Dim sht As Worksheet
    Dim CurrentSheet As Worksheet
    Dim cht As ChartObject
    For Each sht In ActiveWorkbook.Worksheets
        For Each cht In sht.ChartObjects
        'Edit the x-axis
        With cht
            [B]'[help needed here] If 'chart has date x-axis' Then[/B]
                .Chart.Axes(xlCategory).MaximumScale = chart_end_date
                .Chart.Axes(xlCategory).MinimumScale = chart_start_date
            Else if
                'Do nothing
            End if
        End With
        Next cht
    Next sht
End If

Hope someone can help, thanks!
 
I just noticed you said value axis - why are you checking those for dates?
The dates are on the x-axis, is this not classed as a value axis? When I hover over it says "Horizontal (Value) Axis". Not completely clear on the terminology here as hovering over the y-axis says "Vertical (Value) Axis". How do you differentiate between x and y in the code?
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
The test on majorunitscale did not work for me on an xy scatter chart whose x axis was displaying tick labels as Dates. So I changed the test to:
If InStr(ax.TickLabels.NumberFormat, "yy") Then IsDateAxis = True
So far this has worked for the charts I have tested it on.
 
Upvote 0
Thanks RoryA, what's the easiest way to check this property? Probably very simple but I gave this a try and got stuck:

VBA Code:
        With cht
            With .Chart.Axes(xlCategory)
                If .MajorUnitScale = xlDays Then

I'm getting error "Run-time error '-2147467259 (80004005)':

This property is not used by value axes."

The chart I'm testing on has dates on the x-axis.
Your chart is an XY Scatter chart. Dates/times on the X-axis are plotted as values.

MajorUnitScale is only a property of a non-XY chart's X-axis (say, a line chart, not a scatter chart) if the axis scale shows dates.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top