How to change a simple graph using VBA coding

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
I have never coded for graphs using VBA where when I run the macro it changes to the 20 most recent data values collected. If column "A" is going to house my y-values and column "N" is going to house my x-values, what would be the coding to change the graphs data range?

My Idea: I am thinking to get the 20 most recent values I could use a row variable that would equal

Code:
Cells(Rows.Count, "A").End(xlUp).Row

This would help me find the last data point entered. I just don't know the proper syntax to manipulate a graph
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
OK, I believe I have most of it. I just need help getting the "datespulled" variable to be placed in Chart 1 as the x-axis labels. Any ideas?!

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> Graph_Q2()<br><SPAN style="color:#00007F">Dim</SPAN> datespulled <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> chartdata <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> x <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>       <SPAN style="color:#007F00">'row variable</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>       <SPAN style="color:#007F00">'column variable</SPAN><br><br><br>Sheets("Historical").Select<br><br>x = Cells(Rows.Count, "A").End(xlUp).Row<br><br>    <SPAN style="color:#00007F">If</SPAN> x < 24 <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> chartdata = Range(Range("O3"), Cells(x, "O"))<br>        <SPAN style="color:#00007F">Set</SPAN> datespulled = Range(Range("A3"), Cells(x, "A"))<br>    <SPAN style="color:#00007F">Else</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> chartdata = Range(Cells(x - 20, "O"), Cells(x, "O"))<br>        <SPAN style="color:#00007F">Set</SPAN> datespulled = Range(Cells(x - 20, "A"), Celss(x, "A"))<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>Sheets("Dashboard").Select<br>    ActiveSheet.ChartObjects("Chart 1").Chart.SetSourceData Source:=chartdata<br>    <br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

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.
Go back
Back
Top