Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Can't fillout labels with selected groupitems

0

Hello,

What are we doing wrong?

It seems i can't fill out the labels with the selected result i want to show

    Dim Numb                    As Long
    Dim iCheckCount, lastrow, cardid      As Long
    Dim FoundMemb               As Range
    Dim Shp As Shape
    iCheckCount = 0
    lastrow = Tasks.Range("A99999").End(xlUp).Row

If lastrow < 3 Then Exit Sub

For Numb = 3 To lastrow ' For 3 to last row in Tasks...

    On Error Resume Next
    For Each Shp In Shapes ' loop through shapes to see if there's a name match
        If Shp.Name = "Card" & Numb Then 'if so..
            With Shapes("Card" & Numb) 'check ...
                    If .GroupItems("bPic" & Numb).TextFrame2.TextRange.Text = "þ" Then '..it has been checked/marked like a checkbox
                         iCheckCount = iCheckCount + 1 ' if so, increase the count
            Dim ctrl As Control
             With UserForm2
               For Each ctrl In .Controls
                 If TypeName(ctrl) = "Label" Then
                     For ResultCount = 4 To iCheckCount
                       If Not ctrl.Caption = ResultCount Then
                          ctrl.Caption = Tasks.Range("B" & Numb).Value 'like to fillout the labels for each selected?
                       End If
                     Next
                  End If
                Next ctrl
              End With
                    End If
             End With
        End If
    Next Shp

Next Numb
UserForm2.Show

Answer
Discuss

Answers

0
Selected Answer

Ghost

You've put a loop to update the labels in Userfrom2 but that happens each time you find a tick (inder the second If line) so it gets repeated and you don't display the UserForm.

Try inserting a module or sheet macro as follows:

Sub UpdateLabels()

With UserForm2
    LabRow = 3 ' start of data rows
    For Each ctrl In .Controls
        If TypeName(ctrl) = "Label" Then
           ctrl.Caption = LabRow & " " & Tasks.Range("B" & LabRow).Value 'chnage label to next row
           LabRow = LabRow + 1 'up row number
         End If
    Next ctrl
End With
UserForm2.Show

End Sub

and you'll get the results you put on your form (when the macro is run). If you add another label, it will be populated by the next row found when the macro sees another Label.

Suggest you add a command button to your Userform (to close it) and put this in the click macro behind the button (when you double click it the new button in VB Explorer):

Unload UserForm2

Hope this helps.

Discuss

Discussion

Thanks so much John_Ru
GhostofWanted (rep: 46) Aug 28, '21 at 7:25 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login