close a "do you want to save box"

royboy531

Board Regular
Joined
Nov 11, 2005
Messages
52
in middle of my macro I open a new book and at the end of the macro I close it but in the macro the pop up box that asks "save before close" comes up. I need a code to auto select no to close the box. anyone have any ideas. thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You need to add a line of code to save the workbook with either SaveChanges:=True or SaveChanges:=False.
 
Upvote 0
I still can't seem to get it to work. when I put in SaveChanges:=True or SaveChanges:=False I get a compile error: expected: expression. I tried taking the colon out, didn't work. I also tried puttig save instead of savechanges, didn't work. any other suggestions??? thanks for the help.
[/code]
 
Upvote 0
Can you post the code?

You might try this.
Code:
Application.DisplayAlerts= False

' your code here

Application.DisplayAlerts= True

By the way where did you put Brian's suggestion?
 
Upvote 0
Norie,
I was putting Brian's code after Activewindow.close. For your suggestion am I supposed to put Brian's code between yours or part of my code?? I am a bit confused????
thanks
royboy531
 
Upvote 0
here it is Norie
Code:
Sub macro6()
UserForm1.Show
End Sub

Private Sub btnFilter_Click()

    Dim startDate As String
    Dim shift As String
    
    'reads date from the userform
    startDate = tbstartdate.Value 'tbStartDate is the name of the textbox for Starting Date
    shift = tbshift 'tbshift is the name of the textbox for shift
    
    'FILTERS
    With Worksheets("sheet1")
        .AutoFilterMode = False
        .Range("a1:p1").AutoFilter
        .Range("a1:p1").AutoFilter Field:=2, Criteria1:=shift
        .Range("a1:p1").AutoFilter Field:=1, Criteria1:=(">=" & startDate), _
            Operator:=xlAnd, Criteria2:=("<=" & startDate)
       UserForm1.Hide
        Range("A1:P" & Cells(Rows.Count, "A").End(xlUp).Row).Select
End With
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Sort Key1:=Range("E2"), Order1:=xlAscending, Key2:=Range("C2") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom
    Selection.Subtotal GroupBy:=3, Function:=xlAverage, TotalList:=Array(6, 7, _
        8, 9), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    Range("a1").Select
    Range("A1:P" & Cells(Rows.Count, "C").End(xlUp).Row).Select
    With Selection.Font
        .Size = 9
    Selection.AutoFormat Format:=xlRangeAutoFormatSimple, Number:=True, Font _
        :=True, Alignment:=True, Border:=True, Pattern:=True, Width:=True
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = False
    End With
    Range("A1:P1").Select
    With Selection.Font
    .Size = 8.5
    End With
    With ActiveSheet.PageSetup
        .CenterHeader = "&D  &T"
        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .HeaderMargin = Application.InchesToPoints(0.5)
        .FooterMargin = Application.InchesToPoints(0.5)
        .CenterHorizontally = True
        .CenterVertically = False
        .Orientation = xlLandscape
        .Draft = False
        .PaperSize = xlPaperLetter
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = True
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .PrintArea = ActiveSheet.UsedRange.Address
    End With
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
    ActiveWindow.Close
    Application.DisplayAlerts = False
    Savechanges = True
    Application.DisplayAlerts = True
    Range("a1").Select
    Selection.AutoFilter
    Selection.AutoFilter


End Sub
 
Upvote 0
This has both Brian and I's suggestions.
Code:
Private Sub btnFilter_Click()
Dim startDate As String
Dim shift As String
    
    'reads date from the userform
    startDate = tbstartdate.Value 'tbStartDate is the name of the textbox for Starting Date
    shift = tbshift 'tbshift is the name of the textbox for shift
    
    Application.DisplayAlerts = False
    
    'FILTERS
    With Worksheets("sheet1")
        .AutoFilterMode = False
        .Range("a1:p1").AutoFilter
        .Range("a1:p1").AutoFilter Field:=2, Criteria1:=shift
        .Range("a1:p1").AutoFilter Field:=1, Criteria1:=(">=" & startDate), _
            Operator:=xlAnd, Criteria2:=("<=" & startDate)
       UserForm1.Hide
        Range("A1:P" & Cells(Rows.Count, "A").End(xlUp).Row).Copy
    End With
    
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Sort Key1:=Range("E2"), Order1:=xlAscending, Key2:=Range("C2") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom
    Selection.Subtotal GroupBy:=3, Function:=xlAverage, TotalList:=Array(6, 7, _
        8, 9), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    
    With Range("A1:P" & Cells(Rows.Count, "C").End(xlUp).Row)
        Font.Size = 9
        .AutoFormat Format:=xlRangeAutoFormatSimple, Number:=True, Font _
        :=True, Alignment:=True, Border:=True, Pattern:=True, Width:=True
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = False
    End With
    
    With Range("A1:P1").Font
        .Size = 8.5
    End With
    
    With ActiveSheet.PageSetup
        .CenterHeader = "&D  &T"
        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .HeaderMargin = Application.InchesToPoints(0.5)
        .FooterMargin = Application.InchesToPoints(0.5)
        .CenterHorizontally = True
        .CenterVertically = False
        .Orientation = xlLandscape
        .Draft = False
        .PaperSize = xlPaperLetter
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = True
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .PrintArea = ActiveSheet.UsedRange.Address
    End With
    
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
    
    ActiveWindow.Close Savechanges:=True
    
    Range("A1").AutoFilter
    Range("A1").AutoFilter

    Application.DisplayAlerts = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top