Conditional UserForm Label Caption

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
554
Code:
 [FONT=Arial]Private Sub UserForm_Initialize()[/FONT]
[FONT=Arial]With Sheet8[/FONT]
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
[FONT=Arial]Label1.Caption = " " & Sheet8.Range("A3").Text & " - " & Sheet8.Range("L3").Text<o:p></o:p>[/FONT]
[FONT=Arial]Label2.Caption = " " & Sheet8.Range("A4").Text & " - " & Sheet8.Range("L4").Text<o:p></o:p>[/FONT]
[FONT=Arial]Label3.Caption = " " & Sheet8.Range("A5").Text & " - " & Sheet8.Range("L5").Text<o:p></o:p>[/FONT]
[FONT=Arial]Label4.Caption = " " & Sheet8.Range("A6").Text & " - " & Sheet8.Range("L6").Text[/FONT]
Etc…etc…about 40 times
<o:p></o:p>
I wish I could leave labels blank... If Cells in Sheet 8.Range L = 0

Thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Give this a try on a copy, givin your example, if the controls and the rows are in order it may work.



<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()<br><SPAN style="color:#00007F">Dim</SPAN> Ocontrol <SPAN style="color:#00007F">As</SPAN> Control<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> Integer: i = 1<br><br><SPAN style="color:#00007F">With</SPAN> Sheet8<br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Ocontrol <SPAN style="color:#00007F">In</SPAN> Me.Controls<br>            <SPAN style="color:#00007F">If</SPAN> TypeName(Ocontrol) = "Label" <SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> .Range("L" & i + 2).Value <> "" <SPAN style="color:#00007F">Then</SPAN><br>                    Ocontrol.Caption = " " & .Range("A" & i + 2).Text & " - " & .Range("L" & i + 2).Text<br>                <SPAN style="color:#00007F">Else</SPAN><br>                    Ocontrol.Caption = ""<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                i = i + 1<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> Ocontrol<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN></FONT>
 
Upvote 0
Repairman...
I added the code you provided. All labels continue to display even if cell value is 0. And I can't figure out why!

Code:
Private Sub UserForm_Initialize()
Dim Ocontrol As Control<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim i As Integer: i = 1<o:p></o:p>
<o:p></o:p>
With Sheet8<o:p></o:p>
  For Each Ocontrol In Me.Controls<o:p></o:p>
      If TypeName(Ocontrol) = "Label" Then<o:p></o:p>
          If .Range("L" & i + 2).Value <> "" Then<o:p></o:p>
              Ocontrol.Caption = " " & .Range("A" & i + 2).Text & " - " & .Range("L" & i + 2).Text<o:p></o:p>
          Else<o:p></o:p>
              Ocontrol.Caption = ""<o:p></o:p>
          End If<o:p></o:p>
          i = i + 1<o:p></o:p>
      End If<o:p></o:p>
  Next Ocontrol
<o:p></o:p>
Label1.Caption = " " & Sheet8.Range("A3").Text & " - " & Sheet8.Range("L3").Text<o:p></o:p>
Label2.Caption = " " & Sheet8.Range("A4").Text & " - " & Sheet8.Range("L4").Text<o:p></o:p>
Label3.Caption = " " & Sheet8.Range("A5").Text & " - " & Sheet8.Range("L5").Text<o:p></o:p>
Label4.Caption = " " & Sheet8.Range("A6").Text & " - " & Sheet8.Range("L6").Text
<o:p></o:p>
End With
 
Upvote 0
Try it without the lines
...
Label1.Caption = " " & Sheet8.Range("A3").Text & " - " & Sheet8.Range("L3").Text
...





