Create invoice automatcally

towners

Board Regular
Joined
Mar 12, 2009
Messages
225
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I'd like to be able to create an invoice automatically using a workbook that has invoice blanks for each person and a data table with details of all people and invoices.

I've started the code to select the relevant worksheets and insert the specified number of rows based on the occurences of a person and date match.

What I can't work out is how to match the rows of data and populate the invoice lines. i.e if there are 3 occurence of name and date I can insert three rows into the relevant tab but don't know how to select the three rows of data from the source and populate the invoice.

I've pasted psuedo examples of what I am trying to achieve and the code I have so far if anyone can lend a hand.

Invoice tab:

Excel Workbook
AB
1Date14/02/2011
2Details
3A100
4D130
Tom
Excel Workbook
ABCD
1NameDateCustomerValue
2Tom14/02/2011A100
3****13/02/2011B110
4Harry13/02/2011C120
5Tom14/02/2011D130
6****14/02/2011E140
7Harry14/02/2011F150
8****14/02/2011G160
Excel 2010 Details is a named range, in my code it is actually "Canx_and_retentions_Subtotal" Data tab Data

Excel 2010



Code:
Option Explicit
Private Sub makeinvoice()
Dim i As Integer
Dim nRow As Integer
Dim wb As String
Dim ws As Worksheet
Dim iName As String
Dim iDate As String
Dim sText As String
Dim rng As Range
For Each ws In Worksheets
    If Left(ws.Name, 3) = "Inv" Then
        iName = Right(ws.Name, (Len(ws.Name) - 10))
        iDate = Worksheets("Input").Range("B3")
        nRow = Application.WorksheetFunction.CountIf(Worksheets("CRM Order Data").Range("AK:AK"), iName & iDate)
                Range("Canx_and_retentions_Subtotal").Select
                ActiveCell.Offset(2, 0).Resize(nRow).EntireRow.Insert
Else
End If
Next ws
End Sub

Appreciate any guidance, I know the code doesn't exactly relate to my example posted (it relates to my actual sheets), the example shows what I want to achieve.

Thanks

Paul
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You could AutoFilter your Data for Name and Date and copy the visible cells (SpecialCells(xlCellTypeVisible)).
 
Upvote 0
Thank you Andrew, I've googled it to find the context/syntax but I'm strugling to work it out. Could you expand?

Many thanks

Paul
 
Upvote 0
Thanks Andrew,

Not sure I understand what he's doing with the offsets...

Here's my code so far, I get an error "PasteSpecial method of Range class failed" when I attempt to paste the filtered values.

Although my spreadsheet has 37 columns I only want the data from the first few columns to be copied and pasted

Any help much appreciated

Code:
Option Explicit
Private Sub makeinvoice()
Dim i As Integer
Dim nRow As Integer
Dim wb As String
Dim ws As Worksheet
Dim iName As String
Dim sName As String
Dim iDate As String
Dim sText As String
Dim rng As String
 
For Each ws In Worksheets
    If Left(ws.Name, 3) = "Inv" Then
        Sheets(ws.Name).Activate
        iName = Right(ws.Name, (Len(ws.Name) - 10))
        iDate = Worksheets("Input").Range("B3")
        nRow = Application.WorksheetFunction.CountIf(Worksheets("CRM Order Data").Range("AK:AK"), iName & iDate)
            If nRow > 0 Then
                sName = Replace(iName, " ", "")
                Worksheets("CRM Order Data").Range("$B$1:$G$1484").AutoFilter Field:=37, Criteria1:=(iName & iDate)
                    With Sheets("CRM Order Data").AutoFilter.Range
                        .SpecialCells(xlCellTypeVisible).Copy
                        rng = sName & "Sect1"
                    End With
                            With ws
                                .Range(rng).Select
                                ActiveCell.Offset(2, 0).Resize(nRow).EntireRow.Insert
                                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                :=False, Transpose:=False
                            End With
            End If
    End If
Next ws
End Sub


Thanks again
Paul
 
Upvote 0
Is there anything on the clipboard immediately before the PasteSpecial? I think you will find that Insert is doing the copy. Which columns from the AutoFilter Range B:G do you want to copy?
 
Upvote 0
Hi Andrew,

Thanks for the reply.

I've modifed my code based on the link you sent me and I can paste the autfilter selection. My problem is that the whole row of 37 columns is pasted and I only want the data in columns B:G pasted. As you can see i made the range B:G in the autofilter but that doesn't seem to be the answer...

Code:
Option Explicit
Private Sub makeinvoice()
Dim i As Integer
Dim nRow As Integer
Dim wb As String
Dim ws As Worksheet
Dim iName As String
Dim sName As String
Dim iDate As String
Dim sText As String
Dim rng As String
Dim rng2 As Range
Dim rng3 As Range
For Each ws In Worksheets
    If Left(ws.Name, 3) = "Inv" Then
        Sheets(ws.Name).Activate
        iName = Right(ws.Name, (Len(ws.Name) - 10))
        iDate = Worksheets("Input").Range("B3")
        nRow = Application.WorksheetFunction.CountIf(Worksheets("CRM Order Data").Range("AK:AK"), iName & iDate)
            If nRow > 0 Then
                Worksheets("CRM Order Data").Range("$A$1:$G$999999").AutoFilter Field:=37, Criteria1:=(iName & iDate)
                    With Sheets("CRM Order Data").AutoFilter.Range
                        Set rng2 = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
                        .SpecialCells(xlCellTypeVisible)
                    End With
 
                    Worksheets(ws.Name).Activate
                    sName = Replace(iName, " ", "")
                    rng = sName & "Sect1"
                    Set rng3 = Sheets("CRM Order Data").AutoFilter.Range
                    rng3.Offset(1, 0).Resize(rng3.Rows.Count - 1).Copy _
                    Destination:=Worksheets(ws.Name).Range(rng)
 
            End If
    End If
Next ws
End Sub

Thank you again

Paul
 
Upvote 0
Instead of:

Code:
With Sheets("CRM Order Data").AutoFilter.Range

try:

Code:
With Worksheets("CRM Order Data").Range("$A$1:$G$999999")
 
Upvote 0
Thanks, I've changed that but still pasting the whole row...
 
Upvote 0
Looking again I think it's this that needs changing:

Code:
Set rng3 = Sheets("CRM Order Data").AutoFilter.Range
rng3.Offset(1, 0).Resize(rng3.Rows.Count - 1).Copy _
    Destination:=Worksheets(ws.Name).Range(rng)

to:

Code:
Set rng3 = Worksheets("CRM Order Data").Range("$A$1:$G$999999")
rng3.Offset(1, 0).Resize(rng3.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy _
    Destination:=Worksheets(ws.Name).Range(rng)
 
Upvote 0

Forum statistics

Threads
1,213,539
Messages
6,114,221
Members
448,554
Latest member
Gleisner2

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