2016-08-15 88 views
0

enter image description here
這是我創建的用戶表單。然後,它被用作輸入平臺。有一些不同的表格,例如: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 
+1

你可以做的事情不多 - 除了改成數據庫。您可以通過將用戶窗體控件保存到數組,然後將數組一次寫出到單元格區域來節省一些時間。但我認爲真正的障礙是用戶表單讀取和工作表寫入。 – dbmitch

+0

或使用ComboBox.LinkedCell屬性 – Slai

回答

2

通過將計算切換爲手動,然後在信息插入後計算,可以節省一些時間。

具體而言,在你的代碼的開始:

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 

這可以最小化工作表被擊中的次數。