<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()<br><SPAN style="color:#00007F">Dim</SPAN> Ocontrol <SPAN style="color:#00007F">As</SPAN> Control<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> Integer: i = 1<br><br><SPAN style="color:#00007F">With</SPAN> Sheet8<br>  <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Ocontrol <SPAN style="color:#00007F">In</SPAN> Me.Controls<br>      <SPAN style="color:#00007F">If</SPAN> TypeName(Ocontrol) = "Label" <SPAN style="color:#00007F">Then</SPAN><br>          <SPAN style="color:#00007F">If</SPAN> .Range("L" & i + 2).Value <> "" <SPAN style="color:#00007F">Then</SPAN><br>              Ocontrol.Caption = " " & .Range("A" & i + 2).Text & " - " & .Range("L" & i + 2).Text<br>          <SPAN style="color:#00007F">Else</SPAN><br>              Ocontrol.Caption = ""<br>          <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>          i = i + 1<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>  <SPAN style="color:#00007F">Next</SPAN> Ocontrol<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Yeah...
I realized that after I posted. Rem all lines and it still fills each Label with - 0

Maybe because Range L is actually $0.00?
 
Upvote 0
I beleive you are correct.



If they are always 0.00 and never blank then try:



<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()<br><SPAN style="color:#00007F">Dim</SPAN> Ocontrol <SPAN style="color:#00007F">As</SPAN> Control<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> Integer: i = 1<br><br><SPAN style="color:#00007F">With</SPAN> Sheet1<br>  <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Ocontrol <SPAN style="color:#00007F">In</SPAN> Me.Controls<br>      <SPAN style="color:#00007F">If</SPAN> TypeName(Ocontrol) = "Label" <SPAN style="color:#00007F">Then</SPAN><br>          <SPAN style="color:#00007F">If</SPAN> .Range("L" & i + 2).Value <> 0 <SPAN style="color:#00007F">Then</SPAN><br>              Ocontrol.Caption = " " & .Range("A" & i + 2).Text & " - " & .Range("L" & i + 2).Text<br>          <SPAN style="color:#00007F">Else</SPAN><br>              Ocontrol.Caption = ""<br>          <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>          i = i + 1<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>  <SPAN style="color:#00007F">Next</SPAN> Ocontrol<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>



If sometimes 0.00 / "" then try:



<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()<br><SPAN style="color:#00007F">Dim</SPAN> Ocontrol <SPAN style="color:#00007F">As</SPAN> Control<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> Integer: i = 1<br><br><SPAN style="color:#00007F">With</SPAN> Sheet1<br>  <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Ocontrol <SPAN style="color:#00007F">In</SPAN> Me.Controls<br>      <SPAN style="color:#00007F">If</SPAN> TypeName(Ocontrol) = "Label" <SPAN style="color:#00007F">Then</SPAN><br>          <SPAN style="color:#00007F">If</SPAN> .Range("L" & i + 2).Value <> "" And .Range("L" & i + 2).Value <> 0 <SPAN style="color:#00007F">Then</SPAN><br>              Ocontrol.Caption = " " & .Range("A" & i + 2).Text & " - " & .Range("L" & i + 2).Text<br>          <SPAN style="color:#00007F">Else</SPAN><br>              Ocontrol.Caption = ""<br>          <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>          i = i + 1<br>      <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>  <SPAN style="color:#00007F">Next</SPAN> Ocontrol<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Perhaps
Code:
Private Sub UserForm_Initialize()
    Dim oneControl As msforms.Control
    Dim i As Long

    With Sheet1
    
        i = 3
        For Each oneControl In Me.Controls
            If TypeName(oneControl) = "Label" Then
                oneControl.Caption = " " & .Cells(i, "A").Text & " - " & .Cells(i, "L").Text
                oneControl.Visible = (CStr(.Cells(i, "L").Value) <> "0")
                i = i + 1
            End If
        Next oneControl
        
    End With
End Sub
 
Upvote 0
Be sure to change Sheet1 to your Sheet8 in the With statement.


I like how Mike simplified i by starting at 3. :)
 
Upvote 0
Thanks Jeff and Mike...
That does exactly what I wanted.
Your help is greatly appreciated!
 
Upvote 0

Forum statistics

Threads
1,213,530
Messages
6,114,163
Members
448,554
Latest member
Gleisner2

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