Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Changing X-axis ranges in Excel 95

Posted on 2000-02-28
Medium Priority
Last Modified: 2006-11-17
I need to be able to automatically change the X-axis range for several Excel charts.  I typically plot time (in month intervals) on the X-axis and I need the charts to cover the last 24 months.  I am currently using the scatter plot format.  I would like to use the NOW() function to generate the current date in one of the spreadsheet cells and back calculate the starting date of the required 24 month period.

So my question is...  How can I automatically change the minimum and maximum scaling factors to be set to these values?  I would perferred a simple Excel macro since I am not very famaliar with VB.  Also, the sample code (macro or VB) would be greatly appreciated.
Question by:dwcurths
  • 2
  • 2
LVL 17

Accepted Solution

calacuccia earned 300 total points
ID: 2569639
Hi dwcurths,

could you specify in which format the X-values are. You are talking about monthly intervals, and about the now() function.
The now() function will return the actual date and time, but I imagine that the x-values are somthing like January, 1999 .... to February, 2000.

Also, will the chart cover all the data on the sheet and do you want a macro for a custom zoom on the last 24 months by changing the min and max scale tabs of the x-axis ?

In the latter case,and if you really use time,  you couild do it by using this little macro, which will set the minimum and maximum scale for the chart called "Chart1" corresponding two the last two years or last 24 months.

Sub FocusOn24Months()
Dim a, b
a = Now
b = DateSerial(Year(Now) - 2, Month(Now), Day(Now))
With Charts("Chart1").Axes(xlCategory)
        .MinimumScale = b
        .MaximumScale = a
        .MinorUnitIsAuto = True
        .MajorUnitIsAuto = True
        .Crosses = xlAutomatic
        .ReversePlotOrder = False
        .ScaleType = xlLinear
End With
End Sub

I suppose you know how to enter a macro, if not, hit Alt+F11 (this will open the Visual Basic Editor, look for the VBAProject(MyFile) on the left in a kinda explorer tree (where MyFile is the name of your Excel workbook), right-click on the project, select Insert/Module, and paste above code in the right window (module window).
Now exit the VBA Editor (return to Excel) and run the macro by hitting Alt+F8 and selecting the FocusOn24Month macro.

It could be not what you need though, so please specify to my questions if you want something else.

Good Luck


Author Comment

ID: 2574556
To: calacuccia

Must apologize for any problems with question.  This first time I have posted a question, so I really don't know how this site fundamentally works.

Since I work for the "Federal Government", they get pretty upset if I use my work related equipment for internet access.....  So I just now had a chance to review your comment using my personal account.  

To answer your question, the date format should be/can be converted to serial number format (i.e. 36586.5 equals 03/01/2000 12:00), however usually the X-range cell format is MMM-YYYY.  

I'll try your proposed macro tomorrow (03/02/2000) at work.  It appears that it should work (or that I should be able to get it work) with methods that you indicated.  

If it works, then I'll close the question to your response.

Thank you (in advance)

Author Comment

ID: 2578340
To:  calacuccia

Macro worked just fine.  I modified the code and added  a seperate spreadsheet titled "Chart Axis Controls".  On this spreadsheet, I allowed the user to over ride the default span of 24 months by enter desired start and stop dates in selected cells.  Then in other cells, I used the worksheet "if" function to determine if user had entered values other than default period.  The macro then went to the "if" cells and set parameters "a" and "b" to the values.  Wrote some more code which automatically reset the chart to the default values and left user back at "Chart Axis Controls".  Everything works great.  Thanks for the ideas.
LVL 17

Expert Comment

ID: 2578488
Hi dwcurths,

Glad I could help you. From the modifications you made I see you have the right mind about solving problems, you prefer to know how and adjusting yourself so that you can do it yourself the next time. Sometimes, people on this site just ask for a pack & go solution and don't bother bout understanding what made it work. Those people often get stuck is there's a tiny error in the code.

Another tip, if you want to make macros in the future and have no clue which commands, objects... to use, a godd start is often to do manually what you want the macro to do automatically later while recording a macro.

Excel will write large parts of code, of which a lot has to be adjusted to specific needs, but often the basic and important macro commands are generated.

To record a macro, go to Tools/Macro/Record New Macro, enter some info in following dialog boxes, click OK, start doing manually what you want to be generated, and click the 'Stop button' when finished (or go to Tools/Macro/Stop Recording).

Then hit Alt+F11 to enter the VBA editor to look at generated code and start fixing it for your needs. I've learned tons of things this way.



Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

578 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