Highlight TextBox if the value loaded is the same in the worksheet

2

In a registration form when clicking search the textbox are filled. And if any of those TextBoxes has a value that appears in plan1 from the "AS" column then that TextBox has the highlighted text (eg Green). I got a code but it is not working.

In module:

Function ProcuraCheque(Cheque As String) As Boolean

    Dim Intervalo, Celula As Range

    Set Intervalo = Planilha1.Range("A1:B10") 'Informe aqui o intervalo onde estão os dados

    For Each Celula In Intervalo
        If Celula.Value = Cheque Then
        ProcuraCheque = True 'Verdadeiro se encontrado
        Exit For
        End If
    Next Celula

End Function

Não estou conseguindo chamr a função no evento Change ou AfterUpdate da TextBox, passando na função o valor da TextBox (tipo TextBox1.Value...). Caso ela seja verdadeira (If ProcuraCheque(Textbox1.Value)=True), o propriedade ForeColor da TextBox assume o valor vbRed, e se for Falso, vbBlack.

Private Sub TextAI1_Change()

If TextAI1.Value = "FunctionProcuraCheque" Then

    Me.TextAI1.ForeColor = &HFF0000
Else

    Me.TextAI1.ForeColor = &HFF&

End If
End Sub
    
asked by anonymous 28.06.2016 / 19:23

1 answer

1
Note that in this part of the code you are comparing the "content" of the TextBox with the "function name", unless the function name is in the text of the TextBox, which comes after the "Then" it will never run, only the part after the "Else" will run.

If TextAI1.Value = "FunctionProcuraCheque" then
    Me.TextAI1.ForeColor = &HFF0000
else
    Me.TextAI1.ForeColor = &HFF& '<== SOMENTE ESTA INSTRuÇÃO SERÁ EXECUTADA!
endif

I think you want to compare the contents of the TextBox with a cell in the worksheet.

    
28.06.2016 / 19:39