Macro code works but same code in ControlButton will not.

ColinCoady

New Member
Joined
Mar 21, 2019
Messages
35
If I run this code as a macro it works fine but put same code in for a command button and I get a Run-time error '1004': Select method of range class failed.
Anyone help with a fix?

This is where the debug stopped in the code:

Rich (BB code):
Private Sub CommandButton3_Click()
'
' DeleteInputData Macro
' Deletes input data cells on sheets and objects
'


'
    Dim sh As Shape
    
    Range("B3:B11,B13:E13,B14:F14,B15").Select
    Range("B15").Activate
    Selection.ClearContents
    Range("B3").Select
    Sheets("Costing").Select
    Range("A9:C17,D9,D12,D15,D18,B20,B22,B24").Select
    Range("B24").Activate
    Selection.ClearContents
    Range("D9").Select
    
    Sheets("Docs").Select
    For Each sh In ActiveSheet.Shapes
    sh.Delete
    Next sh
    
    Sheets("InfoEntry").Select
    Range("B3").Select
    
End Sub
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

MB110

New Member
Joined
Mar 24, 2019
Messages
29
Create a new macro for your button and call this function.

Sub NewCommandButtonName_Click()
Call CommandButton3_Click
End Sub

I think this is a Private Sub vs Sub thing. You can call a private sub from elsewhere though.
 

MB110

New Member
Joined
Mar 24, 2019
Messages
29
Sorry, I misunderstood your issue. Running it from ActiveX control does give the runtime error.
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,336
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile

ADVERTISEMENT

@ColinCoady, what is the name of the sheet when the code begins?
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,336
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
What happens with the code below?

Code:
Private Sub CommandButton3_Click()
    '
    ' DeleteInputData Macro
    ' Deletes input data cells on sheets and objects
    '


    '
    Dim sh As Shape

    Sheets("InfoEntry").Range("B3:B11,B13:E13,B14:F14,B15").ClearContents
    Sheets("Costing").Range("A9:C17,D9,D12,D15,D18,B20,B22,B24").ClearContents

    For Each sh In Sheets("Docs").Shapes
        sh.Delete
    Next sh

    Sheets("InfoEntry").Select
    Range("B3").Select

End Sub
 

ColinCoady

New Member
Joined
Mar 21, 2019
Messages
35
It deletes all the pictures, objects, shapes etc on the "Docs" sheet - one at a time until the are all deleted.
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,109,540
Messages
5,529,434
Members
409,876
Latest member
Akash Yadav
Top