Changing X-axis ranges in Excel 95

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.
dwcurthsAsked:
Who is Participating?
 
calacucciaConnect With a Mentor Commented:
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

Calacuccia
0
 
dwcurthsAuthor Commented:
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)
0
 
dwcurthsAuthor Commented:
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.
0
 
calacucciaCommented:
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.

Cheers

Calacuccia
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.