auto delete vba with vba

d45a3

New Member
Joined
Feb 15, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
hello i have this code running at the opening off the excel
VBA Code:
Private Sub Workbook_Open()

Dim Chemin As String, Name As String

Chemin = ActiveWorkbook.FullName
Name = ActiveWorkbook.Name

If Name = "FT Vide.xlsm" Then

    Else
    ActiveSheet.Shapes.Range(Array("Button 24")).Select
    Selection.Delete
End If

End Sub
When the name of the document is not equal to ''FT Empty.xlsm''. I make sure to delete a button on the Excel sheet (button 24). but when I open the document again and the button is no longer. I get a runtime error. Is there a way to insert a command to delete the section where I look for the file name?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
The simplest way is probably to use On Error Resume Next

VBA Code:
If Name = "FT Vide.xlsm" Then
        
    Else
        On Error Resume Next
        ActiveSheet.Shapes.Range(Array("Button 24")).Select
        Selection.Delete
        On Error GoTo 0
    End If
 
Upvote 0
error 1004
The simplest way is probably to use On Error Resume Next

VBA Code:
If Name = "FT Vide.xlsm" Then
       
    Else
        On Error Resume Next
        ActiveSheet.Shapes.Range(Array("Button 24")).Select
        Selection.Delete
        On Error GoTo 0
    End If
tanks the error resume next work good
but i need to ajuste the code
VBA Code:
        On Error Resume Next
        ActiveSheet.Shapes.Range(Array("Button 24")).Select.Delete
        On Error GoTo 0
because the fact that it is no longer the button the selection is no longer valid and therefore it selects the last active box of the workbook and removes the box
 
Upvote 0
The simplest way is probably to use On Error Resume Next

VBA Code:
If Name = "FT Vide.xlsm" Then
       
    Else
        On Error Resume Next
        ActiveSheet.Shapes.Range(Array("Button 24")).Select
        Selection.Delete
        On Error GoTo 0
    End If
VBA Code:
If Name = "FT Vide.xlsm" Then

    Else
       On Error GoTo ErrorHandler
        ActiveSheet.Shapes.Range(Array("Button 24")).Select
       On Error GoTo ErrorHandler
        Selection.Delete
        
End If
ErrorHandler:
i use thas is workind good
 
Upvote 0
Glad you found a solution. For future reference, there is a cleaner way to delete form buttons using the button text (the button caption) instead of "Button 24" (the button name)

VBA Code:
Private Sub Workbook_Open()
    Dim B As Button
    If Me.Name <> "FT Vide.xlsm" Then
        For Each B In ActiveSheet.Buttons
            If B.Caption = "Run" Then
                B.Delete  'Delete the button labeled "Run"
            End If
        Next B
    End If
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,214,665
Messages
6,120,803
Members
448,990
Latest member
rohitsomani

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