I have a table of process measurements. Rows are measurements and columns are different variables. I have a macro that copies the data from another worksheet. The number on measurements can vary, and I'd like to write a macro that automatically selects range based on the number on measurements and plots the variable values as a function of time. I'd also like to have a macro that moves the source column of the chart one to the right, so every time that I run the macro, it plots different variable as a function of time(measurement). How should I approach this problem? Thank you.
1 Answer
- Put your data in one sheet
- Then use
rngSourceData = Sheets(index).UsedRange
to get the data automatically. Draw your chart by VBA codes. The chart creation normally need the source data range as input, then pass the
rngSourceData
to the function. Such as below demo:Set PTCache = ActiveWorkbook.PivotCaches.Create( _ SourceType:=xlDatabase, _ SourceData:=rngSourceData, _ Version:=xlPivotTableVersion14)
Then you can rerun the step 2-3 and refresh the chart if your data changed.
You may record the macro codes to get the example on how to create your specific chart then change the codes a little bit.