Copy Cell on Based on Criteria, Paste in Different Sheet, Save as PDF, Loop

shaelyntai

New Member
Joined
Oct 9, 2020
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hello!

I'm pretty new to Excel Macros but absolutely love them (very recently learned how to save as a PDF using the value in a cell, small wins!)

I have a pretty big ask I'm hoping someone can help with:

Essentially looking to have the values of invoice ids (column A, Sheet1) be copied into my invoice template (D2, Sheet2) [where a vlookup takes place], only if Billing = "No" (column K, Sheet1) , and then save as a pdf (name based on B5 and B12 in Sheet2). I would like this to loop through all of the rows where Column K in Sheet1 has "No"

Appreciate any assistance or insights if this would be possible, thank you!
 

Attachments

  • Screenshot 2020-10-09 133918.png
    Screenshot 2020-10-09 133918.png
    29.4 KB · Views: 13
  • Screenshot 2020-10-09 133946.png
    Screenshot 2020-10-09 133946.png
    65.7 KB · Views: 12

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
VBA Code:
Sub CopyValues()
Dim LastCell As Integer
Dim mySheet1 As Worksheet
Dim mySheet2 As Worksheet
Dim fileName As String
Dim filePath As String
filePath = "C:\Invoice\"
Set mySheet1 = Sheets("Sheet1")
Set mySheet2 = Sheets("Sheet2")
LastCell = mySheet1.Cells(Rows.Count, "K").End(xlUp).Row
For i = 1 To LastCell
    If mySheet1.Cells(i, "K").Value = "No" Then
    With mySheet2
    .Range("D2").Value = mySheet1.Range("A" & i).Value
    fileName = .Range("B5").Value & " " & .Range("B12").Value
    .ExportAsFixedFormat Type:=xlTypePDF, fileName:=filePath & fileName & ".pdf"
    End With
    End If
Next i
End Sub
 
Upvote 0
Thank you so much yinkajewole! This works fantastic and a great framework to work with, I truly appreciate it!
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top