Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Sub Test()
  4.  
  5. Dim wbR As Workbook, wbD As Workbook
  6. Dim wsR As Worksheet, wsD As Worksheet
  7. Dim LastrowR As Long, LastrowD As Long, i As Long, CountTimes As Long
  8. Dim PNR As String
  9.  
  10. Set wbR = Workbooks("Recon.xlsm")
  11. Set wbD = Workbooks("DataExt.xlsx")
  12.  
  13. Set wsR = wbR.Worksheets("Sheet1")
  14. Set wsD = wbD.Worksheets("Sheet1")
  15.  
  16. 'Let as assume that PNR column is column A, in Sheet 1, in both workbooks.
  17. 'Also keep in mind that both workbooks should be open and have the following types (Recon -> .xlsm & DataExt -> .xlsx)
  18.  
  19. 'Find the last row of PNR column in Recon File
  20. LastrowR = wsR.Cells(wsR.Rows.Count, "A").End(xlUp).Row
  21. 'Find the last row of PNR column in DataExt File
  22. LastrowD = wsD.Cells(wsD.Rows.Count, "A").End(xlUp).Row
  23. 'Loop PNR column in Recon File
  24. For i = 2 To LastrowR
  25. 'Assign value to PNR
  26. PNR = wsR.Range("A" & i).Value
  27. 'Count how many times PNR appears in column A DataExt file
  28. CountTimes = Application.WorksheetFunction.CountIf(wsD.Range("A2:A" & LastrowD), PNR)
  29.  
  30. 'If appears 1 time
  31. If CountTimes = 1 Then
  32. 'If appears 2 times
  33. ElseIf CountTimes = 2 Then
  34. 'If appears more than 2 times
  35. ElseIf CountTimes > 2 Then
  36. End If
  37.  
  38. Next i
  39.  
  40. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement