Automatically selecting the new data for charts (dynamic arrays)

%3CLINGO-SUB%20id%3D%22lingo-sub-2431433%22%20slang%3D%22en-US%22%3EAutomatically%20selecting%20the%20new%20data%20for%20charts%20(dynamic%20arrays)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2431433%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20an%20excel%20sheet%20that%20automatically%20pulls%20in%20the%20new%20data%20that%20i%20want%20and%20adds%20it%20to%20the%20next%20available%20row.%20I%20have%20several%20charts%20that%20are%20associated%20with%20this%20data%20and%20right%20now%20i%20have%20to%20go%20through%20and%20select%20each%20data%20set%20individually.%20which%20is%20a%20pain.%20I%26nbsp%3B%20am%20trying%20to%20create%20a%20few%20lines%20of%20code%20that%20will%20automatically%20resize%20the%20data%20that%20is%20grabbed.%20I%20have%20the%20data%20that%20starts%20on%20Q4%20and%20i%20want%20it%20to%20go%20to%20U%2C%20to%20what%20ever%20the%20value%20of%20the%20integer%20is%20that%20i%20tell%20it.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDim%20CS%20As%20Integer%26nbsp%3B%20'%20this%20is%20my%20last%20point%20of%20data%3CBR%20%2F%3EDim%20RangerRick%20As%20Range%20'this%20is%20my%20array%20size%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3ESheets(%22EW1060%22).Select%20'%20the%20sheet%20i%20am%20starting%20on%3CBR%20%2F%3ECS%20%3D%20Range(%22AQ3%22)%20'%20the%20integer%20that%20will%20define%20the%20size%20of%20the%20array%3CBR%20%2F%3EReDim%20RangerRick(%22Q4%22%2C%20CS)%26nbsp%3B%20'%20the%20following%20several%20lines%20are%20my%20attempt%20at%20trying%20to%20get%20it%20to%20define%20the%20data%20size%3CBR%20%2F%3E'%20RangerRick%20%3D%20%24Q%243%3AINDEX(%24Q%3A%24U%2CCOUNTA(%24Q%3A%24Q))%3CBR%20%2F%3E'%20RangerRick%20%3D%20Range(%22RangerRick%22).Resize(%2C)%3CBR%20%2F%3E'%20RangerRick%20%3D%20Range(%22Q4%22%2C%20Cells(21%2C%20CS))%3CBR%20%2F%3E'%20MsgBox%20(CS)%20'%20my%20check%20to%20make%20sure%20CS%20was%20grabing%20the%20right%20value%3CBR%20%2F%3EActiveSheet.ChartObjects(%22Chart%201%22).Activate%20'%20then%20the%20next%20several%20lines%20are%20redoing%20the%20graph%20with%20the%20new%20data%20I%20know%20this%20part%20works%20if%20i%20manually%20put%20in%20a%20number.%3CBR%20%2F%3EActiveChart.PlotArea.Select%3CBR%20%2F%3EApplication.CutCopyMode%20%3D%20False%3C%2FP%3E%3CP%3E'Sheets(%22EW1061%22).Select%20'%20from%20here%20forward%20i%20want%20it%20to%20take%20the%20already%20defined%20data%20size%20and%20use%20it%20to%20change%20a%20chart%20across%20several%20other%20sheets%26nbsp%3B%3CBR%20%2F%3E'ActiveSheet.ChartObjects(%22Chart%201%22).Activate%3CBR%20%2F%3E'%20ActiveChart.Axes(xlCategory).MinorGridlines.Select%3CBR%20%2F%3E'%20Application.CutCopyMode%20%3D%20False%3CBR%20%2F%3E'ActiveChart.SetSourceData%20Source%3A%3DRange(%22Q3%3AU12%22)%3CBR%20%2F%3EEnd%20Sub%3CBR%20%2F%3EActiveChart.SetSourceData%20Source%3A%3DRangerRick%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2431433%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2432496%22%20slang%3D%22en-US%22%3ERe%3A%20Automatically%20selecting%20the%20new%20data%20for%20charts%20(dynamic%20arrays)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2432496%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1073200%22%20target%3D%22_blank%22%3E%40rh88596%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20can%20change%20the%20data%20source%20of%20the%20charts%20to%20a%20table%2C%20you%20won't%20need%20any%20code.%3C%2FP%3E%0A%3CP%3EAnother%20option%20is%20to%20create%20dynamic%20named%20ranges%20for%20the%20x-values%20and%20y-values.%3C%2FP%3E%0A%3CP%3ESee%20%3CA%20href%3D%22https%3A%2F%2Ftrumpexcel.com%2Fdynamic-chart-range%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EHow%20to%20Create%20a%20Dynamic%20Chart%20Range%20in%20Excel%3F%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2435873%22%20slang%3D%22en-US%22%3ERe%3A%20Automatically%20selecting%20the%20new%20data%20for%20charts%20(dynamic%20arrays)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2435873%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20agree%20that%20those%20are%20ways%20to%20try%20and%20do%20it%20with%20a%20table.%20The%20Issue%20i%20am%20running%20into%20is%20that%20for%20most%20of%20the%20cells%20that%20i%20would%20need%20to%20include%20into%20the%20table%20have%20equations%20already%20in%20them.%20these%20cells%20are%20pre-allocated%20with%20the%20equations%20so%20that%20when%20it%20pulls%20in%20the%20new%20data%20all%20the%20calculations%20are%20automatically%20done.%20The%20problem%20with%20the%20table%20method%20is%20that%20it%20doesn't%20grab%20just%20the%20information%20that%20I%20want%20at%20this%20time%20to%20be%20in%20the%20graph.%20which%20is%20why%20i%20am%20trying%20to%20create%20an%20index%20system%20to%20select%20appropriately.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I have an excel sheet that automatically pulls in the new data that i want and adds it to the next available row. I have several charts that are associated with this data and right now i have to go through and select each data set individually. which is a pain. I  am trying to create a few lines of code that will automatically resize the data that is grabbed. I have the data that starts on Q4 and i want it to go to U, to what ever the value of the integer is that i tell it. 

 

