Excel数据比较的最佳方法有两张

时间:2012-07-01 17:19:42

标签: excel vba excel-vba

  

可能重复:
  Excel macro to match and lineup rows

我有一个带有2个Sheets的Excel工作簿,这些工作簿具有相同的列集,但数据以不同的方式排列。我需要使用一个或多个Key列来比较Sheet 2和Sheet 1,并识别不匹配的记录。

sheet1中的数据:

UserId Name Salary DeptId DeptName Location
1      Loga 2000   1      HR       Chennai
2      Mano 1500   2      PM       Mumbai
3      Raj  2500   5      GM       Delhi

sheet1中的数据:

UserId Name Salary DeptId DeptName Location
2      Mano 1500   2      PM       Mumbai
3      Raj  2500   5      GM       Delhi

首先我需要匹配基于UserId和DeptId的记录,如果两个表中的匹配比较薪水 - >如果薪水匹配将记录与UserId存储为薪资匹配。 类似地,如果UserId和DeptId在两个工作表中匹配,则比较位置 - >如果匹配,则将记录与userid存储为匹配的位置,如果不报告为不匹配的特定UserID。

我打算在VBA宏中使用HLookUp进行比较,但是当行数没有增加时,这似乎是漫长的过程,也会降低性能。 有什么建议吗?

2 个答案:

答案 0 :(得分:0)

我看到多种解决方案。最简单但不是最干净的解决方案是使用UserId和Dept-Id创建密钥。简单的连接可以完成这项工作。例如,添加一个G列(假设UserId在A中,依此类推..),您可以这样做:=$A2 & "-" & $D2然后使用一个简单的查找函数来查看您创建的唯一ID是否存在于两个工作表中,例如:=IF(ISERROR(VLOOKUP($G2, Sheet2!$G$2:$G$3, 1, 0)), 0, 1)(对不起,如果函数名称不正确,我正在翻译法语)。然后用这些数据做任何你想做的事。

答案 1 :(得分:0)

我总是使用一种方法,它的硬编码可以根据你的例子进行优化。 Sub findMatch()

  Dim i As Integer
  Dim j As Integer
  Dim UserID As Integer
  Dim UserID2 As Integer
  Dim DeptID As Integer
  Dim DeptID2 As Integer
  Dim sal As Integer
  Dim Salary2 As Integer
  Dim Location As String
  Dim Location2 As String

  Dim rows1 as Integer
  Dim rows2 as Integer

  rows1=Worksheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row 'rows count in sheet1
  rows2=Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Row 'rows count in sheet2

    For i = 1 To rows1
        UserID = Workbooks("yourWorkBook").Worksheets("sheet1").Cells(i, "A").Value)
        'yourWorkBook is the name of the Access document
        DeptID = Workbooks("yourWorkBook").Worksheets("sheet1").Cells(i, "D").Value)
        Salary = Workbooks("yourWorkBook").Worksheets("sheet1").Cells(i, "C").Value)
        Location  = Workbooks("yourWorkBook").Worksheets("sheet1").Cells(i, "F").Value)

        for j= 1 to rows2
            UserID2 = Workbooks("yourWorkBook").Worksheets("sheet2").Cells(j, "A").Value)
            DeptID2 = Workbooks("yourWorkBook").Worksheets("sheet2").Cells(j, "D").Value)
            Salary2 = Workbooks("yourWorkBook").Worksheets("sheet2").Cells(j, "C").Value)
            Location2 = Workbooks("yourWorkBook").Worksheets("sheet2").Cells(j, "F").Value)

            If (UserID=UserID2) and (DeptID=DeptID2) Then

                If Salary=Salary2 then 'userID, DeptID and Salary match
                    'you create manually another sheet (sheet3) in wich you will store the desired data
                    lstSalRow = Worksheets("sheet3").Cells(Rows.Count, "A").End(xlUp).Row 'Getting the count of rows in the sheet
                    'inserting after the last row
                    Worksheets("sheet3").Cells(lstSalRow+1, "A").Value=UserID  'Storing UserID
                    Worksheets("sheet3").Cells(lstSalRow+1, "B").Value=Salary  'Storing salary

                Elseif strcmp(Location, Location2)=0 then  'userID, deptID and Location match

                    'Location matched : you can create another sheet (sheet4) in wich you will store the desired data
                    lstLocRow = Worksheets("sheet4").Cells(Rows.Count, "A").End(xlUp).Row
                    Worksheets("sheet4").Cells(lstLocRow+1, "A").Value=UserID 
                    Worksheets("sheet4").Cells(lstLocRow+1, "B").Value=Location

                Else 'only userID and DeptID match
                    'do other actions
                End If

            End If

        next j

    next i

End Sub

我希望它有所帮助。