VBA script to Automatically Plot Scatter Plots after scanning workbook for valid data

johnbrownbaby

New Member
Joined
Dec 9, 2015
Messages
38
Hello All,

I am attempting to automatically plot scatter plots on each tab in an excel workbook. I wanted some help in scanning each worksheet to see if data is present, then plot the scatter plot based on the number of valid data found. For example, in this spreadsheet:


Screenshot 2021-01-14 134044.png


There is 2 valid data sets. The data is always in the same type of format. Each dataset is separated by 5 columns....in this example, the first data set is at A and then G. This worksheet should produce 2 plots on the graph using Amp1 and Amp 2 as the x and y data respectively. The plot should look like this:

Screenshot 2021-01-14 135339.png



Once Row 1 separated by 5 columns have an entry, that is considered a valid plot. From the table presented, A28:A41 is the first x data and B28:B41 as the first y data, then switch to Plot 2 as column G1 has data using G28:G41 as the second x data and H28:H41 as the corresonding second y data.

Other worksheets may have a different number of valid data. Sheet 1 may have 4 sets, Sheet 2 may have just 1.

How to accomplish this task of plotting the scatter plots for each tab with different number of plots?

I was only able to come up with hard coding the plotting:

VBA Code:
Option Explicit

Sub create_advanced_vba_scatter_plot()
Dim ochart As Object, ochartObj As Object
Dim countryRow As Integer, lastrow As Integer
Set ochartObj = ActiveSheet.ChartObjects.Add(Top:=10, Left:=325, Width:=600, Height:=300)
Set ochart = ochartObj.Chart
Dim sheetName As String

ochart.ChartType = xlXYScatterLinesNoMarkers

sheetName = ActiveSheet.Name

'ochart.SeriesCollection.Add Source:=Range("B2:B21")
'Data Series Entry
ochart.SeriesCollection.NewSeries
ochart.SeriesCollection(1).XValues = Range("A28:A41")
ochart.SeriesCollection(1).Values = Range("B28:B41")

ochart.SeriesCollection.NewSeries
ochart.SeriesCollection(2).XValues = Range("G28:G41")
ochart.SeriesCollection(2).Values = Range("H28:H41")

'Chart setup
ochart.HasTitle = True
ochart.ChartTitle.Text = sheetName
ochart.Axes(xlCategory).HasTitle = True
ochart.Axes(xlCategory).AxisTitle.Caption = "Current (A)"
ochart.Axes(xlValue).HasTitle = True
ochart.Axes(xlValue).AxisTitle.Caption = "Current (A)"
ochart.SeriesCollection(1).HasDataLabels = False
ochart.Axes(xlValue).CrossesAt = -200


End Sub




Thanks!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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