Olá
Tenho este código numa userform (dentro de um evento listview_click) que verifica em 5 textbox se a data inserida é anterior à data actual.
O que eu gostaria de saber é como posso simplificar o código. Quero fazer o mesmo noutra userform que tem mais de 10 textbox e assim o código torna-se muito extenso.
'verificar a data de validade dos documentos
Dim CurrDate As Date
CurrDate = Now - 1
If txtValDss < CurrDate Then
txtValDss.ForeColor = vbRed
txtValDss.Font.Bold = True
Else: txtValDss.ForeColor = vbBlack
End If
If txtValDrf < CurrDate Then
txtValDrf.ForeColor = vbRed
txtValDrf.Font.Bold = True
Else: txtValDrf.ForeColor = vbBlack
End If
If txtValSrc < CurrDate Then
txtValSrc.ForeColor = vbRed
txtValSrc.Font.Bold = True
Else: txtValSrc.ForeColor = vbBlack
End If
If txtValSat < CurrDate Then
txtValSat.ForeColor = vbRed
txtValSat.Font.Bold = True
Else: txtValSat.ForeColor = vbBlack
End If
If txtValAlv < CurrDate Then
txtValAlv.ForeColor = vbRed
txtValAlv.Font.Bold = True
Else: txtValAlv.ForeColor = vbBlack
End If
Sub VerificarDatas(Formulario As Object)
Dim DataAtual As Date
Dim Controle As Object
DataAtual = Now - 1
For Each Controle In Formulario.Controls
If TypeName(Controle) = "TextBox" Then
Controle.ForeColor = IIf(Controle.Value < DataAtual, vbRed, vbBlack)
Controle.Font.Bold = IIf(Controle.Value < DataAtual, True, False)
End If
Next Controle
End Sub
Private Sub CommandButton1_Click()
Call VerificarDatas(Me)
End Sub
Vega
Muito obrigado pela ajuda.
O seu código funciona tal como eu desejo. Há apenas um senão:
Ele afecta todas as textbox com data e eu só pretendia afectar algumas onde o realce da condição é importante.
Como faço para conseguir isso?
Uma pequena nota: todas as textbox que pretendo afectar estão nomeadas com "Val...", como pode ver no código que postei. Existe alguma maneira de poder referenciar as textbox com esse prefixo?
Obrigado
Ioncila