Advertisement
YasserKhalil2019

T4252_Transfer To Another Workbook Prevent Duplicates

Nov 3rd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. https://excel-egy.com/forum/t4252
  2. ---------------------------------
  3.  
  4. Sub Transfer_To_Another_Workbook_Prevent_Duplicates()
  5. Dim wb As Workbook, ws As Worksheet, nextRow As Long, lastRow As Long
  6.  
  7. Const wbName As String = "الصادر"
  8. Const source As String = "قاعدة البيانات"
  9. Const target As String = "الصادر العام"
  10. Const shCert As String = "الشهادة"
  11.  
  12. With Application
  13. .ScreenUpdating = False: .DisplayAlerts = False: .AskToUpdateLinks = False
  14. End With
  15. On Error Resume Next
  16. Set wb = Workbooks(wbName & ".xlsm")
  17. Set ws = ThisWorkbook.Sheets(source)
  18. nextRow = ws.ListObjects(1).Range.Columns(3).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  19.  
  20. If wb Is Nothing Then
  21. Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & wbName & ".xlsm")
  22. Else
  23. wb.Activate
  24. End If
  25. On Error GoTo 0
  26.  
  27. With wb.Sheets(target)
  28. If Application.WorksheetFunction.CountIf(.ListObjects(1).ListColumns(6).DataBodyRange, ws.Range("M" & nextRow).Value) >= 1 Then MsgBox "Duplicate", vbExclamation: GoTo Skipper
  29. lastRow = .ListObjects(1).Range.Columns(3).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
  30. .Range("C" & lastRow).Value = Format(Date, "yyyy/mm/dd")
  31. .Range("D" & lastRow).Value = "طلاب"
  32. .Range("E" & lastRow).Value = ws.Range("E" & nextRow).Value
  33. .Range("G" & lastRow).Value = ws.Range("M" & nextRow).Value
  34. .Range("C" & lastRow).Select
  35. ActiveWorkbook.Save
  36. Windows(ThisWorkbook.Name).Activate
  37. End With
  38.  
  39. Sheets(shCert).Select
  40. ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,,,TRUE,,FALSE)"
  41. Sheets(source).Select
  42. Range("C" & [C2].End(xlDown).Row + 1).Select
  43. ActiveWorkbook.Save
  44. Skipper:
  45. With Application
  46. .ScreenUpdating = True: .DisplayAlerts = True: .AskToUpdateLinks = True
  47. End With
  48. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement