ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Creating Name for a Command Button (https://www.excelbanter.com/excel-programming/339923-creating-name-command-button.html)

Alan Douglas

Creating Name for a Command Button
 
I want to create a piece of code that can refer to a specified command
button number

I have tried approaches like the following intiated by clicking Commmand
Button 4:

Dim ThisButtonNo As String
Dim ThisButtonName as string
Dim ThisCommandButton As Object

Private Sub CommandButton4_Click()
ThisButtonNo = "2"
ThisButtonName = "CommandButton" & ThisButtonNo
Set ThisCommandButton = ThisButtonName
ThisCommandButton.Enabled = True
End Sub

The problem is with the CommandButton object variable not set, but I don't
know what code/variables to use to do this.

Alan



bigwheel

Creating Name for a Command Button
 
Command Buttons have properties. So, to change any of the properties, you
would refer to it by name i.e.

CommandButton1.Caption = "CommandButton2"
or
CommandButton2.Name = "CommandButton4"

Is your example code trying to change CommandButton4 into CommandButton2 ?

"Alan Douglas" wrote:

I want to create a piece of code that can refer to a specified command
button number

I have tried approaches like the following intiated by clicking Commmand
Button 4:

Dim ThisButtonNo As String
Dim ThisButtonName as string
Dim ThisCommandButton As Object

Private Sub CommandButton4_Click()
ThisButtonNo = "2"
ThisButtonName = "CommandButton" & ThisButtonNo
Set ThisCommandButton = ThisButtonName
ThisCommandButton.Enabled = True
End Sub

The problem is with the CommandButton object variable not set, but I don't
know what code/variables to use to do this.

Alan




Alan Douglas

Creating Name for a Command Button
 
Thanks very much for the reply. Unfortunately it didn't hit the nail on
the head. Let me explain again


I want to create a line of code which reads something like

ThisCommandButton.Enabled = True

ThisCommandButton is a variable which could contain CommandButton1 or
CommandButton2 or CommandButton3 etc

My problem is how to create the variable holding the Command Button name
so that the above line of code will run.

Thanks

AlanTD (Alan Douglas)

*** Sent via Developersdex http://www.developersdex.com ***

Peter T

Creating Name for a Command Button
 
Hi Alan,

I guess you are looking for -

Set ThisCommandButton = Me.OLEObjects(ThisButtonName)

Perhaps you might also want to declare the object variable,
Dim ThisCommandButton As OLEObject

I assume you have a reason for declaring your variables at module level.

Regards,
Peter T

"Alan Douglas" wrote in message
...
I want to create a piece of code that can refer to a specified command
button number

I have tried approaches like the following intiated by clicking Commmand
Button 4:

Dim ThisButtonNo As String
Dim ThisButtonName as string
Dim ThisCommandButton As Object

Private Sub CommandButton4_Click()
ThisButtonNo = "2"
ThisButtonName = "CommandButton" & ThisButtonNo
Set ThisCommandButton = ThisButtonName
ThisCommandButton.Enabled = True
End Sub

The problem is with the CommandButton object variable not set, but I don't
know what code/variables to use to do this.

Alan






All times are GMT +1. The time now is 05:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com