• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

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

monkey10man

New Member
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:
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
 

Attachments

  • RI-AllMods.xlsm
    105.1 KB · Views: 3
Back
Top