【发布时间】:2017-05-08 01:54:24
【问题描述】:
我有一个用户表单,它只在第一次打开工作簿时出现,以从用户那里获取项目信息。
表单非常基本,我有 12 个标签,每个标签有 1 个文本框、1 个组合框或最多 1 个文本框和 1 个组合框。
问题是当这个用户表单弹出时,输入速度特别慢。只要此表单中没有任何主要代码,就不会花费太多时间。即使在我将输入反映为输出的页面中也没有任何自动计算。(这就是为什么这个问题对我没有帮助:How to improve the saving speed for userform in excel VBA)
(P.S:ComboBoxes 的行源是命名范围,但我有另一种样式相同的表单,我没有遇到这个问题)
这是我的输出代码:
'Project Name Input
Private Sub TextBox7_Change()
Sheet2.Range("E3").Value = TextBox7.Value
End Sub
'Customer Name input
Private Sub TextBox1_Change()
Sheet2.Range("E4").Value = TextBox1.Value
End Sub
'Region Input
Private Sub ComboBox1_Change()
Sheet2.Range("E5").Value = ComboBox1.Value
End Sub
'City Input
Private Sub TextBox6_Change()
Sheet2.Range("G5").Value = TextBox6.Value
End Sub
'Bid Currency Input
Private Sub ComboBox2_Change()
Sheet2.Range("E6").Value = ComboBox2.Value
End Sub
'Industry Type Input
Private Sub ComboBox3_Change()
Sheet2.Range("E7").Value = ComboBox3.Value
End Sub
'Application Type Input
Private Sub ComboBox4_Change()
Sheet2.Range("E8").Value = ComboBox4.Value
End Sub
'Opportunity/Job Number Input
Private Sub TextBox5_Change()
Sheet2.Range("E9").Value = TextBox5.Value
End Sub
'Plant Capacity First Input
Private Sub TextBox4_Change()
Sheet2.Range("E10").Value = TextBox4.Value
End Sub
'Plant Capacity Second (Unit) Input
Private Sub ComboBox5_Change()
Sheet2.Range("F10").Value = ComboBox5.Value
End Sub
'Number of Trains First Input (each)
Private Sub TextBox2_Change()
Sheet2.Range("E11").Value = TextBox2.Value
End Sub
'Number of Trains Second Input (percentage)
Private Sub TextBox3_Change()
Sheet2.Range("g11").Value = TextBox3.Value
End Sub
'Process Type 1st Input
Private Sub ComboBox6_Change()
Sheet2.Range("E12").Value = ComboBox6.Value
End Sub
'Process Type 2nd Input
Private Sub ComboBox10_Change()
Sheet2.Range("f12").Value = ComboBox10.Value
End Sub
'Process Type 3rd Input
Private Sub ComboBox9_Change()
Sheet2.Range("g12").Value = ComboBox9.Value
End Sub
'Process Type 4th Input
Private Sub ComboBox8_Change()
Sheet2.Range("H12").Value = ComboBox8.Value
End Sub
'Engineering Specifications Input
Private Sub ComboBox7_Change()
Sheet2.Range("e13").Value = ComboBox7.Value
End Sub
Private Sub UserForm_Click()
Unload Me
End Sub
所以,为了更清楚,举个例子:因为第一个输入是项目名称,当我开始输入项目名称时,当我按下每个单词后,几乎有第二个延迟继续写,那肯定是不正常。
【问题讨论】:
-
您已经进行了设置,以便每次更改 TextBox(即用户键入字符)时,代码都会将值写入工作簿(并且工作簿将重新计算依赖于该值的任何内容单元格) - 这会很慢。您应该只在用户完成输入后更新工作簿。
-
我的目标是只更新最后一行的工作簿,即 Userform_Click ==> Unload Me。但是嗯,如果它不起作用,那么我应该把所有的代码都放在一个私人子里吗?
-
除非您需要在输入每个字段时存储所有信息(在这种情况下,我至少会更改为
TextBox1_Exit事件而不是TextBox1_Change)然后,是的,当用户单击某种“确定”按钮时,我会将所有文本框和组合框的内容写入工作表。 (我不会使用表单的Click事件 - 用户太容易不小心单击表单上的某个位置,因此在他们准备好之前关闭表单。) -
那么我的私人潜艇的名字应该是什么?我尝试将所有这些 ComboBox.Value 和 TextBox.Value 操作放入一个子中,例如 Private Sub InputUserForm() ,速度变得正常,但是当我关闭表单时,输出不会通过工作表 在这种情况下。我又错过了什么? (我知道从现在开始这是一个非常基本的问题,但我只是卡住了。)
-
创建一个按钮 - 默认情况下它将被称为
CommandButton1- 将按钮的Caption更改为“OK”(或类似的东西) - 然后将您的代码放入名为CommandButton1_Click的子程序中. (或者给按钮一个新的、非默认的Name,例如“OKButton”,并使用OKButton_Click。)然后,当用户单击按钮时,您的代码将运行。
标签: excel forms vba combobox userform