OK I was struggling to find an answer to this one. I have a list of customers in an excel sheet (downloaded from my accounting software) and I have another excel sheet with today's invoices (each invoice is on a different tab, also downloaded from my accounting software(as PDF then converted to excel)) What I do is use VBA to scrape the data from each invoice and paste it into another excel file (to explain reasoning would take ages). What I am currently doing is finding "invoice to" on the sheet then off setting it by 1,0 as I know that is where the customers name is,
Set customer = Cells.Find(what:="INVOICE TO").Offset(1, 0)
but recently they(accounting software) changed "Invoice to" to "Bill to" I have worked around that with an error handler then underneath just used the same code with "Bill to" and that works.
What I would like to do is use the list of customers to find and select the customer name from that tab. can anyone help me with this? here is the whole code (well the main part that is needed) p.s. I am new to programming and newer still to VBA so this may not look good and have lots of terrible practices
Sub scappingData()
On Error Resume Next
Dim fileName As Variant
Dim folderName As String
Dim wsDest As Worksheet
Dim x As Long
Dim customer As Object
Dim firstItem As Object
Dim lastItem As Object
Dim columnItem As Long
Dim firstQTY As Object
Dim lastQTY As Object
Dim ColumnQTY As Long
Dim invoiceDate As Object
Dim LoginName As String
Dim BillTo As Object
'open the destination folder
LoginName = Environ("Username")
Workbooks.Open "C:\Users\" & LoginName & "\Google Drive\Returns\" & "ReturnsData.xlsm"
'set variables
fileName = Dir("C:\Users\" & LoginName & "\Google Drive\sales\")
folderName = "C:\Users\" & LoginName & "\Google Drive\sales\"
Set wsDest = Workbooks("ReturnsData.xlsm").Worksheets("OrderedData")
'Loop through all files in a folder
While fileName <> ""
x = 0
Workbooks.Open folderName & fileName
'consolidate worksheets for those who have a 2 page delivery note
For Each Worksheet In Worksheets
x = x + 1
Workbooks(fileName).Worksheets(x).Activate
If Range("A1") = "ACTIVITY" Then
Range(Cells.Find(what:="ACTIVITY").Offset(1, 0),
Cells.Find(what:="ACTIVITY").End(xlDown)).Copy _
Worksheets(x - 1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Range(Cells.Find(what:="QTY").Offset(1, 0), Cells.Find(what:="QTY").End(xlDown)).Copy _
Worksheets(x - 1).Range("E" & Rows.Count).End(xlUp).Offset(1, 0)
End If
Next
x = 0
LastItemPrevious = ""
For Each Worksheet In Worksheets
x = x + 1
Workbooks(fileName).Worksheets(x).Activate
If Range("A1") = "D&A Foods Ltd" Then
'copy and paste products
Set firstItem = Cells.Find(what:="ACTIVITY").Offset(1, 0)
Set lastItem = Cells.Find(what:="ACTIVITY").End(xlDown)
columnItem = wsDest.Cells(Rows.Count, "C").End(xlUp).Row
Range(firstItem, lastItem).Copy wsDest.Range("C" & columnItem).Offset(1)
'copy,paste, fill name
Set customer = Cells.Find(what:="INVOICE TO").Offset(1, 0)
Set customer = Cells.Find(what:="BILL TO").Offset(1, 0)
customer.Copy wsDest.Range("B" & columnItem).Offset(1)
'Copy and paste Amounts
'If LastItemPrevious <> "(200G)" Then
Set firstQTY = Cells.Find(what:="QTY").Offset(1, 0)
Set lastQTY = Range("E" & Rows.Count).End(xlUp)
ColumnQTY = wsDest.Cells(Rows.Count, 4).End(xlUp).Row
Range(firstQTY, lastQTY).Copy wsDest.Range("D" & ColumnQTY).Offset(1)
'copy,paste, fill date
Set invoiceDate = Cells.Find(what:="DATE").Offset(0, 1)
invoiceDate.Copy wsDest.Range("A" & columnItem).Offset(1)
'LastItemPrevious = lastItem
End If
Next
'Closes file and Sets the fileName to the next file
Workbooks(fileName).Close SaveChanges:=False
fileName = Dir
Wend
End Sub
Thanks Allan