Code cleanup help

P

peter.thompson

I want to disable 30 commandbuttons on a sheet. At the moment I'm using
the following code which works, but I'm sure there must be a better
way.

Sheet7.CommandButton1.Enabled = False
Sheet7.CommandButton2.Enabled = False

(repeated to comandbutton30)

Any help appreciated (If you could also walk me through the code as to
what its doing that would be great for my learning!)

Cheers

Peter (slogging through my first VBA project)
 
N

Norman Jones

Hi Peter,

Try:

'=============>>
Public Sub Tester002()
Dim obj As OLEObject

For Each obj In Sheets("Sheet7").OLEObjects
If TypeOf obj.Object Is MSForms.CommandButton Then
obj.Object.Enabled = False
End If
Next obj

End Sub
'<<=============


---
Regards,
Norman



"peter.thompson"
 
P

peter.thompson

Thanks Norman,

Wouldn't work with Sheets("Sheet7").OLEobjects syntax

but does work with plain Sheet7.OLEObjects

Why is this the case?

Again, thanks for all the help on my first VBA project!

Cheers

Peter
 
N

Norman Jones

Hi Peter,
Wouldn't work with Sheets("Sheet7").OLEobjects syntax
Here "Sheet7" refers to the sheet named "Sheet7".
but does work with plain Sheet7.OLEObjects
Here Sheet7 refers to the sheet whose codename is Sheet7

If a sheet's name is changed, then the sheet name and code name will
differ.You are using the sheet's code name and, consquently, your code will
work irrespective of any change to the sheets name.


---
Regards,
Norman



"peter.thompson"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Shortening code 10
simple question "disable command buttons" 0
Code cleanup 2
Command Button Help 2
Need some PWA help 0
Code cleanup help please 5
Compile problem 2
Userform problem (Listindex) 3

Top