0

可能重复:
用于匹配和排列行的 Excel 宏

我有一个 Excel 工作簿,其中包含 2 张具有相同列集但数据以不同方式排列的工作表。我需要使用一个或多个 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 在两个工作表中都匹配,则比较位置 -> 如果匹配,则将用户 ID 的记录存储为位置匹配,如果不报告为特定的用户 ID 不匹配。,

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

4

2 回答 2

0

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

于 2012-07-01T21:28:22.290 回答
0

我总是使用一种方法,它是硬编码的,您可以根据您的示例对其进行优化。子 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

我希望它有所帮助。

于 2012-07-02T15:35:13.397 回答