Remove all Userforms before Save As

Scallebe

Board Regular
Joined
Feb 4, 2016
Messages
59
Hi Specialists,

I wrote a Code behind a button to save a copy of my worksheet. There I don't have a problem. :cool:

Before he start the "Save As...." code I want to delete all the userforms in my workbook, because in the copy they are not necessary.

I searched on the net but couldn't find any sollution.

Can somebody help me?

Excel version 2010.


Thanks

Greetz

Pascal
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Pascal

This will delete all the userforms in the active workbook.
Code:
Sub DeleteAllForms()
Dim vbComp As Object

    For Each vbComp In ActiveWorkbook.VBProject.VBComponents
        If vbComp.Type = vbext_ct_MSForm Then
            ActiveWorkbook.VBProject.VBComponents.Remove vbComp
        End If
    Next vbComp

End Sub
 
Upvote 0
This is my code :

Code:
Private Sub cmdHMKA_Click()

    Sheets("Alle CCF").Select
    Unload Me
       
' Opslaan als : CCF-MHKA

    ChDir "C:\Users\Pascal\Desktop"


    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\Pascal\Desktop\CCF-MHKA", _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

    Application.DisplayAlerts = True

End Sub
 
Upvote 0
Norie, that was fast! Thanks.

Next thing I couldn't find any sollution :

I want to change the actual code in ThisWorBook with this code, also before saving :

Code:
Private Sub Workbook_Open()
        Application.Visible = True
        Application.DisplayFormulaBar = False
        ActiveWindow.DisplayWorkbookTabs = True
        ActiveWindow.DisplayHeadings = True
        ActiveWindow.DisplayGridlines = False
    End Sub

Probably the sollutions I find will work and it's me who does something wrong writting the code.

Any suggestions?

Thanks

Pascal
 
Upvote 0
Norie,

Thanks for your solution.

Is it better to put my next question into a new thread?

thanks
 
Upvote 0
Norie,

Sorry, but it doesn't work....

This is the complete code :

Code:
Private Sub cmdHMKA_Click()

    Sheets("Alle CCF").Select
    Unload Me
       
' Opslaan als : CCF-MHKA

Dim vbComp As Object

    For Each vbComp In ActiveWorkbook.VBProject.VBComponents
        If vbComp.Type = vbext_ct_MSForm Then
            ActiveWorkbook.VBProject.VBComponents.Remove vbComp
        End If
    Next vbComp

    ChDir "C:\Users\Pascal\Desktop"


    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\Pascal\Desktop\CCF-MHKA", _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

    Application.DisplayAlerts = True

End Sub


Thanks

Pascal
 
Upvote 0
The code after Unload Me might not be getting executed.
 
Upvote 0
It's a button on a userform. It close the userform and start the code. With other buttons in my userform the code works after unload me.

So why shouldn't your code work after Unload me.
 
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

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