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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,351
Office Version
  1. 365
Platform
  1. Windows
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
 

Scallebe

Board Regular
Joined
Feb 4, 2016
Messages
59
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
 

Scallebe

Board Regular
Joined
Feb 4, 2016
Messages
59
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
 

Scallebe

Board Regular
Joined
Feb 4, 2016
Messages
59

ADVERTISEMENT

Norie,

Thanks for your solution.

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

thanks
 

Scallebe

Board Regular
Joined
Feb 4, 2016
Messages
59
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
 

Scallebe

Board Regular
Joined
Feb 4, 2016
Messages
59

ADVERTISEMENT

The userforms are still there...
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,351
Office Version
  1. 365
Platform
  1. Windows
The code after Unload Me might not be getting executed.
 

Scallebe

Board Regular
Joined
Feb 4, 2016
Messages
59
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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,941
Messages
5,856,425
Members
431,812
Latest member
Elastomers

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
Top