Guest User

Untitled

a guest
Feb 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. Option explicit
  2.  
  3. Sub GeneratePDF()
  4.  
  5. Dim oWs As Worksheet
  6. Dim wbA As Workbook
  7. Dim rSearchRng As Range
  8. Dim PDFSearchRng As Range
  9. Dim lEndNum As Long
  10. Dim vFindVar As Variant
  11. Dim loc As Range
  12. Dim supl As Range
  13. Dim Supplier As Range
  14. Dim LastRow As Long
  15. Dim LRow As Long
  16. Dim Copy As Range
  17. Dim firstaddress As String
  18. Dim secondaddress As String
  19. Dim strTime As String
  20. Dim strName As String
  21. Dim strPath As String
  22. Dim strFile As String
  23. Dim strPathFile As String
  24.  
  25.  
  26.  
  27. With Application
  28. Application.ScreenUpdating = False
  29. End With
  30.  
  31. If Sheets("Sheet1").Range("O2") = "" Then
  32. MsgBox "You must assign a Date (dd-mm-yyyy) in cell O2"
  33. Range("O2").Select
  34. Exit Sub
  35. Else
  36.  
  37.  
  38. strTime = Format(Sheets("Sheet1").Range("O2"), "DD-MM-YYYY")
  39. Set wbA = ActiveWorkbook
  40. strPath = wbA.Path
  41. If strPath = "" Then
  42. strPath = Application.DefaultFilePath
  43. End If
  44. strPath = strPath & ""
  45. strName = "Orders Pending"
  46. strFile = strName & "_" & strTime & ".pdf"
  47. strPathFile = strPath & strFile
  48.  
  49. Sheets("Sheet2").Visible = True
  50. Set oWs = ActiveWorkbook.Worksheets("Sheet1")
  51. LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
  52.  
  53. lEndNum = oWs.Range("A2").End(xlDown).Row
  54. Set Copy = oWs.Range("A2" & LRow)
  55. Set rSearchRng = oWs.Range("A2:A" & CStr(lEndNum))
  56. Set PDFSearchRng = Sheets("Sheet2").Range("C2:C" & CStr(lEndNum))
  57.  
  58. Set loc = rSearchRng.Cells.Find(Range("O2").Value)
  59.  
  60. If Not loc Is Nothing Then
  61. firstaddress = loc.Address
  62.  
  63. Do
  64.  
  65. loc.Select
  66. Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 12)).Select
  67. Selection.Copy
  68. Sheets("Sheet2").Select
  69. LastRow = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
  70. Range("A" & LastRow).Select
  71. Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
  72. SkipBlanks:=False, Transpose:=False
  73. ActiveSheet.Paste
  74. Sheets("Sheet1").Select
  75. Application.CutCopyMode = False
  76. Set loc = rSearchRng.FindNext(loc)
  77. Loop Until loc.Address = firstaddress
  78. End If
  79.  
  80. Set loc = Nothing
  81.  
  82. Set supl = PDFSearchRng.Cells.Find(Sheets("sheet1").Range("P2").Value)
  83.  
  84. If supl Is Nothing Then
  85. secondaddress = supl.Address
  86. Do
  87.  
  88. Sheets("Sheet2").Select
  89. supl.Select
  90. Range(ActiveCell.Offset(0, -2), ActiveCell.Offset(0, 10)).Select
  91. Selection.Delete Shift:=xlUp
  92. Set supl = PDFSearchRng.FindNext(supl)
  93. Loop Until supl.Address = secondaddress
  94. End If
  95.  
  96. Sheets("Sheet2").Activate
  97. Sheets("Sheet2").Range("A1").Select
  98. Sheets("Sheet2").Range(Selection, Selection.End(xlToRight)).Select
  99. Sheets("Sheet2").Range(Selection, Selection.End(xlDown)).Select
  100. ActiveSheet.PageSetup.PrintArea = Range("A1:M1",
  101. Range("M65536").End(xlUp)).Address
  102. ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPathFile,
  103. Quality:= _
  104. xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
  105. OpenAfterPublish:=False
  106. Range("A2").Select
  107. Range(Selection, Selection.End(xlToRight)).Select
  108. Range(Selection, Selection.End(xlDown)).Select
  109. Selection.Delete Shift:=xlUp
  110. ActiveWindow.SelectedSheets.Visible = False
  111. Sheets("Sheet1").Activate
  112. Range("A1").Select
  113.  
  114. With Application
  115. Application.ScreenUpdating = True
  116. End With
  117. Sheets("Sheet1").Range("O2").Select
  118. Selection.ClearContents
  119. Sheets("Sheet1").Range("A2").Select
  120. MsgBox "Complete"
  121. End If
  122. End Sub
Add Comment
Please, Sign In to add comment