You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在清空UserForm其他控件时保留指定TextBox内容?

解决搜索栏按下Enter后被清空的问题

嘿,我仔细看了你的代码,问题根源就在CommandButton6_Click事件里的控件清空逻辑——那段循环会把所有TextBox和ComboBox都清空,自然也包括你用作搜索栏的TextBox17(也就是你说的Txt1)。咱们只需要修改这段循环,跳过TextBox17就能解决问题。

修改方案

在清空控件的循环中,添加一个判断条件,排除TextBox17,这样它的内容就会被保留下来,其他需要清空的控件正常处理。

修改后的完整按钮代码

Private Sub CommandButton6_Click()
 If UserForm11.ComboBox4 = "TesellumFisi" Then
 If TextBox2.Text <> "" Then
 Son_Dolu_Satir = Sheets("TesellumFisi").Range("A65536").End(xlUp).Row
 For i = 23 To Son_Dolu_Satir
 If Sheets("TesellumFisi").Cells(i, 2).Value = TextBox2.Text Then Exit Sub
 End If
 Next i
 Bos_Satir = Son_Dolu_Satir + 1
 Sheets("TesellumFisi").Range("A" & Bos_Satir).Value = Application.WorksheetFunction.Max(Sheets("TesellumFisi").Range("A:A")) + 1
 Sheets("TesellumFisi").Range("B" & Bos_Satir).Value = TextBox2.Text
 Sheets("TesellumFisi").Range("C" & Bos_Satir).Value = TextBox13.Text
 Sheets("TesellumFisi").Range("E" & Bos_Satir).Value = TextBox9.Text
 Sheets("TesellumFisi").Range("I" & Bos_Satir).Value = TextBox4.Text
 Sheets("TesellumFisi").Range("J" & Bos_Satir).Value = TextBox6.Text
 Sheets("TesellumFisi").Range("O13").Value = UserForm11.ComboBox1.Text
 Sheets("TesellumFisi").Range("H" & Bos_Satir).Value = UserForm11.ComboBox2.Text
 Sheets("TesellumFisi").Range("G" & Bos_Satir).Value = TextBox8.Text
 End If
 End If
 Dim TC As Control
 For Each TC In Controls
 ' 保留TextBox17的内容,其他文本框和组合框清空
 If (TypeName(TC) = "TextBox" Or TypeName(TC) = "ComboBox") And TC.Name <> "TextBox17" Then
 TC.Value = ""
 End If
 Next TC
 Set TC = Nothing
 '-- TextBox2.SetFocus
 TextBox2.SelStart = 1
 TextBox2.SelLength = Len(TextBox2.Text)
End Sub

关键修改点说明

原来的循环是无条件清空所有TextBox和ComboBox,现在我们加了And TC.Name <> "TextBox17"这个判断:

  • 当遍历到的控件是TextBox17时,跳过清空操作
  • 其他符合条件的控件依然会被正常清空

如果之后你还有其他不想清空的控件(比如某个特定的ComboBox),只需要在条件里继续追加And TC.Name <> "目标控件名称"就可以了。

内容的提问来源于stack exchange,提问作者user11902097

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:06:37