iT邦幫忙

0

Excel-資料輸入至其他工作表(速度問題)

Sub 存到資料庫()
    'Application.ScreenUpdating = False
    'Application.EnableEvents = False
    'Application.Interactive = False
    
    '1.判斷儲存在資料庫的哪一列
    If Sheets("訂單資料庫").Range("A2") = "" Then
        r = 2
    Else
        r = Sheets("訂單資料庫").Range("A2").End(xlDown).Row + 1
    End If
    
    '訂單輸入介面Item欄位到第幾列
    r2 = Sheets("訂單輸入介面").Range("B4").End(xlDown).Row
    

    For i = 5 To r2
        'Item
        Sheets("訂單資料庫").Cells(r + i - 5, 2) = Sheets("訂單輸入介面").Cells(i, 2)
        '客戶、下單日期、交期等其他欄位依序輸入
        
    Next i

    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Interactive = True

End Sub

以上是按鈕的程式碼
這個按鈕主要的功能是把我在"訂單輸入介面"建立的資料輸出至"訂單資料庫"
問題是:當Item數量很高的時候(也就是迴圈數i),執行起來速度會非常慢,想請問各位是否有其他方式能夠達到一樣的效果,但是執行速度又高的呢?

不好意思,首次發問,如果有那裡有缺失還請見諒,謝謝!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
paicheng0111
iT邦大師 5 級 ‧ 2019-12-09 11:59:21
最佳解答

try this

Sub 存到資料庫()
    Dim r2 As Range
    
    Application.ScreenUpdating = False
    
    With Sheets("訂單輸入介面")
        Set r2 = .Range(.Range("B5"), .Range("B4").End(xlDown))
    End With
    
    With Sheets("訂單資料庫").Range("A2")
        If .Value = "" Then
            .resize(r2.rows.count, 1).value = r2.value
        Else
            .End(xlDown).Offset(1).resize(r2.rows.count, 1).value = r2.value
        End If
    End With
    Application.ScreenUpdating = True
End Sub

我要發表回答

立即登入回答