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
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
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.
 
Upvote 0
Sorry, I misunderstood your issue. Running it from ActiveX control does give the runtime error.
 
Upvote 0
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
 
Upvote 0
It deletes all the pictures, objects, shapes etc on the "Docs" sheet - one at a time until the are all deleted.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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