Excel VBA: Chart-making macro that will loop throu

2019-08-20 10:02发布

Alright, I've been racking my brain, reading up excel programming for dummies, and looking all over the place but I'm stressing over this little problem I have here. I'm completely new to vba programming, or really any programming language but I'm trying my best to get a handle on it.

The Scenario and what my goal is:

The picture below is a sample of a huge long list of data I have from different stream stations. The sample only holds two (niobrara and snake) to illustrate my problem, but in reality I have a little over 80 stations worth of data, each varying in the amount of stress periods (COLUMN B).

COLUMN A, is the station name column. COLUMN B, stress period number COLUMN C, modeled rate COLUMN D, estimated rate

What I have been TRYING to figure out is how to make a macro that will loop through the station names (COLUMN A) and for each UNIQUE Group of station names, make a chart that will pop out to the right of the group, say in the COLUMN E area.

The chart is completely simple, it just needs two series scatterplot/line chart; one series with COLUMN B as x-value and COLUMN C as y-value; and the other series needs COLUMN B as x-value and COLUMN D as y-value.

Now my main ordeal, is that I don't know how to make the macro distinguish between station names, use all the data relating to that name to make the chart, then looping on to the next Station group and creating a chart that corresponds for that, and to continue looping through all 80+ station names in COLUMN A and to make the corresponding 80+ charts to the right of it all in somewhere like the COLUMN E.

If I had enough points to "bounty" this, I would in a heartbeat. But since I do not, whoever can solve my dilemma would receive my sincere gratitude in helping me understand run this problem smoothly and hopefully better my understanding of scenarios like this in the future. If there is anymore information that I need to clarify to make my question more understandable please comment your query and I'd be happy to explain in more detail the subject.

Cheers.

Oh, and for extra credit; now that I think about it, I manually entered the numbers in COLUMN B. Since the loop would need to use that column as the x-value it would be important if it could loop through itself and fill that column on its own before it made the chart (I would imagine it would have something to do with anything as simple as "counting out the rows that correspond to the station name". But again, I know not the proper terminology to correspond the station name, hence the pickle I'm in; however if the veteran programmer who is savvy enough to answer this question could, I'd imagine such a piece of code would be simple enough yet crucial to the success of such a macro I seek.

enter image description here

1条回答
Deceive 欺骗
2楼-- · 2019-08-20 10:12

Try this

Sub MakeCharts()
    Dim sh As Worksheet
    Dim rAllData As Range
    Dim rChartData As Range
    Dim cl As Range
    Dim rwStart As Long, rwCnt As Long
    Dim chrt As Chart

    Set sh = ActiveSheet

    With sh
        ' Get reference to all data
        Set rAllData = .Range(.[A1], .[A1].End(xlDown)).Resize(, 4)
        ' Get reference to first cell in data range
        rwStart = 1
        Set cl = rAllData.Cells(rwStart, 1)
        Do While cl <> ""
            ' cl points to first cell in a station data set
            ' Count rows in current data set
            rwCnt = Application.WorksheetFunction. _
               CountIfs(rAllData.Columns(1), cl.Value)
            ' Get reference to current data set range
            Set rChartData = rAllData.Cells(rwStart, 1).Resize(rwCnt, 4)
            With rChartData
                ' Auto fill sequence number
                .Cells(1, 2) = 1
                .Cells(2, 2) = 2
                .Cells(1, 2).Resize(2, 1).AutoFill _
                   Destination:=.Columns(2), Type:=xlFillSeries
            End With
            ' Create Chart next to data set
            Set chrt = .Shapes.AddChart(xlXYScatterLines, _
               rChartData.Width, .Range(.[A1], cl).Height).Chart
            With chrt
                .SetSourceData Source:=rChartData.Offset(0, 1).Resize(, 3)
                ' --> Set any chart properties here

                ' Add Title
                .SetElement msoElementChartTitleCenteredOverlay
                .ChartTitle.Caption = cl.Value

                ' Adjust plot size to allow for title
                .PlotArea.Height = .PlotArea.Height - .ChartTitle.Height
                .PlotArea.Top = .PlotArea.Top + .ChartTitle.Height

                ' Name series'
                .SeriesCollection(1).Name = "=""Modeled"""
                .SeriesCollection(2).Name = "=""Estimated"""

                ' turn off markers
                .SeriesCollection(1).MarkerStyle = -4142 
                .SeriesCollection(2).MarkerStyle = -4142 

            End With

            ' Get next data set
            rwStart = rwStart + rwCnt
            Set cl = rAllData.Cells(rwStart, 1)
        Loop

    End With

End Sub
查看更多
登录 后发表回答