the code I posted will only work with an XY scatterplot. For any other chart type you will need to create dynamic named ranges and insert them into the source data of the chart. Ther are some examples of this at the links below:
BTW, another approach would be to have your MIN and MAX cell entries "feed" an criteria range for an Advanced AutoFilter. You can then set the Chart options (using Tools | Options... menu. command) to only plot visible cells.
if you have an xy scatterplot the code I posted should work. Try this: Open a new workbook and put some xy data and make a chart out of it (as an object in the worksheet). Then right-mouse click on the worksheet tab and choose View Code and paste the code below:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("G2") Or Target = Range("H2") Then
ActiveSheet.ChartObjects("Chart 1").Activate
With ActiveChart.Axes(xlCategory)
If Range("G2").Value <> "" Then .MinimumScale = Range("G2").Value
If Range("H2").Value <> "" Then .MaximumScale = Range("H2").Value
End With
End If
End Sub
Then when you change the values of G2 and H2 the chart x-axis should change accordingly.
But, I copied / modified the code for the Y-axis (xlvalue), and when I enter values into these cells, the code doesn't work, and 'defaults' to the code for the X-axis. ???
Also, if i move around the spreadsheet, and randomly delete data from other cells, it triggers the x-axis macro, and a DEBUG. According to the help, the TARGET macros should not do this...?
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.