搜索相似行的工作表以将Excel VBA组合在一起

时间:2011-08-12 17:31:19

标签: arrays excel vba indexing find

我有一个工作表,其顶部列出了标题。我也在下面的行中列出了数据。该数据将作为一行保持组合在一起,并且不能相对于彼此改变位置。但是,整行可以自由移动。每个标题描述了下面行的数据类型。

我希望能够:

  1. 告诉宏在哪一列中查找组行。为方便起见,我们将此称为 Col D
  2. 查看整个工作表。
  3. 移动相似的行,使它们组合在一起。***
  4. 将组添加到选择
  5. *注意我也会在Z栏中用整数标记组,以显示该组是一个组

    我遇到的问题是我的子FindRow的以下行:

    ActiveSheet.Range(curRange).Resize (resizeRng)
    

    Dubug说curRange =“Elevator”是一个单元格的值,但不是一个范围,但我在上面定义了它:

    inRange = "A" & rngStart & ":" & Mid(alphabet, totHdrLngth, 1) & belowRowCount
    Set curRange = ActiveSheet.Range(inRange)
    

    作为参考,我解释下面的整体代码:

    CreateGroup - 检查后续行是否在 Col D 中具有相同的值。如果是这样,它将该组标记为唯一整数,并增加选择。如果没有调用FindRow。

    FindRow - 在整个工作表中搜索<​​strong> Col D 中具有相同值的行(row1)。如果找到,请调用rowSwapper将找到的行(row2) row1 下面的行交换。

    RowSwapper - 函数接收两个行号并交换它们。

    这是子 CreateGroup

    Public Sub CreatGroup()
    Dim letterCount As Integer
    Dim letString, label, inRange As String
    Dim rowSelect, marker, rowStart, rowCount, rngStart, count As Long
    
    'Predefine necessary variables
    marker = 1
    rowCount = 2 'Start at  row 2, excluding header
    rowStart = 2 'For range reference to create group
    belowRowCount = 3 'start below rowCount
    isGroup = False
    count = 1
    
    'Loop through each cell in column D to determine if consecutive metrics have the same value
    'If true, while loop sorts each series of consecutive metrics individually
    Do
        curCellVal = ActiveSheet.Range("D" & rowCount).Value  'Returns value rowCount(th) row of "Metric" column
        nextCellVal = ActiveSheet.Range("D" & belowRowCount).Value 'Returns the value of the row under rowCount
        marker = 1
        resizeRng = 0
    
        'Makes a cell selection while cell values in column 4 are equal
        If curCellVal = nextCellVal Then '<<<NECESSARY?
            isGroup = True   'Designates group has been found
            If resizeRng = 0 Then
                rngStart = rowCount
            End If
            'rowStart = ActiveSheet.Row
    
            'Resize selection to include cell with same metric name
            inRange = "A" & rngStart & ":" & Mid(alphabet, totHdrLngth, 1) & belowRowCount
            Set curRange = ActiveSheet.Range(inRange)
    
            'Establish place holder in col "Z" (empty) to track groups
            ActiveSheet.Range("Z" & rowCount).Value = marker
            ActiveSheet.Range("Z" & belowRowCount).Value = marker
    
            resizeRng = resizeRng + 1
            rowCount = rowCount + resizeRng
            belowRowCount = rowCount + 1
    
        ElseIf curCellVal <> nextCellVal Then 'And isGroup = True Then
    
            FindRow rowCount
    
            'Re-establish rowCount to account for cells that may have been added to group
            rowCount = rowCount + resizeRng
            belowRowCount = rowCount + 1
        Else
            rowCount = rowCount + 1
            belowRowCount = rowCount + 1
        End If
    
        'to prevent subsequent groups of metrics from being labeled together
        marker = marker + 1
    
        isGroup = False
    Loop While rowCount <= totCount
    
    End Sub
    

    此外,为了进一步参考,这些潜艇是这里描述的更大程序的一部分: Sorting Groups of Rows Excel VBA Macro

    让我知道你的想法......

1 个答案:

答案 0 :(得分:0)

如果要获取单元格范围地址,只需要说rng.Address即可。只是指定你想要rng与说rng.Value

是相同的(基本上)

关于这一行:

ActiveSheet.Range(curRange).Resize (resizeRng)

您可能想尝试

ActiveSheet.Range(curRange).Cells.Resize (resizeRng)

我昨晚在一个自己的小项目上玩了一些格式,并遇到了类似的错误。

希望有所帮助。