本文介绍一个示例程序,利用vba设置数据表格式窗体的条件格式。
主要设计了一个函数,用于按条件进行设置
Function 设置条件格式(frm As Form, tj As String) As Boolean
Dim ctl As Control
Dim fcd As FormatCondition
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox or ctl.ControlType = acComboBox Then
intCount = 0
ctl.FormatConditions.Delete
ctl.FormatConditions.Add acExpression, acEqual, tj
Set fcd = ctl.FormatConditions.Item(intCount)
With fcd
.BackColor = 255 '背景色红色
.ForeColor = 0 '前景色黑色
End With
End If
Next ctl
设置条件格式 = True
Set ctl = Nothing
Set fcd = Nothing
End Function
窗体加载事件中的调用代码
Private Sub Form_Load()
设置条件格式 Me.窗体2.Form, "销售员='33'"
End Sub
组合框中更新后的调用代码
Private Sub Comb1_AfterUpdate()
If Len(Comb1) > 0 Then
设置条件格式 Me.窗体2.Form, "销售员='" & Comb1 & "'"
End If
End Sub
该函数使用两个参数,第一个代表窗体控件,第二个是条件
其中窗体2是窗体1的子窗体。
具体请参考附件:
点击下载此附件