loops - Excel宏: How to loop a chart making macro through a specific column every time the name in that column changes

标签 loops excel charts vba

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

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

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

我很难理解如何编写一个命令来循环并读取该站名称列,并让它理解与该站对应的所有行都属于它,并让它理解这就是它的所有数据需要一张图表,而当电台发生变化时,我希望它转到下一张图表。

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

worksheet

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

最佳答案

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

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

关于loops - Excel宏: How to loop a chart making macro through a specific column every time the name in that column changes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12268581/

相关文章:

java - 如何在 apache poi 中正确评估这个公式?

javascript - DipleJS - 限制图表中显示的类别轴值的数量

php - 在使用 foreach 循环构建 sql 查询时删除最后一个 "UNION ALL"

javascript - 尝试在 Canvas 中延迟循环播放图像

bash - 嵌套Bash while循环,内层循环只是loop循环

Python - 如何在检查前向元素时循环遍历列表?

vba - Excel VBA - 自动筛选(2 列/2 个条件)复制与条件不匹配的行

c# - 使用 C# 使用小计函数后以编程方式对 Excel 中的数据进行排序

VBA:为散点图中的误差线创建不同的格式

jquery - 如何在谷歌图表 API 中从年份中删除小数?