如何提高excel VBA中userform的保存速度
问题描述:
这是我创建的用户表单。然后,它被用作输入平台。有一些不同的表格,例如:2016年,2017年....该保存按钮的逻辑是搜索年份(日期)用户输入和位置正确的工作表。然后,它会找到该工作表的最后一行。如何提高excel VBA中userform的保存速度
例如,最后一行是行1000中的用户窗体将节省用户窗体上的行1001.The第二行的第一行就会节省1002行....
问题但是,当我在真正的excel文件中测试时,保存速度太慢。真正的excel文件很大(每个工作表中大约有1XXXX行)。它使用8秒为userform保存一行,13秒保存两行。
显然,节约的速度是不可接受的。任何方法都可以改进它?
If ComboBox3.Value = "2016" Then
Worksheets("2016").Activate
j = WorksheetFunction.CountA(Worksheets("2016").Range("A:A")) + 1
End If
If ComboBox3.Value = "2017" Then
Worksheets("2017").Activate
j = WorksheetFunction.CountA(Worksheets("2017").Range("A:A")) + 1
End If
'1st
If ComboBox4.Value = "" Then
Else
Cells(j, 1) = ComboBox434.Value
Cells(j, 5) = ComboBox1.Value
Cells(j, 4) = ComboBox2.Value
Cells(j, 3) = ComboBox3.Value
If ComboBox4.ListIndex <> -1 Then
Cells(j, 6) = TimeValue(ComboBox4.Value & ":" & ComboBox5.Value)
Cells(j, 24) = ComboBox4.Value
Cells(j, 25) = ComboBox5.Value
Else
Cells(j, 6) = ""
End If
Cells(j, 7) = ComboBox6.Value
Cells(j, 8) = ComboBox7.Value
Cells(j, 9) = ComboBox8.Value
Cells(j, 10) = TextBox2.Value
Cells(j, 11) = TextBox3.Value
Cells(j, 12) = TextBox4.Value
Cells(j, 13) = TextBox5.Value
Cells(j, 14) = TextBox42.Value
Cells(j, 15) = TextBox43.Value
Cells(j, 16) = TextBox44.Value
Cells(j, 17) = TextBox666.Value
'If ComboBox4.Value = "" Then
End If
'2nd
j = j + 1
If ComboBox9.Value = "" Then
Else
Cells(j, 1) = ComboBox434.Value
Cells(j, 5) = ComboBox1.Value
Cells(j, 4) = ComboBox2.Value
Cells(j, 3) = ComboBox3.Value
If ComboBox9.ListIndex <> -1 Then
Cells(j, 6) = TimeValue(ComboBox9.Value & ":" & ComboBox10.Value)
Cells(j, 24) = ComboBox9.Value
Cells(j, 25) = ComboBox10.Value
Else
Cells(j, 6) = ""
End If
Cells(j, 7) = ComboBox11.Value
Cells(j, 8) = ComboBox12.Value
Cells(j, 9) = ComboBox13.Value
Cells(j, 10) = TextBox6.Value
Cells(j, 11) = TextBox7.Value
Cells(j, 12) = TextBox8.Value
Cells(j, 13) = TextBox9.Value
Cells(j, 14) = TextBox45.Value
Cells(j, 15) = TextBox46.Value
Cells(j, 16) = TextBox47.Value
Cells(j, 17) = TextBox617.Value
答
通过将计算切换为手动,然后在信息插入后计算,可以节省一些时间。
具体而言,在你的代码的开始:
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
,并再次在你的代码的末尾:
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
此外,你可能想存储在一个变量数组数据,它可以一次性插入到工作表中,或者按照阵列的大小调整阵列大小并调整其大小
例如
Cells(j, 1).resize(Ubound(arr,1), Ubound(arr,2)) = arr 'Need to check for exact syntax
这可以最小化工作表被击中的次数。
你可以做的事情不多 - 除了改成数据库。您可以通过将用户窗体控件保存到数组,然后将数组一次写出到单元格区域来节省一些时间。但我认为真正的障碍是用户表单读取和工作表写入。 – dbmitch
或使用ComboBox.LinkedCell属性 – Slai