Hi all,
The following code has been a work in progress. Basically it looks at a summary sheet for a day's worth of salesperson's invoices, looping through up to 40 invoice "lines" and then loops through another series of lines for payment information. I have been able to run this code selecting one file at a time. Since I run this daily on up to six files, I tried adapting some code to loop through multiple files. The code hits the error handler and tells me no file was selected (even though it opened the first file). I would greatly appreciate any help if someone can spot why this happens.
Thanks in advance!
Sub GetSalesTest1()
'This code will enter all valid invoices into one file from each day's END MONEY sheet.
Dim Title As String
Dim FileName As Variant
Dim j As Integer
Application.DisplayAlerts = False
FileName = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsm), *.xlsm", Title:="Select Invoice to update Master Copy", MultiSelect:=True)
'Handle return info from dialog box
On Error GoTo ERRORHANDLER
For j = 1 To UBound(FileName)
Workbooks.Open (FileName(j)), UpdateLinks:=0
Application.ScreenUpdating = False
Dim x As Workbook
Dim y As Workbook
Set y = Workbooks("All Invoices Master.xlsm")
Set x = Workbooks.Open(FileName)
x.Sheets("END MONEY").Select
ActiveSheet.Unprotect
'Get invoice date and salesperson name
Dim invdt As Date
Dim sperson As String
invdt = x.Sheets("END MONEY").Cells(1, 10)
sperson = x.Sheets("END MONEY").Cells(2, 10)
'Check cell in column B in rows 6 to 46, and if it is not zero, copy row columns A through K
Dim i As Integer
For i = 6 To 46
If x.Sheets("END MONEY").Cells(i, 2) <> 0 Then
'Find region code
Dim reg As String
reg = Left(x.Sheets("END MONEY").Cells(i, 1), 2)
x.Sheets("END MONEY").Range(Cells(i, 1), Cells(i, 11)).Copy
'Find next available row to paste values of copied data
lastRow = y.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
y.Sheets("Sheet1").Range("D" & lastRow + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
y.Sheets("Sheet1").Range("A" & lastRow + 1).Value = reg
y.Sheets("Sheet1").Range("B" & lastRow + 1).Value = sperson
y.Sheets("Sheet1").Range("C" & lastRow + 1).Value = invdt
Else
End If
Next i
'Get payments from second half of sheet
For i = 53 To 91
If x.Sheets("END MONEY").Cells(i, 2) <> 0 Then
x.Sheets("END MONEY").Range(Cells(i, 1), Cells(i, 11)).Copy
'Find next available row to paste values of copied data
lastRow = y.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
y.Sheets("Sheet1").Range("D" & lastRow + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
y.Sheets("Sheet1").Range("N" & lastRow + 1).Value = 0 - (y.Sheets("Sheet1").Range("L" & lastRow + 1) + y.Sheets("Sheet1").Range("M" & lastRow + 1))
y.Sheets("Sheet1").Range("A" & lastRow + 1).Value = reg
y.Sheets("Sheet1").Range("B" & lastRow + 1).Value = sperson
y.Sheets("Sheet1").Range("C" & lastRow + 1).Value = invdt
Else
End If
Next i
ActiveSheet.Protect
x.Close SaveChanges:=False
Next j
Application.ScreenUpdating = True
Application.DisplayAlerts = True
y.Save
Exit Sub
ERRORHANDLER:
MsgBox "No file was selected."
End Sub
Bookmarks