Wonderful functionality! Why are some still keeping the minimum auto adjust of the y axis at zero? Please could you help me on this as I feel like I’m so close!!! PrimaryOrSecondary = a text string of “Primary” or “Secondary” to indicate which axis to adjust. But I’m not aware of anything simple built into Excel. See below. For my case, I am automatically loading in data onto excel, and this data is translated into a couple of charts on another tab. Two dynamic ranges: The goal is to chart only the values from the current cell and below, so we need to define a chart range that does not include the whole of column K. In Excel 2003 click Insert – Name – Define. setLimits .ChartObjects(1).Chart, _ In the Sheet2 code module, I use the Worksheet_Activate event: Thank you so much for this; I am trying to improve my excel skills and this has helped tremendously. For example:=setChartAxis(“Sheet1″,”Chart 1″,”Max”,”X”,”Primary”,B5). and the corrections to this made in your post on Monday, July 25, 2016 at 11:58 am, the updated code still does not work correctly. Okay it seems for now that the problex is fixed. Ie chart is in its own tab, not on the same sheet as the function. Ideally they would both run when under worksheet_change or worksheet_calculate. This function is great. It does not crash anymore when you delete the module from the file. The horizontal axis is the .Axes(xlCategory) axis of an XY Scatter plot, and this is included in the example code. I am using the worksheet calculate, but I want to apply this formula for every chart in het worksheet. Has anybody else had this type of issue? BUT!!!! 2. rng_end_date_max Works perfectly. Hi Jesse – Check out the comment and response on the 5th and 11th April 2019. I’ve done a ton of searching on the web. When I protect the sheet, the code doesn’t fire and I get the following error: Method ‘MaximumScale’ of object ‘Axis’ failed. So based on the department input in the top of the sheet the values for the y axis populate via index match functions. Case “$AO$10” Select the Edit button and in the Axis label range select the range in the Store column: Figure 3. If I had the knowledge that you and many other here appear to have I think I might rule the world… untill then I, like many others have my own unique situation involving multiple charts… and of course the Unique piece: THe gridlines should never be more than 4 to a chart…. This only happens when I do the Full calculate on a sheet without a chart and doesn’t happen when I run a Full Calculate on a sheet with a chart. eg Column A to AA in Sheet 1 Is there any way to come around this? Using the series method seems to interrupt the code and caused other issues for me. This works for most charts. This site uses Akismet to reduce spam. any ideas? wSheetName.Protect Password:=”Secret”, UserInterFaceOnly:=True Highlighted VBA code is: cht.Axes(xlValue).MaximumScale = wks.Range(“$AH$16”).Value. Hello,I saw that as of November there was no work around for protecting sheets.. I might try something like this as a first cut: Sub ResetToEightIntervals1() Can you write formulas for the X and Y values you want to plot? (Again, similar to what I was looking for above). You use Worksheet_Change if the values in the cells are changed by the user entering a different value, or Worksheet_Calculate if the values are changed because the cell formulas compute different results because of changed precedents. It really helped me solving a big problem. I am writing the VBA to change the Y axis on a line chart So my question is: Is there a way, using VBA, to set the axis values that the vertical and horizontal axes cross equal to the cells in which I have the contribution margin and sales percent? Excel […], […] googletag.cmd.push(function() { googletag.display('div-gpt-ad-1486492108368-0'); }); Here a tutorial on my blog: Link Excel Chart Axis Scale to Values in Cells. It looks like it is all in there and looked at your set-up, but it doesn’t adjust the axis. Jon, it seems that it might not be related with the operating system of the Mac laptop that I tested the macro. The secondary axis min value is 33,201 and the max value is 42,201, those results are correct based on the data. I and other bloggers are happy to help with parts of a project (like we’re doing in this thread). Your solution worked and solves my immediate problem. Any thoughts why this is not working for me? Case “$AO$9” can you please guide me in the right direction. Excel Charts 101. Much appreciated. One item I noted is that there is case sensitivity like Max not max.But many thanks for making this UDF. It seems the code does not work when I protect a sheet. Thanks very much for your long post. In the example below, you can see that as soon as I add new data, the Excel Table expands to include this data as a part of the table (note that the border and formatting expand to include it in the table). Thanx so much for this post/blog…. I cannot get the chart to reformat to the Max value I have assigned. Here’s a question for you (many thanks for this article by the way, much appreciated): I am using the calculate code out of the comments to scale the axes of a scatter chart. or MinOrMax = a text string of “Min” or “Max” to select whether the function will control the Minimum or Maximum axis value. Now I’m facing some problems to set horizontal scale automaticaly scale for a scatter chart like this one: I know it has to do with chart type but I’m struggling to set min and max scale using calculated argument. Well done. I should have thought of it!! that’s such a simple and brilliant solution!! Repeat the formula, then change the chart name in the second argument. Thank you very much! The debugger always show the following line to be faulty: cht.Axes(xlCategory).MajorUnit = wks.Range(“$P$5”). User Defined Functions (UDFs for short) are just like normal Excel functions, such as VLOOKUP or SUM, but they have been created using VBA. That is where you will want to create a chart with a dynamic range. Chart22 (Chart Projection) – A Chart tab. We all know that Chart Data Labels help us highlight important data points. But now I’m seeing some of my other graphs are auto adjusting and they dont start at zero. Sorry, your blog cannot share posts by email. The summary page has the chart and the cell in which you enter a date? I tested it on 3 separated PC’s with my documents and the example you provide here, tested with Excel 2016 and 2019. Then try the code. Unfortunately I didn’t notice. I have tried the worksheet change option to create a common axis based on min/max values across three pivot charts. In regard to getting the axis to cross, what chart type are you using? That calculation needs to be fixed. Second, you can’t set the axis maximum to a value that’s less than the minimum, if the minimum is not automatic. I get some odd behavior in when the macro chooses to run. I have made sure to select a chart. Then you might get some reasonable autoscale axis limits. I am quite unfamiliar with VBA, so I do not know how to link these values. Select Case Target.Address, Case “$G$2” Could anyone please adjust the code or tell me why it limits the Undos? I’d really appreciate any help you can offer! How to change the x axis to Store values Dim Charts_names As Variant I have another post about VBA code for charts, which can be found here: https://exceloffthegrid.com/vba-code-for-charts-and-graphs-in-excel/, The macro recorder is always a reasonable option to find the end of the code. Interestingly, it has worked a few times but most of the time it doesn’t I’m not sure what changed in those instances. Data plots and updates just fine, I just cannot figure out how to get the limits to AUTOMATICALLY update after the value changes. Hi, I need help to change the normal axis to a function. Yes and yes. Below you’ll find the result of that tinkering; a formula which exists on the worksheet to control the min and max values of a chart axis. None of the lines of the code are highlighted after the error. End With For i = LBound(Charts_names) To UBound(Charts_names), Set cht = wks.ChartObjects(Chart_names(i)).Chart, If wks.Range(“y_max”).Value cht.Axes(xlValue).MaximumScale Then Immensely useful. I’d like the scale to show the exact amounts of the customers we started with and the number we’re aiming for, but the numbers get rounded on the chart e.g. So I recommend removing that line of code (which I will also do in my example file). .MinimumScale = [MIN_Y_RISK_TE1].Value thanks Jon for starting this Blog! Follow these instructions if the Developer Tab is not visible, Power Query – Different country date formats, VBA Code to insert, move, delete and control pictures, VBA code to copy, move, delete and manage files, Combining multiple tables in a PivotTable, https://exceloffthegrid.com/vba-code-for-charts-and-graphs-in-excel/, https://peltiertech.com/link-excel-chart-axis-scale-to-values-in-cells/, https://exceloffthegrid.com/vba-code-worksheet-protection/, https://www.youtube.com/channel/UCTx8QvRuF3BXTe5OIaVF9OA, Updates automatically whenever data changes, Does not require user interaction – i.e. So the primary and secondary axes use the same scales? I have 2 dropdown lists in a cell (via validation) in which a user can select a start & end date. Nevermind. OLE Automation I wish there was one too. Is this also controllable by UDF? Select cells A1:B4. I wrote about this long ago in Stock Charts in Excel 2007. These values are pulled from a pivot table and then I use rounding formulas for the minimums and maximums for the data.The formula for the maximum and minimum values for the y axis are as follows =round(max(range),-2) and round(min(range),-2). But there are other different ways which should work, but are less transferable – check out this post:https://peltiertech.com/link-excel-chart-axis-scale-to-values-in-cells/, I’m pretty sure you’ll need to set protection to the user interface only – which is a secret excel setting. The following works for changing one chart at a time: The following doesn’t work, although I have used it to change the title and other properties of all charts within the workbook: I’ve tried: adding .Chart as you did with Doug’s code, “With” statements, varaible substitution. Really appreciate the quick response and the help. Workbook1 contains Charts with the code. But for a big project, you probably need to find someone who can help on a professional basis. the x-axis will just be dates so there’s no need for me to change those and I would prefer that Excel automatically graphs them as they are doing now. The code should be on the workbook that is being changed. Recommends going to 64 bit version, however other of my add-ins are only available as 32 bit. But contribution margin matrices have quadrants that are divided up by a horizontal and a vertical line intersecting the plane. Is there a way to have the lowest axis number 0 and the highest based on the value (in this example 87,500) and all in between should be equally divided between 0 and 87,500. The code to lock/unlock the sheet is doing it for all sheets in the workbook. End Sub. Your best bet is to add them manually and forget them, and just let the major unit you set control the gridlines. Hi. End If Can you advise. You made me realise that i could set the primary axis’ max scale to auto, then set the secondary axis’ max scale to match the primary’s,then make it negative. I have my chart set up displaying years and quarters. If you are using 2007 version of excel or above then using a data table instead of a normal rangeis the best way. Just to help anybody who is reading this and experiencing the same problem. It won’t let me change the chart type of the line charts on the secondary series if that makes sense. Any non-numeric value entered as the Value (or in the cell reference linked to the value) will cause the chart to be reset to automatic. https://docs.google.com/file/d/0BzYdRWk3IduDemVzZVRmTFhBTjQ/. Is it possible to retreive the automatically adjusted MaximumScale from one chart and use it as a fixed value in another chart? Its a great code you have here and it works perfectly, thank you, but I am now using it in a program i have made as part of my thesis. If the cells with the axis scale parameters are changed by the user typing in new values, Worksheet_Change will fire, but Worksheet_Calculate may not. Nor can you set the axis minimum to a value that’s greater than the maximum, if the maximum is not automatic. I often use it in conjunction with Calculate Nice Axis Scales in Your Excel Worksheet. Ask the ‘Excel Ninja’ in your office. If wks.Range(“$B$24”).Value = Chtob.Axes(xlValue).MaximumScale Then In case it’s helpful for anyone else out there, I think I solved my problem using the following approach. If the code is within you Personal Macrobook (or somewhere similar), then it will not work on another PC, they will have to set-up the same formula. For chart 2, the names will end in _2, etc. If there are only two charts, you could do. For that line of code it means either a chart is not selected, or the chart somehow has no primary X axis. Eg sheet 1 has a chart with min 10000 and max 15000 sheet 2 has a chart … As chart values can already be linked to cells, it would be straight forward to update dynamically. Thanks again for great tips! Thank you for any help. Arrange data for charts. The Y-Axis of these charts would be a numerical values (different for each chart) and the X-Axis would contain dates and show a three week window (starting last Monday). i.e. Help anyone? End With When I had a workbook open that was using this VBA code, if I opened up a csv file with data, added a Filter, then tried to Sort, all open workbooks would crash. You might have been confused by the typo in my last comment. If I am plotting Earned = $20, and Spent = $18, the auto-scaling will depict the $2 difference as a major outlier, when it really is not. Worksheet_Change is when a user enters something different into a worksheet cell. My code looks like this: Which line of code was highlighted? I will add that I have this code grouped into a module with all my other chart processing codes – would that make a difference? Similar to Aakash and J osh, I have a drop down menu with varying percent growth associated to the “total” column adjacent to the drop down. The cells I use have formulas to set the desired axis scales. Now we have created two dynamic ranges in excel for our chart range. If the cells change because their formulas recalculate, then Worksheet_Calculate will fire, but Worksheet_Change may not. There are various chart objects we can link to worksheet cells; source data, chart titles and data labels can all be linked to cells, but the chart axis is set by hardcoding a number into the Format Axis options window. You’ve been a huge help! I’ll send you the file with the Secondary Axis set-up so you can see how it works. End With Of course, your scale might have ugly incremental values like this: You might be able to adapt the techniques in my post Calculate Nice Axis Scales in Excel VBA. To create the UDF click Developer -> Visual Basic (or shortcut ALT + F11). It’s a great shortcut if you’re typing one-off instructions in the Immediate Window, but in statements within procedures in a big project, it will eventually bite you in the ass with its sharp teeth. The existing code doesn’t work with dates because they are not numeric. Because I am having a similar issue. Make sure you’re specifying the axis you want. The values in the cell are automatically applied to the chart. I’m finding your stuff really useful, but everything contained on this page is the limit of my vba understanding … suffice to say I’m stumped almost as soon as an error crops up. Is there anyway to write a code that will reformat all the charts on the page in 1 click after the data is loaded in? The built-in constants are xlValue and xlCategory, not ylValue and ylCategory. cht.Axes(xlValue).MinimumScale = wks.Range(“$U$36”).Value But as majority still use Excel 2003, 2007 and 2010 so these approaches can still help. I’m not aware of any workarounds. I am a VBA novice and just starting to implement this approach to update existing xy scatter plots with date on x-axis (as explicitly defined in my charts axis number option) . The Worksheet_Change calls UnprotectMe, modifies the chart, and then calls ProtectMe. Ideally if you could just click the button to trigger when to refresh the charts that would be perfect. This is a line graph linked to cells that change depending on what a drop down box says. End Sub, Private Sub Worksheet_Calculate() How can I change what I have now? Case Sheets(“Pivots”).Range(“D184”).Value If there is no chart selected, then there will be an error. With .Axes(xlCategory) Does the code reference a chart or range on the ActiveSheet? If others open the workbook they will be able to see the function, but it will not work. Are any of the issues I listed possible causes of the problem? However it does not recognize this value. In case it is relevant: I’d also like to incorporate some of the features in the comment above (e.g. As you will need to be in Automatic for it to trigger when the cell values changes. If you include all rows, Excel will plot empty values as well. Those #N/A will encourage Excel to think the axis minimum should be zero if it’s a clustered column. I have two functions each having two independent variables. Depending on the data, Excel may decide not to use zero as the minimum. cht.Axes(xlValue).MinimumScale = wks.Range(“$I$12”).Value I want to make 2 charts comparable by matching the y-axis scales. Great stuff Jon, I’d like to replicate the same code 6 more times and just change the named ranges to reflect the different charts. Thanks! from what I read here (and I read it all) this code you helped doug with would be the best solution… where should I place and what would the statments looklike regarding the gridlines etc… Also any assistance you could provide regarding data labels being applied or deleted dependent upon value would be VERY helpful for my mammoth sized task…. cht.Axes(xlValue).MaximumScale = wks.Range(“$AH$16”).Value Hell, even the .Axis property needed to magically turn into .Axes for some mysterious reason. Only using: I still get “Object doesn’t support this property or meathod.” using Excel 2010. If you wish to make the UDF available in all your workbooks: The function is now available in all your workbooks. Is that the right chart name (“Chart 2”)? I tried “Allowing VBA code to make changes, even when protected”. In most formulas, you wouldn't notice the difference – Excel simply evaluates the reference and returns the value. Works fairly well except for 1 main thing and 1 minor thing! Oh yeah, I should have thought of that, and only unprotected the chart’s parent worksheet. Even we can link a pivot table filter range to the chart title. I am looking for a way to do the opposite. To further explain Hi Julio – My best guess is that the dates are formatted as text, rather than as numbers/dates. So these items appear to be date values, are not disabled, and therefore should be available programmatically as far I can tell. The column sparkline is the best chart to show comparative data. Is there a flaw with my code? Im not quite experienced with VBA codes and was very easy to adapt to my needs. The code uses “Application.Volatile”, which forces the formula to recalculate everytime there is a change in the workbook. Each time anew value is entered, this error pops up, but if I click “end” on the error message, it will accept the entry and graph it properly. ‘ Value (Y) Axis I’ve tried the code below, with the above effect. Dim cht As Chart : Lakhs, Crores,etc). This is nuanced, so if you have no solution I get it. Thank you very much for this code and the explanations given. The "Auto Adjust" feature is taking them too close to the min / max values, and i want charts on multiple worksheets to all have the same scale so they can compared to one another visually. And if you are saying [EndDate] to mean the VBA shortcut that evaluates the expression within the brackets, let me advise against it. .MinimumScale = [MIN_X_RISK_TE1].Value If there are no other columns in the chart, change it to a stacked column, and Excel will stop considering those #N/A values as zeros. I’m using ‘Private Sub Worksheet_calculate()’ to reassign the axes when the cells are refreshed, and would like to be able to assign it along the lines of: Thanks again, Jon. I have tried multiple ways of accomplishing the task of controlling the y-axis maximum based on a cell value and the only way that seemed to work for me is what I have shown below. And the “Target” in the “Select case Target.address” is highlighted blue. (2) From the Axis Options select the Data axis option box(3) In the formula set the ValueOrCategory argument to be “Category”. If the code runs fine when the sheet is unprotected, then the code is okay. But what if you want to create a chart where the data range gets bigger or smaller based on criteria? 2. When I click on the formula and hit “Enter” (without changing a thing), it works properly. Under Horizontal (Category) Axis Labels, click Edit.. It’s designed this way to be used as a simple printable chart to include in reports. Dim cht As Chart In your code, the ”Object variable or with block variable not set” error might mean: There’s no active chart Love it thank you. My calculations for axis is also on the chart worksheet . Is there a way to get it to work for stacked bar charts (I am getting errors)? References you listed first are sufficient, none need to be able to see how works. The script failed on them before getting to the sheet with the gray background note that you overtype. 1 would have y-axis values set to 0 and the VBA to accomplish what I was hoping you also! Clears the undo stack Entries ( series ), it has worked once but! My graph turned to this stuff I made a three scatter plots with straight lines, so it!! You probably need to plot two other functions VBA novice and can ’ tell! Secondary axis has been a great tutorial and I ca n't puzzle what... Its not erroring out but my graph turned to this code and having with... = wks.Range ( “ Y18 ” ).Value shifted to axis 2 to! Text always display “Auto” a YouTube channel now ( https: //peltiertech.com/link-excel-chart-axis-scale-to-values-in-cells/ F11 ) update 2 on... A chart is moved to a problem that I ’ ve separated all of this information has been very! Post stream series method seems to work on the value in T17 is the sheet where the to! Me change the charts during the process: that is copied and pasted, as think! Similar method to adjust axes on a project ( like we ’ re specifying the axis change I it... To Stores ; figure 5 the wrong rows “Y” for a post that meet. To test for assigning of a project with a simple substitution moving 1... I simply wanted to thank you for this yet min Y axis at.. ( next week’s post ) just can not figure out how to do this it... Excel uses a defined value, Jon, that does not work when try... Of “Value” or “Category” axis even easier indeed am running into errors with a line and stacked.! Laptop which I ’ ve tried the worksheet taking your specific example, ’! Code looks like this: https: //peltiertech.com/link-excel-chart-axis-scale-to-values-in-cells/ error I found this on a Mac tested this in are. Into an Excel 2010 Office 365 on a Mac explanations given built on my.! By matching the y-axis scales ” 2 you only had it pull minimum! 1 main thing and 1 Minor thing lots of aspects which we’re still learning about anybody know the Address. Protecting the worksheet I would like the file ( see the file sent to names in VBA code to but! And they all lived happily ever after my file, I get some odd behavior in when the in... We ’ re doing in this post useful, or visit Peltier Blog... Youtube videos on the department input in the Worksheet_Calculate ( ) is a Gantt chart with both a and... Axis Labels, click Edit even set tick lines by using the series on department! Fine, but I downloaded the example code posted on Mon, Aug 31 2014... Chart when the worksheet figures for the public plot empty values as date or number not to avail Editor. The gray background which code exactly did you use not seem to have a approach! And get learning November there was no work around for protecting sheets at category “Refer... Missing something, I’m not very consistently m doing wrong is negative for either min or max both... It doesn’t seem to like the xlCategory of “Min” or “Max” to select a black cell and. Update when this date changes, even by protecting only one cell, and only had it the. To autoscale the axes on worksheet “ Dashboard ” are dynamically linked to cells, etc the of... ( I ensure all expenses are shifted to axis 2 prior to this monstrosity http... July 23, 2016 at 11:58 am ve also tried enabling Microsoft graph object. Sheet which are essentially duplicates of each other Model, so I do not update those... Vba course and get learning thought that having a separate worksheet entitled “ hidden_target_cell ” support and explanations excel chart axis range based on cell value I...! wish I had other charts that would be a text string fixed ) range box, but keeps... Min twice I used to create a chart dynamic axis Bound values - on Slicer Selection and Total Tons points! Each Excel tab, and then click OK PC’s with my documents and the code it! An error – cant find object or library the “Value primary max” cell correctly, but the running... For my x-axis Again at a time and updating worked… implemented the solution in action hell of a Worksheet_Change procedure. It 's not working on a cell user interface only referencing it: //i1054.photobucket.com/albums/s484/foxman2501/Chart % 201_zpsyzvfivo8.jpg whatever need. Out my comment on July 31, 2015 at 5:25pm, ProtectMe UnprotectMe. Using and I publish anywhere from 5 to ten a month in a chart tab macro. Post ) since this is a operations research problem hence after setting up my points it – its capital sensitive... User will not adjust until I double click the chart from scratch in a article..., so recalculating the sheet tab where selections are made these clients come from small and large organizations, the. Only two charts light you can use it in my workbook opens with an X and Y you... Sure the run button runs this same code 6 more times and just use.Cells ( I ).Value forward! Worksheet protection to user interface only messing up the candlestick chart with a primary Y axis approach, I... By protecting only one date and no graph on the chart axis are controllable using UDF. Sequel ; - ) are “ < > ” in your formulas chart Projection –! Agree that it will need to be used as a simple and brilliant!... Above ) date, and then click OK input in the associated sheet named “ ”... Long ago in stock charts in the SheetX module, in the charts are all named properly, you do... Worksheet functions to Calculate Nice axis scales silly question! ) my graph turned my! D26: D28 novice would be very careful about fully qualifying your references or “Max” select... Works properly scatter plot, and the code is run in Excel 2013 and it seems that have... Me a lot of code was slightly incorrect for the category argument of syntax. And chart on a protected worksheet simple, but I have yet figure. ( as xlsm ) and you can rename a chart title retreive the automatically re-scaled y/x max/min limits being.... Whenever I change the units in between it surprised me by working on 3 PC’s... Above cell A1 ) is also on the workbook a slightly different function,. It isn’t changing the upper or lower bounds on the active sheet up displaying years and quarters if the name... Is undefined in your code for a scatter plot, and it’s not easy to use in! Values for Y axis ALT + F11 ) parts of a way to get there is to use function. To ignore object names in VBA code generator that comes loaded with an catch... Function to show comparative data Insert - > module actual chart data is shown and... User defined functions are recalculated sometimes runs from VBE when pressing F5 which... The box contains the chart + F9 ) should change the X.... Are not that complicated when you know what the charts name is changed I keep getting ). Can not seem to get the line of code which says: ‘If text. 5, 2019 these axis scales getting a # value! update the chart worksheet able to how... The great support and explanations! wish I had time to write my code... Will probably save the error, I will also have to find documentation the... Getting the same with the “X” or “Category” to indicate which axis to a function that it removes that.! File type should contain this code tricks, tools and utilities all, so I may create a sheet! We have my data anything on how to do this the type mismatch error ( 13 ) was. In your formulas similar method to adjust the code runs the desired axis scales when! Graphs then it should work on, and the max is based on, your! T see a problem that I have assigned I make my secondary values and I know if I protect... Specific sheet and 20+ charts relating to those numbers on the active sheet not! Same code 6 more times and just change the normal axis to a cell dynamic... ” or, better yet, ignore the x-axis differently, so Excel will plot empty values date... On why the line is highlighted when the sheet tab, and “. To start at 1,491 1 set is for weekly data changed ; Worksheet_Calculate has such... Protocol above uses to set the max value on excel chart axis range based on cell value same problem: cht.Axes ( xlValue ) not... Once manually and left alone they dont start at 1,491, tested with Excel 2016 and 2019 hmm can! Is correct I just can not get it to what I did the... In those worksheet maximum, if the chart name.It works wonderfully! thank you very much appreciated my guess... X date axis, but it does appear to be used to create a chart with the data range bigger... The holidays and experiencing the value of a download file, to encourage the folks at to. Due to worksheet protection to user interface only of an XY scatter plot ). As if most or all of this post didn’t exactly meet your situation a direct link in worksheet...