Consulting

Results 1 to 3 of 3

Thread: several handlers with same piece of code

  1. #1
    VBAX Newbie
    Joined
    Aug 2013
    Posts
    5
    Location

    several handlers with same piece of code

    Hi. I would like to ask for you guys help. I'm a begginer in VBA but I was asked at work to develop a VBA app and I need to finish it asap. I'm trying to learn as fast as I can but all the advanced stuff I've been searching online and writting "dirty code". I don't know exactly what I'm doing but it's working so far. I'm stuck with the following matter. The code below works like a charm.




    Private Sub Label3_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    Label3.ForeColor = vbWhite
    Label3.SpecialEffect = 1
    Label3.BackStyle = 1
    Label3.BackColor = &H808000

    End Sub


    Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    Label3.ForeColor = vbBlack
    Label3.SpecialEffect = 0
    Label3.BackStyle = 0

    End Sub


    My problem is that I need the same piece of code for other 500 Labels in the same userform. I know that it's possible to do it without writting the code for each label separately, but I had no success in the past 2 days. Would you guys help me find a solution? I tried to work with class modules and variables, but it's too advanced for me, I need a Light.


    Thank you!

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    In the userform's code-module:
    Dim Labels() As New LblClass
    Private Sub UserForm_Initialize()
    Dim LabelCount As Long
    Dim ctl As Control
    ' Create the Label objects
    LabelCount = 0
    For Each ctl In Controls
      If TypeName(ctl) = "Label" Then
        LabelCount = LabelCount + 1
        ReDim Preserve Labels(1 To LabelCount)
        Set Labels(LabelCount).LabelGroup = ctl
      End If
    Next ctl
    End Sub
    
    Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    For Each lbl In Labels
      With lbl.LabelGroup
        .ForeColor = vbBlack
        .SpecialEffect = 0
        .BackStyle = 0
      End With
    Next lbl
    End Sub
    Create a new Class Module, change its name to
    LblClass
    and paste this code in it:
    Public WithEvents LabelGroup As MsForms.Label
    
    Private Sub LabelGroup_Click() 'you don't need this sub but it serves as an example
    Msg = "You clicked " & LabelGroup.Name & vbCrLf & vbCrLf
    Msg = Msg & "Caption: " & LabelGroup.Caption & vbCrLf
    Msg = Msg & "Left Position: " & LabelGroup.Left & vbCrLf
    Msg = Msg & "Top Position: " & LabelGroup.Top
    MsgBox Msg, vbInformation, LabelGroup.Name
    End Sub
    
    Private Sub LabelGroup_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    LabelGroup.ForeColor = vbWhite
    LabelGroup.SpecialEffect = 1
    LabelGroup.BackStyle = 1
    LabelGroup.BackColor = &H808000
    End Sub
    Adapted from John Walkenbach's code in Excel 20nn Power Programming with VBA, where nn is one of 02, 03, 07, 10.

    All above in the attachment.
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum Lberteh.

    i'm wondering why someone needs their userform has 500 labels.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •