View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Ken Puls Ken Puls is offline
external usenet poster
 
Posts: 58
Default Customizing userforms on button clicks

Me.Name will actually return the userform's caption.

The best I can do for you is that you could pass it as an MSForms
button, then query the caption. You'll still need to assign the
specific CommandButton object at the beginning of each button though:

Private Sub CommandButton1_Click()
Call CheckButton(Me.CommandButton1)
End Sub

Private Sub CommandButton2_Click()
Call CheckButton(Me.CommandButton2)
End Sub

Private Sub CheckButton(cButton As msforms.CommandButton)
MsgBox "You clicked " & cButton.Caption
End Sub

HTH,

Ken Puls, CMA - Microsoft MVP (Excel)
www.excelguru.ca

wrote:
Ken Puls wrote:
Well, here's one way.

Private Sub CommandButton1_Click()
Call ChangeCaption("CommandButton1")
End Sub

Private Sub ChangeCaption(sCaptionName As String)
Select Case sCaptionName
Case Is = "CommandButton1"
Me.Caption = "MyCustomTitle"
Case Else
Me.Caption = "Something else"
End Select
End Sub

The only benefit that I can see to doing this, though, is to have a
place to centrally manage the naming. It's actually more code than just
changing it in the Button_Click event.

Ken Puls, CMA - Microsoft MVP (Excel)
www.excelguru.ca

Actually centralizing the coding is important, as I may need to add
additional functions later.

I actually came up with an approach similar to the one suggested but
using string parsing.

Is there away to refer to the title of each button in a uniform manner?

E.g.

buttonTitle = me.name

I tried the above but it seemed to be extracting the name of the sheet.
If there was a way generic way to extract the caption or name field
from the list of properties (as opposed to writing CommandButton1 etc),
I could simply pass a generic reference - extract some data and use
that to manipulate the title.