None Specific Code of VBA for my label to light up as long as the word is found

Akw47

Board Regular
Joined
Nov 6, 2020
Messages
90
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello I current have a userform with these code

Private Sub UserForm_Initialize()
Me.TextBox2.Text = CStr(ThisWorkbook.Sheets("Sheet1").Range("F15").Value)
End Sub

-> this help to make my textbox show the value of the cell F15, whenever initialized.

Next,

Private Sub TextBox2_Change()
If TextBox2.Text = "Hello" Then
Label1.BackColor = RGB(255, 128, 128)
Else
Label1.BackColor = RGB(255, 255, 255)
End If
End Sub

-> this code will help to make my LABEL turn colour when there is a 'Hello' in the cell, could you assist me on what changes to make if i want it to turn colour if there is "Hello boy" in the cell as well. means not 'Hello' specific. Thanks :)
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
-> this code will help to make my LABEL turn colour when there is a 'Hello' in the cell, could you assist me on what changes to make if i want it to turn colour if there is "Hello boy" in the cell as well. means not 'Hello' specific. Thanks :)

Hi,
Try this update to your code & see if does what you want

VBA Code:
Private Sub TextBox2_Change()
    Dim m As Variant
    m = Application.Match(TextBox2.Text, Array("Hello", "Hello Boy"), 0)
    
    Label1.BackColor = IIf(IsError(m), RGB(255, 255, 255), RGB(255, 128, 128))

End Sub

Dave
 
Upvote 0
Hi,
Try this update to your code & see if does what you want

VBA Code:
Private Sub TextBox2_Change()
    Dim m As Variant
    m = Application.Match(TextBox2.Text, Array("Hello", "Hello Boy"), 0)
   
    Label1.BackColor = IIf(IsError(m), RGB(255, 255, 255), RGB(255, 128, 128))

End Sub

Dave
Hi dave, thanks for your help. But this code only register for 'hello' and 'hello boy'. but what if i have a sentence hello, my name is jeff. I want it to light up, as long as there is a 'hello' inside.
 
Upvote 0
Hi dave, thanks for your help. But this code only register for 'hello' and 'hello boy'. but what if i have a sentence hello, my name is jeff. I want it to light up, as long as there is a 'hello' inside.

Based on your published code, solution does what I thought you were asking

You can try this update & see if helps

VBA Code:
Private Sub TextBox2_Change()
  
    Label1.BackColor = IIf(InStr(Me.TextBox2.Text, "Hello") > 0, RGB(255, 128, 128), RGB(255, 255, 255))

End Sub

Dave
 
Upvote 0
Based on your published code, solution does what I thought you were asking

You can try this update & see if helps

VBA Code:
Private Sub TextBox2_Change()
 
    Label1.BackColor = IIf(InStr(Me.TextBox2.Text, "Hello") > 0, RGB(255, 128, 128), RGB(255, 255, 255))

End Sub

Dave
this work well, thanks so much dmt32.
 
Upvote 0
dave just to check, but if its HELLO it doesn't work. can it be not case sensitive?

try
VBA Code:
Private Sub TextBox2_Change()
   
    Label1.BackColor = IIf(InStr(UCase(Me.TextBox2.Text), "HELLO") > 0, RGB(255, 128, 128), RGB(255, 255, 255))

End Sub

Dave
 
Upvote 0
Solution
try
VBA Code:
Private Sub TextBox2_Change()
  
    Label1.BackColor = IIf(InStr(UCase(Me.TextBox2.Text), "HELLO") > 0, RGB(255, 128, 128), RGB(255, 255, 255))

End Sub

Dave
u a legend dave :) appreciate u 100%
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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