使用vba比较一个excel表中的4列

时间:2016-03-23 13:25:39

标签: excel vba excel-vba excel-formula

我需要你的帮助,我在excel表中有4列,我需要比较它们2比2我会向你解释:

在A栏中我有用户(user1,user2,user3 ......) 在B列中,我有功能(fonc1,fonc2,fonc3 .....)

在C栏中我有用户(user1,user2,user3 ......) 在列D中,我有功能(fonc1,fonc2,fonc3 .....)

列C和D是C和D列中A列和B列的新版本,用户可以更改顺序或更改功能。

当我执行我的代码时,我将结果放在其他新列中: 列F我有用户 列G我把Deleted_functionalities放在哪里 列H我把New_functionalities

第一个问题是代码不会让用户只获得新的和删除的功能。第二个问题是,当列A超过列C用户库存时,代码不起作用。你能帮我找一个解决方案吗?先感谢您 。 这是我的代码和我正在处理的文件:

Private Sub CommandButton2_Click()
  Dim rngCell As Range
    For Each rngCell In Range("B2:B2000")
        If WorksheetFunction.CountIf(Range("D2:D2000"), rngCell) = 0 Then
            Range("G" & Rows.Count).End(xlUp).Offset(1) = rngCell
        End If
    Next
    For Each rngCell In Range("D2:D2000")
        If WorksheetFunction.CountIf(Range("B2:B2000"), rngCell) = 0 Then
            Range("H" & Rows.Count).End(xlUp).Offset(1) = rngCell
        End If
    Next
End Sub

这是excel文件 http://www.cjoint.com/c/FCxnwjp22rv

2 个答案:

答案 0 :(得分:0)

我希望得到你想要达到的目标。以下是否解决了您的问题?

 Private Sub CommandButton2_Click()
  Dim rngCell As Range
    For Each rngCell In Range("A2:A20000")
        If WorksheetFunction.CountIf(Range("C2:C20000"), rngCell) > 0 Then
            Range("F" & Rows.Count).End(xlUp).Offset(1) = rngCell
            Range("F" & Rows.Count).End(xlUp).Offset(0, 1) = rngCell.Offset(0, 1).Value
            Range("F" & Rows.Count).End(xlUp).Offset(0, 2) = Application.WorksheetFunction.VLookup(rngCell.Value, Range("C2:D20000"), 2, 0)
        ElseIf (rngCell <> "") Then

            Range("F" & Rows.Count).End(xlUp).Offset(1) = rngCell
            Range("F" & Rows.Count).End(xlUp).Offset(0, 1) = rngCell.Offset(0, 1).Value

        End If
    Next

    For Each rngCell In Range("C2:C20000")
        If (WorksheetFunction.CountIf(Range("A2:A20000"), rngCell) = 0 And rngCell <> "") Then
              Range("F" & Rows.Count).End(xlUp).Offset(1) = rngCell
              Range("F" & Rows.Count).End(xlUp).Offset(0, 2) = rngCell.Offset(0, 1).Value
        End If
    Next


End Sub

当用户出现在A列和C列中时,用户仅包含在F列中。如果您想要包含A列或C列中的每个用户,则必须更改代码。

答案 1 :(得分:0)

试试这个

Private Sub CommandButton2_Click()
Dim ws As Worksheet
Dim cell As Range, funcCell As Range
Dim oldUserRng As Range, newUserRng As Range, reportRng As Range
Dim iReport As Long
Dim oldFunc As String, newFunc As String

Set ws = ThisWorkbook.Worksheets("users") '<== adapt it to your needs
With ws
    Set oldUserRng = .Columns(1).Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeConstants, xlTextValues)
    Set newUserRng = .Columns(3).Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeConstants, xlTextValues)
    Set reportRng = .Range("F1:I1") '<== added one report column to account for unchanged functions
End With

reportRng.Value = Array("user", "deleted", "new", "same")
iReport = 1
For Each cell In oldUserRng
    With cell
        oldFunc = .Offset(, 1).Value
        Set funcCell = FindAndOffset(newUserRng, .Value, 1)
        If funcCell Is Nothing Then
            reportRng.Offset(iReport) = Array(.Value, "", "", oldFunc)
        Else
            newFunc = funcCell.Value
            If newFunc = oldFunc Then
                reportRng.Offset(iReport) = Array(.Value, "", "", newFunc)
            Else
                reportRng.Offset(iReport) = Array(.Value, oldFunc, newFunc, "")
            End If
        End If
        iReport = iReport + 1
    End With
Next cell

For Each cell In newUserRng
    With cell
        Set funcCell = FindAndOffset(oldUserRng, .Value, 1)
        If funcCell Is Nothing Then
            reportRng.Offset(iReport) = Array(.Value, "", .Offset(, 1).Value, "")
            iReport = iReport + 1
        End If
    End With
Next cell

End Sub

不太确定它能满足您的需求。 你最好在&#34;之前提供&#34;的截图。 &#34;&#34;&#34;&#34;场景。 顺便说一句,可以安全地假设旧用户列和新用户列都不能重复(即:两个或更多&#34; userX&#34;在A列和/或C列中?)

但它确实加速了事情,因为它只通过非空单元格进行迭代。