Adding 4 out of 6 Names to labels based on a variable

SlinkRN

Well-known Member
Joined
Oct 29, 2002
Messages
724
Hi all!! I have a list of 6 names. Out of those 6 names, only 4 will be valid. The valid names need to populate 4 labels on my userform. I have code that determines if a name is valid, but I'm going blank on how I get those 4 valid names to populate only 4 available labels.
So, if Bowler1 = "Good" then that name would go to lbl1.caption
If Bowler2 = "Good" then that name would go to lbl2.caption but if lbl1 is still empty it should go to lbl1.caption
If Bowler3 = "Good" then that name would go to lbl3.caption unless lbl1 and/or lbl2 are empty
and on and on until the 4 "Good" names populate the 4 labels.
I'm sure there's a loop here but I must be rusty with my loops because I just can't get my head around this one.
Can someone help? Thanks, Slink
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I don't think this is a job for a loop, rather a Select Case block where the test is True:
VBA Code:
Select Case True
    Case lbl1.Caption = "" 
        lbl1.Caption = "Good"
    Case lbl1.Caption <> "" And lbl2.Caption = ""
        lbl2.Caption = "Good"
    Case lbl1.Caption<>"" And lbl2.Caption <> ""
        lbl3.Caption = "Good"
        'and so on with the other possibilities.
End Select
 
Upvote 0
I don't think this is a job for a loop, rather a Select Case block where the test is True:
VBA Code:
Select Case True
    Case lbl1.Caption = ""
        lbl1.Caption = "Good"
    Case lbl1.Caption <> "" And lbl2.Caption = ""
        lbl2.Caption = "Good"
    Case lbl1.Caption<>"" And lbl2.Caption <> ""
        lbl3.Caption = "Good"
        'and so on with the other possibilities.
End Select
Thanks for your response Micron! Let me explain a little better. I have 6 bowlers on a team, but only 4 of them have bowled. I used the variables of Bowler1 through Bowler 6 and then did a test to see which of them have actual scores. If they have scores, they are Bowler1 = "Good", Bowler2 = "Good", Bowler4="Good, and Bowler6 = "Good". That means I need to populate label1 with Bowler 1, lbl2 with Bowler2, lbl3 with Bowler4, and lbl4 with Bowler6. I tried this but it stops then after adding Bowler 1.
VBA Code:
Select Case True
    Case lbl1.caption = "" And Bowler1 = "Good"
        lbl1.caption= "Bowler 1"
    Case lbl1.caption <> "" And lbl2.caption = "" And Bowler2 = "Good"
         lbl2.caption= "bowler 2"
    Case lbl1.caption  <> "" And lbl2.caption  <> "" And Bowler3 = "Good"
        lbl3.caption  = "bowler 3"
    Case lbl1.caption  <> "" And lbl2.caption  <> "" And lbl3.caption  <> "" And Bowler4 = "Good"
        lbl4.caption  = "Bowler 4"
End Select

Of course, it would have to check Bowler5 and Bowler 6 also. For every Bowler that is "Good", their name would go to a label. In this case, Bowler3 and Bowler5 do not have scores so they are not "Good". (There would never be more than 4 Bowlers that are "Good")
 
