print/save functions and error message
I'm having a problem getting the print, save and error handling working properly in my form. The combination of print and save are contingent on the error handling. Once the combobox is changed, the code filters the spreadsheet by the value of the combobox. It then checks to see if there are any filled in rows and if there are not it should create an error and exit. That part is not working correctly. The other part is the print/save. If there are filled in rows, I want the code to print the rows and save them in a seperate workbook using the OrgShp value as part of the saved filename - which it should overwrite anytime you filter without asking for overwrite permission. Any help will be greatly appreciated. Here's the code:
Private Sub cboOrgShpFilterExp_Change()
Dim Expenditures As Range
Dim NewWorkbook As Workbook
Dim WorksheetName As Variant
Dim OrgShp As String
Dim TempWorkbook As Workbook
OrgShp = cboOrgShpFilterExp.Value
On Error Resume Next
WorksheetName = Format( _
DateValue(OrgShp & " "), "ddmmmyyyy")
Set TempWorkbook = Workbooks(WorksheetName)
With Range("expenditures")
.AutoFilter field:=2, Criteria1:=(OrgShp), VisibleDropDown:=False
On Error Resume Next
MsgBox "There are no expenditures to report for this Org/Shop"
Call Unload(Me)
Set NewWorkbook = Workbooks.Add
.Copy Destination:=NewWorkbook.Range("B1")
End With
NewWorkbook.Columns("A:H").AutoFit
ActiveSheet.Name = WorksheetName
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.CenterHeader = "Expenditure Log" & Chr(10) & "&D"
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.Orientation = xlPortrait
.Zoom = 100
End With
'ActiveSheet.PrintOut
Set TempWorkbook = Workbooks.Add
Application.DisplayAlerts = False
Workbook1.SaveAs Filename:=ThisWorkbook.Path & "\" & (OrgShp.Value) & "Exp_log.xls"
Application.DisplayAlerts = True
Call Unload(Me)
End Sub
|