Copy Specific Column from One Sheet to another based on criteria from another cell

sprague519

New Member
Joined
Dec 29, 2015
Messages
1
I am working on a macro to generate invoices that will be saved off to a folder as PDFs. they data in the invoice is defined by the company, then a few lookups populate the header information (address contact and whatnot). The macro loops through each company and generates a document for each company. the problem i am having is populating the field data. it is all held on a "Spends" sheet where i need to copy some of but not all of the columns for a specific row of data. i need to pull column B,C,E,H,J,K,M. My filter data is in Column N and the filter criteria is in "Submission Form" "C3". I am fairly new to writing VB so the code below is comprised of a bunch of little bits i have pulled from a bunch of random places.


Thanks,
Mike
Code:
Sub Invoice()    Dim VendorList As Range
    Dim Vendor As Range
    Dim VendorSheet As Worksheet
    Dim SubmissionForm As Worksheet
    Dim Quarter As Range
    Dim FilCol1 As Range
    Dim VenQ As String
    Dim Cell As Object
    Dim SubmissionQ As Range
        
    Set VendorList = Worksheets("Vendor Sheet").Range("A2:A19")
    Set VendorSheet = Worksheets("Vendor Sheet")
    Set SubmissionForm = Worksheets("Submission Form")
    Set Quarter = Worksheets("Submission Form").Range("F7")
    Set FilCol1 = Worksheets("Spends").Range("N2:N3000") 'Filteres for the Quarter and Vendor
    Set VenQuar = Worksheets("Submission Form").Range("C3") 'Filteres for the Quarter and Vendor
    
    
' Creates a folder for the current yerar if one does not already exist
    Dim YearDir As String
        YearDir = "C:\Users\sprag\Desktop\Invoice Test\" & Format(Now, "yyyy") & "\"
        If Dir(YearDir, vbDirectory) = "" Then
        MkDir YearDir
        Else
        End If
' Creates a folder for the current Quarter if one does not exist
    Dim QuarterDir As String
        QuarterDir = "C:\Users\sprag\Desktop\Invoice Test\" & Format(Now, "yyyy") & "\Q" & Format(Now, "q") & "\"
        If Dir(QuarterDir, vbDirectory) = "" Then
        MkDir QuarterDir
        Else
        End If
  
    For Each Vendor In VendorList
        Debug.Print Vendor.Value
        SubmissionForm.Range("B3") = Vendor
        
                    
                       
        fName = Vendor.Value & "_Q" & Quarter.Value & "_Submission"
        SubmissionForm.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\sprag\Desktop\Invoice Test\" & Format(Now, "yyyy") & "\Q" & Format(Now, "q") & "\" & fName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    
    Next Vendor
    
    
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Here is a sample of using the Union method to copy non-contiguous cells.
Code:
Sub t()
Dim c As Range, rng As Range
For Each c In Intersect(Range("A:A"), ActiveSheet.UsedRange)
    Set rng = Union(Range("A" & c.Row), Range("B" & c.Row), Range("E" & c.Row))
    rng.Copy Cells(Rows.Count, 2).End(xlUp)(3)
Next
End Sub
So long as the cells to be copied are on the same row or in the same column, the Union method will work. But if rows or columns are staggered to form the range, then the Union method fails and you would have to use a loop in cobination with an array, or similar.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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