比较来自两个不同工作表的值

时间:2015-02-05 20:19:28

标签: excel vba excel-vba

所以我有两个不同的工作表,其中包含一个包含服务订单的列。基本上我想要做的是,如果Worksheet1中的SO-ID列中的数字与Worksheet2中的SO#列匹配,那么我想复制那些相应的列," Order"和"键入",与工作表2中的特定服务订单号相关联到工作表1。任何帮助将非常感谢。我在下面展示了一个例子,但是显示的数据点比显示的要多得多:

   Worksheet1 named "In Progress":

    Region  SO-ID   Site ID
    C01    5818476  DNYF8766D
    E01    5975844  DCD00139
    E01    5446828  DEG02219

.
.
.
.
.


Worksheet B named "0205":

No. SO#             Order     Type
1   5446828       KMC07570    Re-Install
2   5975844       KSS10269A   Install
3   5818476        KSS10349D    Re-Install
.
.
.
.


Results:

In Worksheet1 named "In Progress":

Region  SO-ID   Site ID     Order     Type
C01    5818476  DNYF8766D   KSS10349D   Re-Install
E01    5975844  DCD00139    KSS10269A     Install
E01    5446828  DEG02219    KMC07570      Re-Install
.
.
.
.
.

这是我写的代码,但它不起作用

Sub DCompare()
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Dim i As Long


For i = LR To 2 Step -1
If ThisWorkbook.Sheets("Sheet1").Range("B" & i) = ThisWorkbook.Sheets("Sheet2").Range("B" & i) Then
Worksheets("Sheet2").Cells(i, 8).Value = Worksheets("Sheet2").Cells(i, 3).Value
Worksheets("Sheet2").Cells(i, 9).Value = Worksheets("Sheet2").Cells(i, 4).Value
End If
Next i


End Sub

2 个答案:

答案 0 :(得分:0)

在接受匹配之前,您所进行的每个比较似乎都必须按行匹配。最好搜索匹配的目标并记录用于传输数据的行。

Sub DCompare()
    Dim LR As Long, i As Long, t As Long, ws2 As Worksheet

    With ThisWorkbook
        Set ws2 = .Sheets("Sheet2")
        With .Sheets("Sheet1")
            LR = .Range("A" & Rows.Count).End(xlUp).Row
            For i = LR To 2 Step -1
                If CBool(Application.CountIf(ws2.Columns("B"), .Cells(i, "B").Value2)) Then
                    t = Application.Match(.Cells(i, "B").Value2, ws2.Columns("B"), 0)
                    ws2.Cells(t, 8).Resize(1, 2) = .Cells(i, 3).Resize(1, 2).Value
                End If
            Next i
        End With
    End With

    Set ws2 = Nothing
End Sub

查找匹配项,如果找到匹配项,则会捕获目标匹配行并传输两个单元格的信息。我很好奇为什么你虽然有必要从底层开始工作但我想可能存在未公开的信息层次结构。如果披露倒退的原因,可能更容易避免转移数据。

答案 1 :(得分:0)

第二个例子。使用一个简单的.Find on Worksheet B.包含一些变量,以便在每张工作表上定位数据,命名工作表等,这可以为您提供一些自定义的灵活性?

Option Explicit

Sub subFindSOID()
Dim wsSO As Worksheet, wsOrd As Worksheet
Dim SORng As Range, OrdRng As Range, fndSO As Range, c As Range
Dim SOStRow As Long, SOEndRow As Long, SOCol As Long, SOff As Long
Dim OrdStRow As Long, OrdEndRow As Long, OrdCol As Long

'Assign worksheets
Set wsSO = Worksheets("In Progress")
Set wsOrd = Worksheets("0205")

'Assign data start position on each sheet
SOStRow = 3
SOCol = 2

OrdStRow = 3
OrdCol = 2
SOff = 1

    'Get search range
    With wsOrd
        OrdEndRow = .Cells(Rows.Count, OrdCol).End(xlUp).Row
        Set OrdRng = .Range(.Cells(OrdStRow, OrdCol), .Cells(OrdEndRow, OrdCol).Offset(0, SOff + 2))
    End With

    With wsSO
        'Get range to iterate
        SOEndRow = .Cells(Rows.Count, SOCol).End(xlUp).Row
        Set SORng = .Range(.Cells(SOStRow, SOCol), .Cells(SOEndRow, SOCol))
            'Iterate the range
            For Each c In SORng.Offset(0, 1)
                Set fndSO = OrdRng.Find(What:=c, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
                If Not fndSO Is Nothing Then
                    'Copy the data columns
                    c.Offset(0, 2).Value = fndSO.Offset(0, SOff).Value
                    c.Offset(0, 3).Value = fndSO.Offset(0, SOff + 1).Value
                End If
            Next c
    End With
End Sub