【问题标题】:VBA macro excel, calculating difference between two values based on previous variableVBA宏excel,根据前一个变量计算两个值之间的差异
【发布时间】:2017-07-06 05:01:05
【问题描述】:

我正在尝试为 Excel 项目添加现有 VBA 代码。 我正在寻找一个 VBA 来查找列中的重复值,结果将打印在另一列中。例如,如果 User1 在一个列中输入了两次,则第二次输入 - 下一列中将出现“重复”。

Sub DuplicateFinder()
    Dim LastRow As Long
    Dim matchFoundIndex As Long
    Dim iCntr As Long
    LastRow = Range("A65000").End(xlUp).Row
    For iCntr = 1 To LastRow
        If Cells(iCntr, 1) <> "" Then
            matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & LastRow), 0)
            If iCntr <> matchFoundIndex Then
                Cells(iCntr, 2) = "Duplicate"
            End If
        End If
    Next
End Sub

是否可以修改它,然后当发现重复时,它将检查另一列是否存在两个值之间的差异。

如果我有一个:

 A      |    B    |    C    |    D
 user1                11
 user2                11
 user1    duplicate   12      "error"

如果两个值之间的差为 =,我希望宏说“错误”

【问题讨论】:

  • 你必须使用VBA吗?一个简单的公式就可以解决问题。告诉我,我会发布的。
  • 如果 A 列中有两个以上的 user1 实例会怎样?应该比较哪两个值?
  • @Pucho 如果你能用公式做到这一点,请这样做。我没想到可以这样。
  • @J. Garth 如果有多个实例,它应该比较最后找到的重复值和原始值。

标签: vba excel


【解决方案1】:

如果要检查最近的单元格与匹配项之间的差异是否为

If iCntr <> matchFoundIndex Then
   Cells(iCntr, 2) = "Duplicate"
   If Cells(iCntr, 3) - Cells(matchFoundIndex, 3) <= 6 Then
      Cells(iCntr, 4) = "Error"
   End If
End If

如果你想要绝对差异:

If Abs(Cells(iCntr, 3) - Cells(matchFoundIndex, 3)) <= 6 Then

【讨论】:

  • 谢谢。我刚刚将此添加到我的第一个代码中,它可以满足我的需要
【解决方案2】:

对于更通用的方法,我会如下所示:

Option Explicit

Sub DuplicateFinder()
    Dim user As Variant

    With Sheets("duplicates") '<--| change "duplicates" to your actual sheet name
        With .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)) '<--| reference its column A range from row 1 (header) down to the one corresponding to last column A not empty row
            For Each user In GetUsers(.Resize(.Rows.Count - 1).Offset(1)) '<-- get unique users starting from 2nd row downwards and loop through them
                If Application.WorksheetFunction.CountIf(.Cells, user) > 1 Then HandleUser .Cells, user '<--| if more then one current user occurrences then "handle" it
            Next
        End With
        .AutoFilterMode = False
    End With
End Sub

Sub HandleUser(rng As Range, user As Variant)
    Dim cell As Range
    Dim iCell As Long, refvalue As Long

    With rng
        .AutoFilter Field:=1, Criteria1:=user '<--| filter column A cells with current 'user'
        With .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible) '<-- reference filtered cells, skippinh headers
            refvalue = .Cells(, 2).Value '<--| retrieve first occurrence value from cell two columns to the right
            For Each cell In .Cells '<--| loop through filtered cells
                If iCell > 0 Then '<--| start handling occurrences form the 2nd one on
                    cell.Offset(, 1) = "Duplicate" '<--| mark it as duplicate
                    If cell.Offset(, 2) - refvalue > 6 Then cell.Offset(, 3) = "error" '<--| place "error" if two cells to the right from current 'user' has a value greater then first occurrence value + 6
                End If
                iCell = iCell + 1 '<--| update user occurrences counter
            Next
        End With
    End With
End Sub

Function GetUsers(rng As Range) As Variant
    Dim cell As Range
    With CreateObject("Scripting.Dictionary")
        For Each cell In rng
            .Item(cell.Value) = cell.Value
        Next cell
        GetUsers = .keys
    End With
End Function

【讨论】:

  • 我尝试了这段代码,但它所做的只是找到了重复项
  • 这是经过测试的代码。因此,单步执行它并使用即时窗口来查看相关变量值或对象属性是否与您期望的匹配
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多