You will need to modify those to fit your specific setup since I am unaware of the names of your workbooks or worksheets.
In your invoicing workbook, paste the following code inside an ordinary (not class) module. Put a call to this macro using the command "Call salesBookUpdate" at the conclusion of your pdf button macro (but before the "End Sub").
It will open your sales book if it is not already open. Your customer row will be located on the salesbook. If the salesbook does not contain the customer's name from invoice A7, a new row will be created for him at the first open spot.
Modify this code as per your setup:
Sub salesBookUpdate()
Dim custID As String
Dim item1() As Variant
Dim wbInvoice As Workbook
Dim salesBook As Workbook
Dim salesSheet As Worksheet
Dim lastRow As Long
Dim salesBookRow As Long
Dim colrange As Range
Dim columnNumber As Long
Set wbInvoice = ThisWorkbook
Sheet1.Activate ' change to your correct data sheet
item1 = Range("A23:B27")
custID = UCase(Sheet1.Range("A7").Value)
If IsFileOpen("C:\Temp\salesBook.xlsx") Then
Set salesBook = Workbooks("salesBook.xlsx"): salesBook.Activate
Else
Set salesBook = Workbooks.Open("C:\Temp\salesBook.xlsx") ' change directory and filename to yours
End If
salesBook.Activate
Set salesSheet = salesBook.Sheets("Sales Sheet") ' change worksheet to correct one
lastRow = salesSheet.Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
With salesSheet.Range("a1:a" & lastRow)
Set c = .Find(custID, LookIn:=xlValues, MatchCase:=False)
If Not c Is Nothing Then
salesBookRow = c.Row
Else
salesBookRow = lastRow + 1
salesSheet.Range("A" & salesBookRow).Value = custID ' new customer
End If
End With
Set colrange = salesSheet.Range("B1:X1")
For i = LBound(item1) To UBound(item1)
If item1(i, 1) <> "" Then
With colrange
Set c = .Find(item1(i, 1), LookIn:=xlValues, MatchCase:=False)
If Not c Is Nothing Then
columnNumber = c.Column
Cells(salesBookRow, columnNumber).Value = Cells(salesBookRow, columnNumber).Value + item1(i, 2)
End If
End With
End If
Next i
End Sub
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function