在用户表单上写入缓慢



我有一个用户表单,它只发生在第一次打开工作簿时,以便从用户那里获取项目信息。

表单非常基本,我有 12 个标签,每个标签有 1 个文本框、1 个组合框或最多 1 个文本框和 1 个组合框。

问题是当弹出此用户表单时,输入速度特别慢。只要此表单中没有任何主要代码,就不应该花费那么多时间。即使页面中没有任何自动计算,我将输入反映为输出。(这就是为什么这个问题对我没有帮助:如何在excel VBA中提高用户表单的保存速度)

(附言:ComboBox 的行源是命名范围,但我有另一种样式相同的表单,我没有遇到这个问题)

这是我的输出代码:

'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 时(即用户键入字符),代码都会将值写入工作簿(工作簿将重新计算依赖于该单元格的任何内容)。那会很慢。

除非您需要在输入每个字段时存储所有信息(在这种情况下,我至少会更改为TextBox1_Exit事件而不是TextBox1_Change),否则我建议您在用户单击某种"确定"按钮时将所有文本框和组合框的内容写入工作表。

(我不会使用表单的 Click 事件 - 用户很容易意外单击表单上的某处,从而在准备好之前关闭表单。

我建议你:

  • 创建一个按钮 - 默认情况下它将被称为 CommandButton1,但如果您愿意,可以将其Name更改为"OKButton"之类的内容。

  • 将按钮的Caption更改为"确定"或类似内容,以便用户知道在输入所有信息后单击它。

  • 将代码放入名为CommandButton1_Click的 sub 中(如果将按钮重命名为OKButton,则OKButton_Click)。

然后,当用户单击该按钮时,代码将运行。

可能的代码:

Private Sub CommandButton1_Click()        
'Project Name Input
Sheet2.Range("E3").Value = TextBox7.Value
'Customer Name input
Sheet2.Range("E4").Value = TextBox1.Value
'Region Input
Sheet2.Range("E5").Value = ComboBox1.Value
'City Input
Sheet2.Range("G5").Value = TextBox6.Value
'Bid Currency Input
Sheet2.Range("E6").Value = ComboBox2.Value
'Industry Type Input
Sheet2.Range("E7").Value = ComboBox3.Value
'Application Type Input
Sheet2.Range("E8").Value = ComboBox4.Value
'Opportunity/Job Number Input
Sheet2.Range("E9").Value = TextBox5.Value
'Plant Capacity First Input
Sheet2.Range("E10").Value = TextBox4.Value
'Plant Capacity Second (Unit) Input
Sheet2.Range("F10").Value = ComboBox5.Value
'Number of Trains First Input (each)
Sheet2.Range("E11").Value = TextBox2.Value
'Number of Trains Second Input (percentage)
Sheet2.Range("g11").Value = TextBox3.Value
'Process Type 1st Input
Sheet2.Range("E12").Value = ComboBox6.Value
'Process Type 2nd Input
Sheet2.Range("f12").Value = ComboBox10.Value
'Process Type 3rd Input
Sheet2.Range("g12").Value = ComboBox9.Value
'Process Type 4th Input
Sheet2.Range("H12").Value = ComboBox8.Value
'Engineering Specifications Input
Sheet2.Range("e13").Value = ComboBox7.Value
Unload Me
End Sub

最新更新