save file, delete the assigned button on new saved worksheet

Seeker2013

New Member
Joined
Jun 2, 2014
Messages
12
I have a quotation form that have two buttons.

One is click and will generate a new quote number.
Another is click and it will be save with the assigned path and file name format.

When I click on save File, the new saved files will also copy the buttons and macron, which I like to delete the buttons and macro completely.

here is my code:
PHP:
Sub SaveWSToNewWBs()
Dim wb As Workbook
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    'ws.Copy
    Set wb = ActiveWorkbook
     wb.SaveAs ThisWorkbook.Path & "\" & ws.Name & ws.Range("G3").Value & ".xls"
    wb.Close False
Next ws
End Sub

what should I add to be able to delete the buttons and macro in the new destination file?

Many thanks in advance.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
The following macro assumes that the buttons are form controls, not ActiveX controls. Also, the macro settings need to allow access to the VBA project model...

Code:
Developer > Code > Macro Security > Macro Settings > Developer Macro Settings

...and select 'Trust access to the VBA project model'. So for each worksheet within the workbook, a new workbook is created with a copy of the sheet, all the buttons on the sheet are deleted, any code within the sheet module is deleted, etc.

Code:
Option Explicit

Sub SaveWSToNewWBs()

    Dim VBP     As Object
    Dim wb      As Workbook
    Dim ws      As Worksheet
    Dim Msg     As String
    
    On Error Resume Next
    Set VBP = ThisWorkbook.VBProject
    On Error GoTo 0
    If VBP Is Nothing Then
        Msg = "Your macro settings do not allow this macro to run.  "
        Msg = Msg & "You'll need to allow access to the VBA project model."
        MsgBox Msg, vbExclamation, "Macro Settings"
        Exit Sub
    End If
    
    For Each ws In ThisWorkbook.Worksheets
        ws.Copy
        Set wb = ActiveWorkbook
        With wb
            .Worksheets(1).Buttons.Delete
            With .VBProject.VBComponents(.Worksheets(1).CodeName).CodeModule
                .DeleteLines 1, .CountOfLines
            End With
            .SaveAs ThisWorkbook.Path & "\" & ws.Name & ws.Range("G3").Value & ".xls"
            .Close False
        End With
    Next ws
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,637
Messages
6,125,964
Members
449,276
Latest member
surendra75

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