Last edited:
Upvote 0
This is really CLUNKY but it works:
VBA Code:
Select Case True
    Case Bowler1 = "Good" And Bowler2 = "Good" And Bowler3 = "Good" And Bowler4 = "Good"
        lbl1.Caption = "Bowler1"
        lbl2.Caption = "Bowler2"
        lbl3.Caption = "Bowler3"
        lbl4.Caption = "Bowler4"
    Case Bowler1 = "Good" And Bowler2 = "Good" And Bowler3 = "Good" And Bowler5 = "Good"
        lbl1.Caption = "Bowler1"
        lbl2.Caption = "Bowler2"
        lbl3.Caption = "Bowler3"
        lbl4.Caption = "Bowler5"
    Case Bowler1 = "Good" And Bowler2 = "Good" And Bowler3 = "Good" And Bowler6 = "Good"
        lbl1.Caption = "Bowler1"
        lbl2.Caption = "Bowler2"
        lbl3.Caption = "Bowler3"
        lbl4.Caption = "Bowler6"
    Case Bowler1 = "Good" And Bowler3 = "Good" And Bowler4 = "Good" And Bowler5 = "Good"
        lbl1.Caption = "Bowler1"
        lbl2.Caption = "Bowler3"
        lbl3.Caption = "Bowler4"
        lbl4.Caption = "Bowler5"
    Case Bowler1 = "Good" And Bowler3 = "Good" And Bowler4 = "Good" And Bowler6 = "Good"
        lbl1.Caption = "Bowler1"
        lbl2.Caption = "Bowler3"
        lbl3.Caption = "Bowler4"
        lbl4.Caption = "Bowler6"
    Case Bowler1 = "Good" And Bowler4 = "Good" And Bowler5 = "Good" And Bowler6 = "Good"
        lbl1.Caption = "Bowler1"
        lbl2.Caption = "Bowler4"
        lbl3.Caption = "Bowler5"
        lbl4.Caption = "Bowler6"
    Case Bowler1 = "Good" And Bowler2 = "Good" And Bowler4 = "Good" And Bowler5 = "Good"
        lbl1.Caption = "Bowler1"
        lbl2.Caption = "Bowler2"
        lbl3.Caption = "Bowler4"
        lbl4.Caption = "Bowler5"
    Case Bowler1 = "Good" And Bowler2 = "Good" And Bowler4 = "Good" And Bowler6 = "Good"
        lbl1.Caption = "Bowler1"
        lbl2.Caption = "Bowler2"
        lbl3.Caption = "Bowler4"
        lbl4.Caption = "Bowler6"
    Case Bowler2 = "Good" And Bowler3 = "Good" And Bowler4 = "Good" And Bowler5 = "Good"
        lbl1.Caption = "Bowler2"
        lbl2.Caption = "Bowler3"
        lbl3.Caption = "Bowler4"
        lbl4.Caption = "Bowler5"
    Case Bowler2 = "Good" And Bowler3 = "Good" And Bowler4 = "Good" And Bowler6 = "Good"
        lbl1.Caption = "Bowler2"
        lbl2.Caption = "Bowler3"
        lbl3.Caption = "Bowler4"
        lbl4.Caption = "Bowler6"
    Case Bowler3 = "Good" And Bowler4 = "Good" And Bowler5 = "Good" And Bowler6 = "Good"
        lbl1.Caption = "Bowler3"
        lbl2.Caption = "Bowler4"
        lbl3.Caption = "Bowler5"
        lbl4.Caption = "Bowler6"

End Select

I'm sure there is an easier way to do this but I think this does cover all the possibilities. I would replace "Bowler1" etc in the labels with the actual bowler names that I can reference from the sheet.
I'm definitely open to trying something else that wouldn't be so clunky though. Thanks so much for your help! Slink
 
Upvote 0
Solution
Select case stops evaluating as soon as one case is true. I missed the point (or forgot) that you want to do this for several members in one go. To use that you'd wrap it in a loop.
I'd try assigning 2,3,5,6 (as an example of the bowler numbers) to an array.
The counter becomes
For i = 0 To Ubound(myArray). This loop would then pick out 2,3,5,6 from the array (one at a time) and pass

myArray(0), [which is 2] to label 2 (or to your caption, or both - I'm not sure).
Userform.Controls("Label2").Caption="Bowler" & myArray(0)

next from the array -
Userform.Controls("Label3").Caption = "Bolwer" & myArray(1))

The Select tests would still be needed to determine where to put what but I'm a bit fuzzy on the behind the scenes details so I can't be specific. If what you have works and won't need occasional tweaking then maybe stick with it. Otherwise you might have fun working with an array.
HTH
 
Upvote 0
Thanks so much Micron! I did think about using an array but I'm pretty rusty on arrays. I think I'll keep my clunky code for now since it works but I really appreciate your input!! You definitely helped!!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top