【问题标题】:Find row number of match VBA查找匹配 VBA 的行号
【发布时间】:2020-09-17 16:40:32
【问题描述】:

在一张表中,我有一个姓名列表,在另一列中有一个与之相关的金额。我需要在另一张纸的一列中写下这些名称而不重复。当名称重复时,我还需要汇总金额。 例如:

XXXX 20
YYYY 30
XXXX 10

我的结果应该是

XXXX 30
YYYY 30

我正在执行一个 for 循环来遍历名称,如果我没有在结果列中写下该名称,我会写下它。如果我已经写了,我将不得不留在同一行并计算金额的总和。 问题是我尝试使用 Range.Find,但即使名称已经写好,它也总是返回 Nothing。

代码如下:

Sub CommandButton1_Click()

Dim rif_reg As Integer
Dim rif_col As Integer
Dim riga_vuota As Integer
Dim rif_foglio As Integer
Dim rowNum As Range

For Each area In Worksheets("XXX").Range("C15:C26,C30:C41,C44:C55,C58:C69").Areas 'more than 1 selected area
    For Each cell In area 'loop through each cell in the selected range
        If IsEmpty(cell) = False Then
            If IsNumeric(cell) = False Then
                If Range("A61:A74").Find(What:=cell.Value2, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True) is Nothing Then 'search if the name has not being inserted yet
                    Cells(rif_rig + 61, rif_col + 1) = cell 'Nominativo
                    Cells(rif_rig + 61, rif_col + 7) = Worksheets("XXX").Cells(area.Cells(1).Row, rif_col + 1).Value2 'Ruolo
                    Cells(rif_rig + 61, rif_col + 11) = (Worksheets("XXX").Cells(cell.Row, rif_col + 5).Value2 * Worksheets("YYY").Cells(rif_foglio + 32, rif_col + 2)) 'Quota Lordo
                    rif_rig = rif_rig + 1
                Else
                    Set rowNum = Range("A61:A74").Find(What:=cell.Value2, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
                    Cells(rowNum.Row, rif_col + 11) = (Cells(rowNum.Row, rif_col + 11) + Worksheets("XXX").Cells(cell.Row, rif_col + 5).Value2 * Worksheets("YYY").Cells(rif_foglio + 32, rif_col + 2)) 'Quota Lordo
                    If Cells(rowNum.Row, rif_col + 7) <> Worksheets("XXX").Cells(area.Cells(1).Row, rif_col + 1).Value2 Then
                        Cells(rowNum.Row, rif_col + 7) = Cells(rowNum.Row, rif_col + 8) & "," & Worksheets("XXX").Cells(area.Cells(1).Row, rif_col + 1).Value2 'Ruolo
                    Else
                        Cells(rowNum.Row, rif_col + 7) = Worksheets("XXX").Cells(area.Cells(1).Row, rif_col + 1).Value2 'Ruolo
                    End If
                End If
            Else
            End If
        Else
        End If
    Next
areaCount = areaCount + 1
Next

由于这不起作用,我尝试使用 Application.Match 但这并没有给我正确的行号

Sub CommandButton1_Click()

Dim rif_reg As Integer
Dim rif_col As Integer
Dim riga_vuota As Integer
Dim rif_foglio As Integer
Dim rowNum As Long

For Each area In Worksheets("XXX").Range("C15:C26,C30:C41,C44:C55,C58:C69").Areas 'more than 1 selected area
    For Each cell In area 'loop through each cell in the selected range
        If IsEmpty(cell) = False Then
            If IsNumeric(cell) = False Then
                If IsError(Application.Match(cell.Value2, Range("A61:A74"), 0)) Then 'search if the name has not being inserted yet
                    Cells(rif_rig + 61, rif_col + 1) = cell 'Nominativo
                    Cells(rif_rig + 61, rif_col + 7) = Worksheets("XXX").Cells(area.Cells(1).Row, rif_col + 1).Value2 'Ruolo
                    Cells(rif_rig + 61, rif_col + 11) = (Worksheets("XXX").Cells(cell.Row, rif_col + 5).Value2 * Worksheets("YYY").Cells(rif_foglio + 32, rif_col + 2)) 'Quota Lordo
                    rif_rig = rif_rig + 1
                Else
                    rowNum = Application.WorksheetFunction.Match(cell.Value2, Range("A61:A74"), 0)
                    Cells(rowNum, rif_col + 11) = (Cells(rowNum, rif_col + 11) + Worksheets("XXX").Cells(cell.Row, rif_col + 5).Value2 * Worksheets("YYY").Cells(rif_foglio + 32, rif_col + 2)) 'Quota Lordo
                    If Cells(rowNum, rif_col + 7) <> Worksheets("XXX").Cells(area.Cells(1).Row, rif_col + 1).Value2 Then
                        Cells(rowNum, rif_col + 7) = Cells(rowNum, rif_col + 8) & "," & Worksheets("XXX").Cells(area.Cells(1).Row, rif_col + 1).Value2 'Ruolo
                    Else
                        Cells(rowNum, rif_col + 7) = Worksheets("XXX").Cells(area.Cells(1).Row, rif_col + 1).Value2 'Ruolo
                    End If
                End If
            Else
            End If
        Else
        End If
    Next
areaCount = areaCount + 1
Next

谢谢, 卡洛塔。

附:我是 VBA 新手,所以我的代码可能非常混乱

【问题讨论】:

  • 为什么不直接使用数据透视表?
  • 无论何时使用Range()Cells(),您都需要使用特定的工作表对象对其进行限定,否则您的代码可能 工作与否,具体取决于发生的工作表活跃起来。
  • ...似乎还有一堆缺失的代码,并且您使用的一些变量不是您声明的变量 - 例如rif_reg vs rif_rig
  • @BigBen - 你是对的,因为我选择了错误的代码行。我要在第一个宏中选择 Find 代码。 rowNum = Range("A61:A74").Find(What:=cell.Value2, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True).Row
  • @GMalc - 除了最好先测试查找是否成功,而不是最后链接.Row

标签: excel vba find range match


【解决方案1】:

您不必遍历范围内的所有区域(大约是For Each area In Worksheets("XXX")...)。只需遍历范围内的单元格,范围对象将负责将它们全部显示给您。

另外,不要将所有范围都写入宏。您每次都必须更改代码才能选择其他单元格。在运行宏之前选择所需的单元格并使用 Selection 对象会更容易(请不要担心这里有这么多文字 - 实际上,代码不到 30 行,其余的都是 cmets 给你):

Sub SumsByNames()
Dim rSource As Range
Dim aCell As Range  ' Iterate by each cells in current Selection
Dim aData As Variant    ' Array for Names and ranges with this Name
Dim i As Long
    aData = Array() ' Empty array
    Set rSource = Application.Intersect(Selection, ActiveSheet.UsedRange)
    For Each aCell In rSource ' If cell is not empty then .text also not empty string
        If aCell.Text <> vbNullString Then
            If Not IsNumeric(aCell) Then
                Call addToData(aData, aCell.Text, aCell.Offset(0, 1))
            End If
        End If
    Next aCell
    Set aCell = Worksheets("Result").Range("A61")
    aCell.CurrentRegion.ClearContents
    For i = LBound(aData) To UBound(aData)
        aCell.Value = aData(i)(0)   ' Paste Name
        aCell.Offset(0, 1).Value = Application.WorksheetFunction.Sum(aData(i)(1))
        Set aCell = aCell.Offset(1, 0)
    Next i
End Sub

Sub addToData(ByRef aData As Variant, sName As String, rCell As Range)
Dim i As Long
    For i = LBound(aData) To UBound(aData)
        If aData(i)(0) = sName Then
            Set aData(i)(1) = Application.Union(aData(i)(1), rCell)
            Exit Sub
        End If
    Next i
    i = UBound(aData) + 1
    ReDim Preserve aData(i)
    aData(i) = Array(sName, rCell)
End Sub

示例文件mySumIf_TestDemo.xlsm

【讨论】:

  • 请在运行宏之前尝试选择具有 NAME X ... NAME Z 的单元格,并且只选择那些单元格(没有要汇总的数据)。
  • 在您的原始数据表上执行此操作。该宏将收集您选择的所有名称,注意它们的唯一性(跳过重复项)并将该名称右侧的下一个单元格中的值相加。结果将从单元格 A61 开始写入另一个工作表(我将其称为“结果”,您可能有不同的工作表名称)
  • 感谢您的建议,我尝试了您的代码,但这是我得到的:'1 NAME X 3 NAME Y 2 NAME W NAME Z' 但是我想要的是:` NAME X NAME Y NAME W NAME Z ` 另外我必须做的总和来自另一个工作表
  • 谢谢!我明白现在问题出在哪里了!
  • @CarlottaFabris 我稍微修改了代码以避免错误并加快了宏的速度,并添加了一个演示文件
【解决方案2】:

您可以尝试修改此代码

Sub test()
    
    Dim Sh1 As Worksheet, Sh2 As Worksheet
    Dim CollNames As Collection
    Dim Cella As Range
    Dim R As Long
    
    Set Sh1 = ThisWorkbook.Sheets("Foglio1") 'origine
    Set Sh2 = ThisWorkbook.Sheets("Foglio2") 'destinazione
    Set CollNames = New Collection
    With Sh1
        R = 1
        On Error Resume Next
        For Each Cella In .Range("A1:A" & .Cells(Rows.Count, 1).End(xlUp).Row) 
            CollNames.Add Sh2.Cells(R, 1), CStr(Cella)
            If Err.Number = 0 Then
                CollNames(Cella) = Cella
                R = R + 1
            Else
                Err.Clear
            End If
            CollNames(Cella).Offset(0, 1) = CollNames(Cella).Offset(0, 1) + Cella.Offset(0, 1)
        Next Cella
        On Error GoTo 0
    End With
    Set Sh1 = Nothing
    Set Sh2 = Nothing
    Set CollNames = Nothing
    
End Sub

代码读取 Sh1 的 A 和 B 列中的数据,并在 Sh2 的相同列中报告结果。

需要修改 For Each 语句中的范围和偏移量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    相关资源
    最近更新 更多