Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option explicit
- Sub GeneratePDF()
- Dim oWs As Worksheet
- Dim wbA As Workbook
- Dim rSearchRng As Range
- Dim PDFSearchRng As Range
- Dim lEndNum As Long
- Dim vFindVar As Variant
- Dim loc As Range
- Dim supl As Range
- Dim Supplier As Range
- Dim LastRow As Long
- Dim LRow As Long
- Dim Copy As Range
- Dim firstaddress As String
- Dim secondaddress As String
- Dim strTime As String
- Dim strName As String
- Dim strPath As String
- Dim strFile As String
- Dim strPathFile As String
- With Application
- Application.ScreenUpdating = False
- End With
- If Sheets("Sheet1").Range("O2") = "" Then
- MsgBox "You must assign a Date (dd-mm-yyyy) in cell O2"
- Range("O2").Select
- Exit Sub
- Else
- strTime = Format(Sheets("Sheet1").Range("O2"), "DD-MM-YYYY")
- Set wbA = ActiveWorkbook
- strPath = wbA.Path
- If strPath = "" Then
- strPath = Application.DefaultFilePath
- End If
- strPath = strPath & ""
- strName = "Orders Pending"
- strFile = strName & "_" & strTime & ".pdf"
- strPathFile = strPath & strFile
- Sheets("Sheet2").Visible = True
- Set oWs = ActiveWorkbook.Worksheets("Sheet1")
- LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
- lEndNum = oWs.Range("A2").End(xlDown).Row
- Set Copy = oWs.Range("A2" & LRow)
- Set rSearchRng = oWs.Range("A2:A" & CStr(lEndNum))
- Set PDFSearchRng = Sheets("Sheet2").Range("C2:C" & CStr(lEndNum))
- Set loc = rSearchRng.Cells.Find(Range("O2").Value)
- If Not loc Is Nothing Then
- firstaddress = loc.Address
- Do
- loc.Select
- Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, 12)).Select
- Selection.Copy
- Sheets("Sheet2").Select
- LastRow = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
- Range("A" & LastRow).Select
- Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
- SkipBlanks:=False, Transpose:=False
- ActiveSheet.Paste
- Sheets("Sheet1").Select
- Application.CutCopyMode = False
- Set loc = rSearchRng.FindNext(loc)
- Loop Until loc.Address = firstaddress
- End If
- Set loc = Nothing
- Set supl = PDFSearchRng.Cells.Find(Sheets("sheet1").Range("P2").Value)
- If supl Is Nothing Then
- secondaddress = supl.Address
- Do
- Sheets("Sheet2").Select
- supl.Select
- Range(ActiveCell.Offset(0, -2), ActiveCell.Offset(0, 10)).Select
- Selection.Delete Shift:=xlUp
- Set supl = PDFSearchRng.FindNext(supl)
- Loop Until supl.Address = secondaddress
- End If
- Sheets("Sheet2").Activate
- Sheets("Sheet2").Range("A1").Select
- Sheets("Sheet2").Range(Selection, Selection.End(xlToRight)).Select
- Sheets("Sheet2").Range(Selection, Selection.End(xlDown)).Select
- ActiveSheet.PageSetup.PrintArea = Range("A1:M1",
- Range("M65536").End(xlUp)).Address
- ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPathFile,
- Quality:= _
- xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
- OpenAfterPublish:=False
- Range("A2").Select
- Range(Selection, Selection.End(xlToRight)).Select
- Range(Selection, Selection.End(xlDown)).Select
- Selection.Delete Shift:=xlUp
- ActiveWindow.SelectedSheets.Visible = False
- Sheets("Sheet1").Activate
- Range("A1").Select
- With Application
- Application.ScreenUpdating = True
- End With
- Sheets("Sheet1").Range("O2").Select
- Selection.ClearContents
- Sheets("Sheet1").Range("A2").Select
- MsgBox "Complete"
- End If
- End Sub
Add Comment
Please, Sign In to add comment