Getting TextBox value to change color on textbox based on a other textbox

Wicked_

Board Regular
Joined
Jun 5, 2018
Messages
81
Hi.

I'm trying to make a userform textbox to change color depending on the input value.
I managed to get one of my textboxes to display the correct color based of the value, but then i got a second texbox, that is going to change the color based on a calculation from the first textbox, and the input that is put in the textbox 2, then it shows the color at wrong values.

This is what i got:

Private Sub syl_20_tip1_rk1_Change()
On Error Resume Next
Select Case syl_20_tip1_rk1.Value
Case 11.185 - 0.02 To 11.185 + 0.02:
syl_20_tip1_rk1.BackColor = vbGreen

Case 11.155 To 11.165:
syl_20_tip1_rk1.BackColor = vbYellow

Case 11.205 To 11.215:
syl_20_tip1_rk1.BackColor = vbYellow

Case 11.155 - 11.155 To 11.215 + 20:
syl_20_tip1_rk1.BackColor = vbRed

Case Else:
syl_20_tip1_rk1.BackColor = vbWhite
End Select


End Sub






Private Sub syl_20_tip1_rk2_Change()
On Error Resume Next
Select Case syl_20_tip1_rk2.Value
Case syl_20_tip1_rk1.Text - 0.041 To syl_20_tip1_rk1.Text + 0.01:
syl_20_tip1_rk2.BackColor = vbGreen

Case syl_20_tip1_rk1.Text - 0.041 To syl_20_tip1_rk1.Text - 0.01:
syl_20_tip1_rk2.BackColor = vbGreen

Case syl_20_tip1_rk1.Text - 0.041 To syl_20_tip1_rk1.Text - 0.02:
syl_20_tip1_rk2.BackColor = vbYellow

Case syl_20_tip1_rk1.Text - 0.041 To syl_20_tip1_rk1.Text + 0.02:
syl_20_tip1_rk2.BackColor = vbGreen

Case syl_20_tip1_rk1.Text - 0.041 To syl_20_tip1_rk1.Text + 0.03:
syl_20_tip1_rk2.BackColor = vbRed

Case Else:
syl_20_tip1_rk2.BackColor = vbWhite
End Select
End Sub


The first one, works just fine, but when i try to (in textbox2(which is not called textbox 1 and 2, just easier to write so you understand :) )) set a value in textbox 2, the calculations are off.
How can i improve on this one?
Also tried .value instead of .text, but no changes.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try
Code:
CDbl(syl_20_tip1_rk1.Text)-0.041
 
Upvote 0
Try
Code:
CDbl(syl_20_tip1_rk1.Text)-0.041

Hmm. i tried that, and CDbl(syl_20_tip1_rk1.Text)-0.041 To CDbl(syl_20_tip1_rk1.Text)+0,01: , but the the calculations is still off.

To explain a bit more, just incase i described it poorly.

On the first one, i got Case 11.185 - 0.02 To 11.185 + 0.02:

What i want with the second textbox, is if textbox1 is withtin these parameters, i want the textbox2 to show green if its "textbox1
- 0.041 to 0.01 " depending if both textbox 1 and 2 is withtin its parameters.

Kinda hard to explain really hehe.

If its to any help. I do the exact same with cells within conditional formatting, and with manage rules.

Is there a way to copy the rule from a cell to a spesific textbox? Example, if cell E4 = green, yellow or red, i want that spesific textbox to take the same color, with sub change. ?
 
Upvote 0
If you start with 11.185 in Txtbox1 then anything in txtbox2 from 11.144 to 11.195 will comply with the first case, which means that the 2nd & 3rd cases will never be checked.
 
Upvote 0
If you start with 11.185 in Txtbox1 then anything in txtbox2 from 11.144 to 11.195 will comply with the first case, which means that the 2nd & 3rd cases will never be checked.

Got it working, thanks :)

Last question:

Case 0 + 0.03 To 0 + 0.05:
syl_20_tip1_pr1.BackColor = vbYellow


Case 0 - 0.03 To 0 - 0.05:
syl_20_tip1_pr1.BackColor = vbYellow



When i type example 0,03 or 0,04 or 0,05 it changes to yellow, but if i type -0,03 or -0,04 or -0,05 it dosent change color at all.
Any reason why negative numbers wont change?
 
Upvote 0
try
Code:
Case -0.05 To -0.03
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

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