Alright so my goal is to create a chart making macro since I have about 90-some different Station name; and Each station needs it’s own chart.
The multiple series I would like to use are my estimated CFS, simulated CFS, stress period number (no.), and the Station Name. I’m attempting to make simple xy scatter line graphs where the No. value would be my x range, and the est:CFS and sim:CFS would both be my y ranges to create 2 rather simple lines.
Now my problem is simply: How should I design the code in VBA so that it knows to stop receiving data for a series in the chart for Niobrara River Station chart, and start using the following data the Snake River station chart… and on so forth until it loops through all 90-some charts.
I’m stumped on how to phrase a command to loop through and read that Station Name column and for it to understand that all those rows that correspond to that station belong to it, and for it to understand that’s all the data it needs for one chart while when the station changes I’d like it to move on to the next.
The image below shows how the data on the worksheet is laid out:
I apologize if that’s a little difficult to understand if there is any more information I can provide to make my question more clear I’d be happy to post more.
The code below will allow you to retrieve all the unique Station names from the worksheet and put them into the
Stations() string array.
Dim TopRowOfData As Integer Dim StationNameColumn As Integer Dim Stations() As String Sub GetUniqueChartNames() Dim rngTmp As Range Dim outRange As Range 'Select the first data cell of the Station name column. Cells(TopRowOfData, StationNameColumn).Select 'Select the rest of the data in the column. Range(Selection, Selection.End(xlDown)).Select 'Assign this data to a range variable. Set rngTmp = Selection 'Find a row that occurs below the area of the range data. This will be used 'to paste the filtered values into temporarily. outRow = rngTmp.Row + rngTmp.Rows.Count + 10 '10 is arbitrary, could be any number. Set outRange = Cells(outRow, 1) 'Filter the data values by unique values and paste the results into the outRange area. rngTmp.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=outRange, Unique:=True 'Get the output results of the filter operation and store them in a range object. 'outRow contains the heading of the filtered column. outRow + 1 is where the data starts. Cells(outRow + 1, 1).Select Range(Selection, Selection.End(xlDown)).Select Dim rngFinal As Range Set rngFinal = Selection 'Add the output results into the Stations array. ReDim Stations(rngFinal.Rows.Count - 1) Dim i As Integer For i = 0 To rngFinal.Rows.Count - 1 Stations(i) = Cells(rngFinal.Row + i, rngFinal.Column).Value Next 'Delete the temporary range. rngFinal.Clear End Sub
TopRowOfData variable is just that, an integer telling your code what the top row is where the data starts.
StationNameColumn is the number of the column containing station names ( A=1, B=2, etc.)
Once you have the array of Station names you can step down through the station name column and retrieve all the data values associated with each item in the array. Then create the individual charts based on that data.
Or you can step through the values in the station name column and just retrieve the address of the first and last row associated with the current station name, and then create your chart based on that data. The code below will do that, assuming your data is sorted by Station name so that all identical station names are grouped together.
Sub FindRowsAssociatedWithStationName() Dim i As Integer Dim j As Integer Dim rng As Range 'Temp variables to store the first and last row numbers. Dim first As Integer Dim last As Integer 'Loop through all of the station names. For i = 0 To UBound(Stations) 'Select the first data cell of the station names column. Cells(TopRowOfData, StationNameColumn).Select 'Select the rest of the data in the column. Range(Selection, Selection.End(xlDown)).Select 'Assign this data to a range variable. Set rng = Selection 'Initialize both of the row number variables to 0. first = 0 last = 0 'Loop through all the data rows. For j = 0 To rng.Rows.Count - 1 If Cells(rng.Row + j, StationNameColumn).Value = Stations(i) Then 'Set the first variable. If first = 0 Then first = rng.Row + j End If 'Set the last variable. last = rng.Row + j End If Next 'Call a method to create the actual charts, passing in the first and last row associated with the current Station name. Call CreateChart(first, last) Next End Sub
Your code to create the actual charts could then use the values of first and last (rows) to retrieve the appropriate data for those rows.