Need to remove macro button when creating new sheet

alleneure

New Member
Joined
Feb 22, 2023
Messages
17
Office Version
  1. 365
Platform
  1. Windows
I have a workbook that has an initial form that I fill out and once I'm done I press a form control button that copies everything to a new sheet and deletes all input information from the initial form except the logo and the form control box. Currently the form control box stays active in the new sheets so I need to delete the form control box before making the new sheet. Current code below work perfectly minus deleting the form control box.
VBA Code:
Public Sub CopySheetAndRenameByCell2()
  
Dim wks As Worksheet
  Set wks = ActiveSheet
  ActiveSheet.Copy Before:=Worksheets(Sheets.Count)
  If wks.Range("C3").Value <> "" Then
    On Error Resume Next
    ActiveSheet.Name = wks.Range("C3").Value
  End If
  
  Shapes.Range(Array("Button 1")).Delete 'Used this to try to delete button

  wks.Activate
  
  Sheets("New MRL").Range("c3:c7") = ""
  Sheets("New MRL").Range("c9:d11") = ""
  Sheets("New MRL").Range("c19:c25") = ""
  Sheets("New MRL").Range("h3") = ""
  Sheets("New MRL").Range("d5") = ""
  Sheets("New MRL").Range("c31") = ""
  Sheets("New MRL").Range("h2:h5") = ""
  
   Dim pic As Picture
    
    For Each pic In ActiveSheet.Pictures
        If pic.Name <> "Logo" Then
            pic.Delete
        End If
    Next pic

End Sub

I attempted to delete button with the line shown but as I'm not super familiar with VBA I'm sure I'm doing something wrong. Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Let's say the form button on worksheet wks shows text that says "Run Macro #1". Here is one way to delete that button

VBA Code:
    Dim B As Button
    Dim wks As Worksheet
    Set wks = ActiveSheet

        For Each B In wks.Buttons
            If B.Caption = "Run Macro #1" Then
                B.Delete  'Delete the button labeled "Run Macro #1"
            End If
        Next B
 
Upvote 0
Let's say the form button on worksheet wks shows text that says "Run Macro #1". Here is one way to delete that button

VBA Code:
    Dim B As Button
    Dim wks As Worksheet
    Set wks = ActiveSheet

        For Each B In wks.Buttons
            If B.Caption = "Run Macro #1" Then
                B.Delete  'Delete the button labeled "Run Macro #1"
            End If
        Next B
Thanks! That worked for deleting the button but it deleted the active sheet button and not the new sheet button. The new sheet has the button remaining and blank form has the button deleted button Modifications?
 
Upvote 0
You could try

VBA Code:
Dim wks As Worksheet
  Set wks = ActiveSheet
  ActiveSheet.Copy Before:=Worksheets(Sheets.Count)
  If wks.Range("C3").Value <> "" Then
    On Error Resume Next
    ActiveSheet.Name = wks.Range("C3").Value
  End If

    Dim B As Button
        For Each B In ActiveSheet.Buttons
            If B.Caption = "Run Macro #1" Then
                B.Delete  'Delete the button labeled "Run Macro #1"
            End If
        Next B
 
Upvote 0
I changed my mind and what to name new sheet after cells C3 and C5. I tried the below code and it did not work properly. Any suggestions?
VBA Code:
Public Sub CopySheetAndRenameByCell2()
  
Dim wks As Worksheet
  Set wks = ActiveSheet
  ActiveSheet.Copy Before:=Worksheets(Sheets.Count)
  If wks.Range("C3","C5").Value <> "" Then
    On Error Resume Next
    ActiveSheet.Name = wks.Range("C3","C5").Value
  End If
 
Upvote 0
wks.Range("C3","C5").Value references multiple cells. You can't use that to name your worksheet. It needs to be just a single cell.
 
Upvote 0
You can use a variable to name the sheet with those references.
With the current code if c3 or c5 is empty then your variable is also empty and Excel will name the sheet as a copy with a (2)
You'd need to decide what you want to name the sheet if either of those cells is empty.

Code:
  If wks.Range("C3").Value <> "" And wks.Range("C5").Value <> "" Then
  NewSheetName = wks.Range("C3").Value & " " & wks.Range("C5").Value
  ActiveSheet.Name = NewSheetName
  Else
  ActiveSheet.Name = "c3 or 5 is empty"
  End If
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,145
Members
449,363
Latest member
Yap999

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