I am new to VBA and am trying to create an automatic filing system. I want to copy invoice information directly over from an invoice 'InvoiceMaker' workbook to a separate 'InvoiceTracker' workbook each time an invoice has been filled out. I have a long line of free invoice numbers in the 'Tracker' workbook and i want to assign specific information from each new invoice to each free invoice number. I have been writing to code to automatically search for the invoice number using 'find' in the column B where these free invoice numbers are located. Once this has been found the information in specific cells can be copied over directly to this row of cells.
I have come across a problem with finding the location of the invoice numbers in the external workbook. The error message 91 keeps recurring 'Object Variable or With block variable not set', even though a name has been given an object and set correctly. The find result comes up with 'nothing' even though there is a value in that cell in this external workbook. I changed the format of the cell and it still does not work!
Sub test()
Dim Source As Workbook
Dim Destination As Workbook
Dim Rng As Range
Dim RowNumber As Long
Dim InvoiceNumber As String
'Names the workbooks
Set Source = Workbooks.Open("C:\Desktop\InvoiceMaker.xlsm")
Set Destination = Workbooks.Open("C:\Desktop\InvoiceTrack.xlsx")
'Picks the invoice number
InvoiceNumber = Source.Sheets(1).Range("H9")
'Finds the row of the sheet
The problem stems from below with 'error 91' and I cannot seem to problem solve it! I cannot seem to achieve anything but 'nothing' upon running. All of the sheet locations are correct, I have tried everything!
Set Rng = Destination.Sheets(1).Columns("B:B").Find(What:=InvoiceNumber, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
RowNumber = Rng.Row`
`'Copies the data from the source
Source.Sheets(1).Range("C8").Copy
'Pastes to the destination
Destination.Sheets(1).Cells(RowNumber, 3).PasteSpecial
Source.Sheets(1).Range("H11").Copy
Destination.Sheets(1).Cells(RowNumber, 4).PasteSpecial
Source.Sheets(1).Range("C5").Copy
Destination.Sheets(1).Cells(RowNumber, 5).PasteSpecial
Source.Sheets(1).Range("H10").Copy
Destination.Sheets(1).Cells(RowNumber, 6).PasteSpecial
Source.Save
'Destination.Save
'Destination.Close
End Sub
If someone is kind enough to look over this for me, can you please look at the special paste section and correct me if i am coding incorrectly!
Thanks!