如何提高excel VBA中用户表单的存储速度

在这里输入图像说明
这是我创build的用户表单。 然后,它被用作input平台。有一些不同的表,例如2016年,2017年….这个保存button的逻辑是search年(date),用户input和位置正确的工作表。 然后,它会find该工作表的最后一行。

例如,最后一行是行1000.用户窗体的第一行将保存在行1001上。用户窗体的第二行将保存在行1002 ….

但是,当我在真正的excel文件中testing时,保存速度太慢。真正的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 

另外,您可能希望将数据存储在variables数组中,该数组可以一次性插入到工作表中,也可以将数组和Cells(j,1)的大小调整为数组大小

例如

 Cells(j, 1).resize(Ubound(arr,1), Ubound(arr,2)) = arr 'Need to check for exact syntax 

这可以最小化工作表被击中的次数。