3

好吧,我的目标是创建一个图表制作宏,因为我有大约 90 个不同的站名;每个站都需要自己的图表。

我想使用的多个系列是我的估计 CFS、模拟 CFS、压力期数(编号)和站名。我正在尝试制作简单的 xy 散点线图,其中 No. 值将是我的 x 范围,而 est:CFS 和 sim:CFS 都将是我的 y 范围以创建 2 条相当简单的线。

现在我的问题很简单:我应该如何在 VBA 中设计代码,以便它知道停止接收 Niobrara 河站图表图表中的系列数据,并开始使用以下数据蛇河站图表......等等以此类推,直到它遍历所有 90 多个图表。

我不知道如何用一个命令来循环并读取该站名列,并让它理解与该站相对应的所有行都属于它,并让它理解这就是它需要的所有数据图表,而当车站改变时,我希望它移动到下一个。

下图显示了工作表上的数据是如何布局的:

工作表

如果这有点难以理解,我深表歉意,如果我可以提供更多信息以使我的问题更清楚,我很乐意发布更多信息。

4

1 回答 1

3

下面的代码将允许您从工作表中检索所有唯一的站名称并将它们放入Stations()字符串数组中。

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变量就是这样,一个整数告诉你的代码最上面一行是数据开始的地方 。StationNameColumn是包含站名的列的编号(A=1、B=2 等)

获得站名数组后,您可以逐步通过站名列并检索与数组中每个项目关联的所有数据值。然后根据该数据创建单独的图表。

或者,您可以逐一检查站名列中的值,只检索与当前站名关联的第一行和最后一行的地址,然后根据该数据创建图表。下面的代码将执行此操作,假设您的数据按站点名称排序,以便所有相同的站点名称组合在一起。

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

然后,您创建实际图表的代码可以使用第一个和最后一个(行)的值来检索这些行的适当数据。

于 2012-09-04T21:33:08.327 回答