Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing workbook AND VBA file

Tags:

excel

vba

I have built a small VBA script, but I am having trouble fully closing out the workbook. I can close the work book but have noticed that it still shows up in the VBA editor. I have seen other forms on placing a bit of code to close everything out but I can not get it to work.

Can anyone point me in the right direction?

Private Sub PNTXLXS_Click()

Application.DisplayAlerts = False
Application.EnableCancelKey = xlDisabled

RCD_PNT.Hide


 With Sheets("Clash List").UsedRange
      mr = .Rows.Count
      mc = .Columns.Count
     .Range(Cells(1, 26), Cells(mr,mc)).SpecialCells(xlCellTypeVisible).Copy
 End With

 Workbooks.Add

 Application.Visible = True

With ActiveSheet.Range("A1")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
End With

With Selection
    .WrapText = False
    .EntireColumn.AutoFit
    .WrapText = True
End With


InitialName = Range("A1") & " - " & Format(Now(), "DDMMYY")

filesavename = Application.GetSaveAsFilename(InitialFileName:=InitialName, 
fileFilter:="Excel Files (*.xlsx), *.xlsx")

ActiveWorkbook.SaveAs FileName:=filesavename

ActiveWorkbook.Close

I know this is is the location i need something to close the VBA file correct?

Application.CutCopyMode = False

Application.DisplayAlerts = True

End Sub

enter image description here

Thanks

Mark

like image 300
Mark Austin Avatar asked Dec 02 '25 06:12

Mark Austin


2 Answers

Remove Application.Visible = Truefrom your code and instead of RCD_PNT.Hide use Me.Hide

Private Sub PNTXLXS_Click()

Application.DisplayAlerts = False
Application.EnableCancelKey = xlDisabled

Me.Hide


 With Sheets("Clash List").UsedRange
      mr = .Rows.Count
      mc = .Columns.Count
     .Range(Cells(1, 26), Cells(mr,mc)).SpecialCells(xlCellTypeVisible).Copy
 End With

 Workbooks.Add

' Application.Visible = True

With ActiveSheet.Range("A1")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
End With

With Selection
    .WrapText = False
    .EntireColumn.AutoFit
    .WrapText = True
End With


InitialName = Range("A1") & " - " & Format(Now(), "DDMMYY")

filesavename = Application.GetSaveAsFilename(InitialFileName:=InitialName, 
fileFilter:="Excel Files (*.xlsx), *.xlsx")

Application.CutCopyMode = False
Application.DisplayAlerts = True

End Sub

And it is certainly a good idea to read Userform.show

And it is better way to show the userform like

Sub Show_RCDPNT()

Dim frm As New RCD_PNT

    frm.Show
    Unload frm

End Sub
like image 128
Storax Avatar answered Dec 04 '25 00:12

Storax


I see a Workbooks.Add command so a new Workbook is being opened: are you doing anything with it? You're using Sheets before that line so you've got one opened already and it's the ActiveWorkbook, not that new one. Then you make Excel visible, work with some ranges, and save a copy of the ActiveWorkbook. But since the Excel Application object still exists that new workbook is hanging out in the Workbooks collection, yes? Maybe that's what you're seeing?

like image 23
VBANoobJoe Avatar answered Dec 04 '25 01:12

VBANoobJoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!