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
 
It deletes all the pictures, objects, shapes etc on the "Docs" sheet - one at a time until the are all deleted.

That is what your lines below in your original code does

Code:
    Sheets("Docs").Select
    For Each sh In ActiveSheet.Shapes
    sh.Delete
    Next sh
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Yes - works great.
I use Insert Object for email msg, PDFs, pictures, Word Documents as icons, etc.
Some times there are none and sometimes 8-10
Cleans them all so a new workbook can be started with no data.
 
Last edited:
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

By changing this part...

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

To this...

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

It will work no matter which worksheet the command button is on. The previous is dependent on location.
 
Upvote 0
By changing this part...

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

To this...

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

It will work no matter which worksheet the command button is on. The previous is dependent on location.

I would prefer

Code:
Application.Goto Sheets("InfoEntry").Range("B3")
or
Code:
Application.Goto Sheets("InfoEntry").Range("B3"), True
 
Last edited:
Upvote 0
But since there is no need to select anything here, Mark’s code in post 9 is cleaner still. ;)
 
Upvote 0
But since there is no need to select anything here

No need for it but I left it there in post 9 because it was the OP's last action in their original code and didn't want the end result to be different to the original :rofl:
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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