Solved

Changing X-axis ranges in Excel 95

Posted on 2000-02-28
4
271 Views
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.
0
Comment
Question by:dwcurths
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
calacuccia earned 75 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

Calacuccia
0
 

Author Comment

by:dwcurths
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)
0
 

Author Comment

by:dwcurths
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.
0
 
LVL 17

Expert Comment

by:calacuccia
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.

Cheers

Calacuccia
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

My experience with Windows 10 over a one year period and suggestions for smooth operation
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

623 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