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