Willi Fischer
Willi Fischer

Reputation: 455

Excel VBA Textbox click event on Userform

On a EXCEL Userform, I have several Textboxes. Their number can vary as they are created dynamically.

I want to achieve the following: When the user clicks on any of the Textboxes, I want to display a Msgbox, but only on this particular userform and only once for the first click.

Could you give me a pointer for documentation that would help me achieve this?

after googling on this, my code looks like this:

Userform: create a variable number of textboxes

Option Explicit

Dim oKlasseExcel() As Klasse1

Sub userform_initialize()

    Dim i As Long
    Dim k As Long

    k = InputBox("insert number")
    i = 0

    Do
        ReDim oKlasseExcel(0 To i)
        Set oKlasseExcel(i) = New Klasse1
        Set oKlasseExcel(i).objTextbox = Userform1.Controls.Add("Forms.Textbox.1", "Textbox" & CStr(i))

        With oKlasseExcel(i).objTextbox
            .Left = 30
            .Top = 75 + 25 * i
            .Width = 300
            .Height = 25
            .MultiLine = True
        End With

        i = i + 1

    Loop Until i = k

End Sub

class module:

Option Explicit

Public WithEvents objTextbox As MSForms.TextBox

Sub objTextbox_click()
    MsgBox objTextbox.Name & ": Changeereignis ausgelöst!"
End Sub

I think I have to create a class module probably, but I am totally new to this and I think I need a well-written example with some explanation comments, please. My code above does nothing when I click on a textbox.

Upvotes: 0

Views: 7587

Answers (1)

crazylane
crazylane

Reputation: 62

If you don't want to create any additional data structures to capture clicks, maybe you can try changing some TextBox properties, for example: create a TextBox with WordWrap = False and after it was clicked change its value to True - then you could distinguish which were clicked or not.

Upvotes: 1

Related Questions