Excel宏:每次该列中的名称更改时,如何循环使宏通过特定列的图表

时间:2012-09-04 17:32:39

标签: loops excel-vba charts vba excel

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

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

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

我很难说如何用一个命令来循环并读取该站名列,并让它理解所有那些与该站对应的行都属于它,并让它理解&#39 ; s当一个图表所需的所有数据,而当电台改变时,我希望它继续下一个。

下图显示了工作表中数据的布局方式:

worksheet

如果我能提供更多信息以便让我的问题更清楚,我很难理解,如果我有点难以理解,我很乐意发布更多信息。

1 个答案:

答案 0 :(得分: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

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