Sub QBExport()
Dim NextPromptDate As Date: NextPromptDate = Worksheets("PrintSettings").Range("NextPromptDate")
Dim ReportMonth As Date: ReportMonth = Application.WorksheetFunction.EoMonth(NextPromptDate, -2) + 1
Dim DateStart As Date: DateStart = Application.WorksheetFunction.EoMonth(NextPromptDate, -2) + 1
Dim DateEnd As Date: DateEnd = Application.WorksheetFunction.EoMonth(NextPromptDate, -1)
Dim NewPromptDate As Date: NewPromptDate = Application.WorksheetFunction.EDate(NextPromptDate, 1)
Dim wsToExport As Worksheet
Dim w As Long
Dim NewWB As Workbook
Dim cEs As New clsExcelSettings
Dim tbl As ListObject
'Check Next Prompt
If Date < NextPromptDate Then Exit Sub
'Ask UI
If MsgBox("It is time for the QB Export, press 'YES' to begin the process.", vbYesNo, "Quickbooks Export") = vbYes Then
'Settings Off
cEs.SettingsOff
'Create New WB and Worksheets
Set NewWB = Workbooks.Add
NewWB.Worksheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Paid"
NewWB.Worksheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Void"
NewWB.Worksheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Replaced Checks"
NewWB.Worksheets("Sheet1").Delete
'Paid
ThisWorkbook.Activate
Call WSUnProtect(Worksheets("Paid"))
Worksheets("Paid").ListObjects(1).Refresh
DoEvents
Set wsToExport = Worksheets("Paid")
Set tbl = wsToExport.ListObjects(1)
Call WSUnProtect(wsToExport)
tbl.Range.AutoFilter Field:=3, Criteria1 _
:=">=" & CStr(Format(DateStart, "m/d/yyyy")), Operator:=xlAnd, Criteria2:="<=" & CStr(Format(DateEnd, "m/d/yyyy"))
tbl.Range.SpecialCells(xlCellTypeVisible).Copy
NewWB.Activate
NewWB.Worksheets(wsToExport.Name).Range("A1").PasteSpecial
NewWB.Worksheets(wsToExport.Name).Columns.EntireColumn.AutoFit
ThisWorkbook.Activate
tbl.AutoFilter.ShowAllData
Call WSProtect(wsToExport)
'Void
Set wsToExport = Worksheets("Void")
Set tbl = wsToExport.ListObjects(1)
Call WSUnProtect(wsToExport)
tbl.Range.SpecialCells(xlCellTypeVisible).Copy
NewWB.Activate
NewWB.Worksheets(wsToExport.Name).Range("A1").PasteSpecial
NewWB.Worksheets(wsToExport.Name).Columns.EntireColumn.AutoFit
ThisWorkbook.Activate
Call WSProtect(wsToExport)
'Paid
Set wsToExport = Worksheets("Replaced Checks")
Set tbl = wsToExport.ListObjects(1)
Call WSUnProtect(wsToExport)
tbl.Range.AutoFilter Field:=3, Criteria1 _
:=">=" & CStr(Format(DateStart, "m/d/yyyy")), Operator:=xlAnd, Criteria2:="<=" & CStr(Format(DateEnd, "m/d/yyyy"))
tbl.Range.SpecialCells(xlCellTypeVisible).Copy
NewWB.Activate
NewWB.Worksheets(wsToExport.Name).Range("A1").PasteSpecial
NewWB.Worksheets(wsToExport.Name).Columns.EntireColumn.AutoFit
ThisWorkbook.Activate
tbl.AutoFilter.ShowAllData
Call WSProtect(wsToExport)
'Save New WB
Dim wbFile As String
NewWB.SaveAs QBExportFolder & "QB Export " & Format(ReportMonth, "m_d_yyyy") & ".xlsx", 51
wbFile = NewWB.FullName
NewWB.Close SaveChanges:=False
'Set New Next Prompt
Call WSUnProtect(Worksheets("PrintSettings"))
Worksheets("PrintSettings").Range("NextPromptDate") = NewPromptDate
Call WSProtect(Worksheets("PrintSettings"))
'Send Email
Call QBExportEmailAccountant(CStr(Format(ReportMonth, "mmmm yyyy")), wbFile)
'Settings On
cEs.SettingsOn
'UI Confirm
MsgBox "Process Complete"
End If
End Sub
Sub QBExportEmail(ReportMonth As String) 'this one works
Dim sMail As String, sSubj As String, sBody As String
sMail = "email@email"
sSubj = "QB Export " & ReportMonth
sBody = "QB Export for " & ReportMonth & " is in " & QBExportFolder & " a copy was sent to the accountant."
Call SendMail(sMail, sSubj, sBody)
End Sub
Sub QBExportEmailAccountant(ReportMonth As String, sAttachment As String)
Dim sMail As String, sSubj As String, sBody As String
sMail = "email@email"
sSubj = "Full Check Log Export " & ReportMonth
sBody = "Full Check Log Export for " & ReportMonth & " is attached. Please reveiw for accuracy."
Call SendMailAccountant(sMail, sSubj, sBody, sAttachment)
End Sub
Sub SendMailAccountant(sMail, sSubj, sBody, sAttachment)
Dim OutlookApp As Object
Set OutlookApp = CreateObject("Outlook.Application").CreateItem(0)
With OutlookApp
.To = sMail
.Subject = sSubj
.Body = sBody
.Attachments.Add = sAttachment
' .Display 'Display Email
.Send 'Send Email
End With
End Sub