Set background color on userform label from cell with conditional formatting inside certain range

Jimmypop

Well-known Member
Joined
Sep 12, 2013
Messages
753
Office Version
  1. 365
Platform
  1. Windows
This has probably been asked before and I have tried numerous solutions however I do not know what I am doing wrong but here goes:

I have a worksheet in my workbook named “5S Worksheet”. On this worksheet columns “K” and “L” are merged to display a percentage value which is filled with a certain colour depending on value in cell. This is done with conditional formatting. Value can be from 0% to 100%.

I then created a button that when clicked will activate a userform called “DeleteRec”. When userform is activated it displays info to the user, e.g. record 1 has a value of 100%, 2 has a value of 50%, 3 has a value of 25% etc.

Next to record 1 I have added a label (label51) which shows the value of whatever percentage. I got this right so it can show the percentage in the label. (on userform)

My problem is I also want to set the background colour of the label to whatever is in the cell, so let’s say value is 100% and colour is green then label51 on userform must show 100% and be coloured green. I get this to work using select case.

However, it no longer works when I extend select case to cover the subsequent cells beneath. My whole range is from K12 to K112. So when I update
VBA Code:
Select Case Sheets("5S Worksheet").Range("K12").value
to
VBA Code:
Select Case Sheets("5S Worksheet").Range("K12:K112").value
I get a type 13 mismatch.

Here is the code so far: hope someone can assist.



PS. I know userform (screenshot attached) looks like a dogs breakfast but I am still designing it lol :ROFLMAO:?….

VBA Code:
Private Sub UserForm_Initialize()
    Dim vCol
    Select Case Sheets("5S Worksheet").Range("K12").value
    Case Is = Empty
        vCol = vbWhite
    Case 0 To 0.24
        vCol = RGB(255, 0, 0)
    Case 0.25 To 0.49
        vCol = RGB(255, 255, 0)
    Case 0.5 To 0.99
        vCol = RGB(255, 192, 0)
    Case Is = 1
        vCol = RGB(0, 176, 80)
    Case Else
        vCol = vbWhite
    End Select
    
    With Sheets("5S Worksheet").Range("K12")
        Label51.Caption = .Text
        Label51.BackColor = vCol
    End With
    With Sheets("5S Worksheet").Range("K14")
        Label52.Caption = .Text
        Label52.BackColor = vCol
    End With
    With Sheets("5S Worksheet").Range("K16")
        Label53.Caption = .Text
        Label53.BackColor = vCol
    End With
    With Sheets("5S Worksheet").Range("K18")
        Label54.Caption = .Text
        Label54.BackColor = vCol
    End With
 

Attachments

  • Capture.PNG
    Capture.PNG
    74.4 KB · Views: 82

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
There's no need for the select case, you can do it like
VBA Code:
Private Sub UserForm_Initialize()
   With Sheets("5S Worksheet")
      With .Range("K12")
         Label51.Caption = .Text
         Label51.BackColor = .DisplayFormat.Interior.Color
      End With
      With .Range("K14")
         Label52.Caption = .Text
         Label52.BackColor = .DisplayFormat.Interior.Color
      End With
      With .Range("K16")
         Label53.Caption = .Text
         Label53.BackColor = .DisplayFormat.Interior.Color
      End With
      With .Range("K18")
         Label54.Caption = .Text
         Label54.BackColor = .DisplayFormat.Interior.Color
      End With
   End With
End Sub
 
Upvote 0
There's no need for the select case, you can do it like
VBA Code:
Private Sub UserForm_Initialize()
   With Sheets("5S Worksheet")
      With .Range("K12")
         Label51.Caption = .Text
         Label51.BackColor = .DisplayFormat.Interior.Color
      End With
      With .Range("K14")
         Label52.Caption = .Text
         Label52.BackColor = .DisplayFormat.Interior.Color
      End With
      With .Range("K16")
         Label53.Caption = .Text
         Label53.BackColor = .DisplayFormat.Interior.Color
      End With
      With .Range("K18")
         Label54.Caption = .Text
         Label54.BackColor = .DisplayFormat.Interior.Color
      End With
   End With
End Sub
And I almost dropped an F-Bomb in the office. :ROFLMAO: Thank you very much seeing as this has been with me for last two days.??
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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