我有2个Excel文件,分别名为Recon File和DataExt File。 这两个文件都有一个名为PNR的列。
我需要检查Recon的Pnr列中的每个值在DataExt的Pnr列中是否具有匹配项。 如果找到匹配项,请检查其中有多少个匹配项。
如果找到0个匹配项,请将“检查”中的“ H”列保留为空白
如果找到1个匹配项,则将与匹配的Pnr单元相对应的D,E,F和G列的单元格中的值复制到D,E,F和G中的侦查文件中。
如果找到2个匹配项,则检查对于第一个和第二个匹配项,Recon中名为Sector的列是否也与DataExt中的Sector列匹配。如果与复制与该列对应的值匹配,则将值复制到“返回”列。
如果找到2个以上匹配项,则将“侦察”列填入“找到2个以上匹配项”。
任何人都可以通过Excel Macros的VBA代码来帮助我。
答案 0 :(得分:1)
jishnu欢迎使用StackOverflow。以下是您要寻找的东西的一个很好的起点。请查看并尝试修改以使您到达想要的地方。
Option Explicit
Sub Test()
Dim wbR As Workbook, wbD As Workbook
Dim wsR As Worksheet, wsD As Worksheet
Dim LastrowR As Long, LastrowD As Long, i As Long, CountTimes As Long
Dim PNR As String
Set wbR = Workbooks("Recon.xlsm")
Set wbD = Workbooks("DataExt.xlsx")
Set wsR = wbR.Worksheets("Sheet1")
Set wsD = wbD.Worksheets("Sheet1")
'Let as assume that PNR column is column A, in Sheet 1, in both workbooks.
'Also keep in mind that both workbooks should be open and have the following types (Recon -> .xlsm & DataExt -> .xlsx)
'Find the last row of PNR column in Recon File
LastrowR = wsR.Cells(wsR.Rows.Count, "A").End(xlUp).Row
'Find the last row of PNR column in DataExt File
LastrowD = wsD.Cells(wsD.Rows.Count, "A").End(xlUp).Row
'Loop PNR column in Recon File
For i = 2 To LastrowR
'Assign value to PNR
PNR = wsR.Range("A" & i).Value
'Count how many times PNR appears in column A DataExt file
CountTimes = Application.WorksheetFunction.CountIf(wsD.Range("A2:A" & LastrowD), PNR)
'If appears 1 time
If CountTimes = 1 Then
'If appears 2 times
ElseIf CountTimes = 2 Then
'If appears more than 2 times
ElseIf CountTimes > 2 Then
End If
Next i
End Sub