【发布时间】:2019-04-24 14:46:24
【问题描述】:
我有一个带有多个控件(文本框)的用户窗体。这些文本框将通过选择 ListBox 项来填充。 初始化用户窗体时,这些文本框将分配给处理它们的特定类。
我希望 VBA 仅在对值进行真正更改时才更改这些文本框的背景颜色。我所拥有的是,一旦执行更改,BackgroundColor 总是会更改,但这不是我想要的。
示例 #1: 更改前的文本框值: "test" 更改后的文本框值: "test2" --> BackgroundColor 应该改变
示例 #2: 更改前的文本框值: "test" 更改后的文本框值:“test bla”,但我再次输入“test”。 --> BackgroundColor 不应该改变,因为初始值又在 TextBox 中了。
到目前为止我所拥有的:
' **************************************************************
' Module: clsTextbox Typ = Class Module
' **************************************************************
Public WithEvents mTextBoxs As MSForms.TextBox
Private Sub mTextBoxs_Change()
If mTextBoxs.Text = strInitialVal Then
Reset_BackColor
Else
mTextBoxs.BackColor = RGB(255, 255, 153)
End If
End Sub
Public Sub Reset_BackColor()
mTextBoxs.BackColor = RGB(255, 255, 255)
End Sub
' **************************************************************
' Module: frmEmployee Type = Userform
' **************************************************************
Dim arrLabels() As New clsLabel, UBoundarrLabels As Integer
Dim arrTextBoxs() As New clsTextbox, UBoundarrTextBoxs As Integer
Private Sub UserForm_Initialize()
Dim Ctrl As Control, obLabel As MSForms.Label, obTextbox As MSForms.TextBox
tblName = "tblMitarbeiter"
Set wb = ThisWorkbook
Set ws = wb.Sheets("Mitarbeiter")
i = 0
For Each Ctrl In Me.Controls
If Left(Ctrl.Name, 7) = "TextBox" Then
i = i + 1
ReDim Preserve arrTextBoxs(i)
Set obTextbox = Me.Controls("TextBox" & i)
Set arrTextBoxs(i).mTextBoxs = obTextbox
End If
Next Ctrl
' Fill Listbox1 with values (Vorname & Nachname) from Table [tblMitarbeiter]
Dim lngLastRow As Long: lngLastRow = getListLastRow(ws, tblName)
Dim vArrListBox1() As Variant
ReDim vArrListBox1(0 To lngLastRow - 1, 0 To 2)
For j = 1 To lngLastRow
vArrListBox1(j - 1, 0) = ws.ListObjects("tblMitarbeiter").DataBodyRange(j, 1).Value
vArrListBox1(j - 1, 1) = ws.ListObjects("tblMitarbeiter").DataBodyRange(j, 2).Value
vArrListBox1(j - 1, 2) = ws.ListObjects("tblMitarbeiter").DataBodyRange(j, 3).Value
Next j
For t = 1 To 4
Me.Controls("TextBox" & t) = vArrEmployee(t - 1)
Next t
strInitialVal = Me.Controls("TextBox2")
End Sub
我的想法是: 如您所见,我尝试在获取文本框(例如 TextBox2)的初始值的模块中声明一个公共变量(strInitialVal),并在执行 mTextBoxs_Change() 事件时检查 strInitialVal 是否与文本框等。 --> 这有效,但仅适用于变量和文本框的 1:1 关系。
如何将所有文本框值加载到数组中?然后检查 TextBox 类中的值。
如果您需要更多信息,请告诉我。我希望我没有违反任何 SO 规则。
【问题讨论】:
-
将您的初始值存储在 TextBox 的 .Tag 属性中,(或在您的类中的自定义属性中)更容易将其与实际值进行比较。每次在 TextBox 中输入时都会触发 Change-Event,因此最好使用 Afterupdate 或 Exit-event(但不能被 WithEvents 使用。
-
这么简单,这么好! .Tag 属性是一个非常好的主意!谢谢
标签: excel vba textbox userform