【问题标题】:Excel Macro: How to loop a chart making macro through a specific column every time the name in that column changesExcel宏:每次该列中的名称更改时,如何通过特定列循环制作宏
【发布时间】:2012-08-29 09:56:11
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

  • 如果这个问题的答案有帮助,您应该点击绿色的勾号表示答案已被接受。 See this

标签: loops excel charts vba


【解决方案1】:

下面的代码将允许您从工作表中检索所有唯一的电台名称并将它们放入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

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

【讨论】:

  • 我的评论从未出现过!上个月当你回答时我提到了我多么想感谢你,它帮助我解决了很多问题!
  • @WATERflowTech,很乐意为您提供帮助。如果它解决了您的问题,您可以单击向上/向下投票箭头下方的复选标记以接受此答案。这样,其他人就会立即知道这有帮助。
  • 啊,太刺激了!哈我试过了,但直到现在我的排名还不够高,无法投票,再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多