Dim CS As Integer  ' this is my last point of data
Dim RangerRick As Range 'this is my array size

Sheets("EW1060").Select ' the sheet i am starting on
CS = Range("AQ3") ' the integer that will define the size of the array
ReDim RangerRick("Q4", CS)  ' the following several lines are my attempt at trying to get it to define the data size
' RangerRick = $Q$3:INDEX($Q:$U,COUNTA($Q:$Q))
' RangerRick = Range("RangerRick").Resize(,)
' RangerRick = Range("Q4", Cells(21, CS))
' MsgBox (CS) ' my check to make sure CS was grabing the right value
ActiveSheet.ChartObjects("Chart 1").Activate ' then the next several lines are redoing the graph with the new data I know this part works if i manually put in a number.
ActiveChart.PlotArea.Select
Application.CutCopyMode = False

'Sheets("EW1061").Select ' from here forward i want it to take the already defined data size and use it to change a chart across several other sheets 
'ActiveSheet.ChartObjects("Chart 1").Activate
' ActiveChart.Axes(xlCategory).MinorGridlines.Select
' Application.CutCopyMode = False
'ActiveChart.SetSourceData Source:=Range("Q3:U12")
End Sub
ActiveChart.SetSourceData Source:=RangerRick

3 Replies

@rh88596 

If you can change the data source of the charts to a table, you won't need any code.

Another option is to create dynamic named ranges for the x-values and y-values.

See How to Create a Dynamic Chart Range in Excel? 

@Hans Vogelaar 

 

I agree that those are ways to try and do it with a table. The Issue i am running into is that for most of the cells that i would need to include into the table have equations already in them. these cells are pre-allocated with the equations so that when it pulls in the new data all the calculations are automatically done. The problem with the table method is that it doesn't grab just the information that I want at this time to be in the graph. which is why i am trying to create an index system to select appropriately. 

@rh88596 

If a table isn't suitable, try dynamic named ranges.