我正在使用此代码进行命令单击事件,以便将具有某些条件的行从一个工作表复制到另一个工作表。加载到工作表中需要花费太多时间。
有没有办法加快速度?我是VBA excel的新手,我正在学习。
Dim lngLastRow As Long
Dim lngRow As Long
Dim strValue As String
Dim lngRowOutput As Long
' getting last row of Material Master data
lngLastRow = Sheets(2).UsedRange.Rows.Count
' MsgBox lngLastRow
Application.ScreenUpdating = False
' Clear down sheet from Row 2. Row 1 is for column headers.
Sheets(6).Range("2:1048570").Clear 'MM Criticality sheet
lngRowOutput = 2 ' where are we going to write the values to in Sheet2
For lngRow = 2 To lngLastRow
strValue = Sheets(2).Cells(lngRow, 5).Value ' getting value from column D
'Checking for particular text in the transactions..
If InStr(1, strValue, "specified in the table ", vbTextCompare) > 0 Then
Sheets(2).Rows(lngRow).Copy
Sheets(6).Rows(lngRowOutput).PasteSpecial
lngRowOutput = lngRowOutput + 1
Else
' MsgBox Sheets(3).Rows(lngRow).Copy
End If
Next lngRow
Application.ScreenUpdating = True
Worksheets(6).Activate
Worksheets(6).Visible = True
Worksheets(6).Select
End sub
答案 0 :(得分:0)
尝试下面的简单更改。而不是复制粘贴,具体参考值:
Dim lngLastRow As Long Dim lngRow As Long Dim strValue As String Dim lngRowOutput As Long
' getting last row of Material Master data
lngLastRow = Sheets(2).UsedRange.Rows.Count
' MsgBox lngLastRow
Application.ScreenUpdating = False
' Clear down sheet from Row 2. Row 1 is for column headers.
Sheets(6).Range("2:1048570").Clear 'MM Criticality sheet
lngRowOutput = 2 ' where are we going to write the values to in Sheet2
For lngRow = 2 To lngLastRow
strValue = Sheets(2).Cells(lngRow, 5).Value ' getting value from column D
'Checking for particular text in the transactions..
If InStr(1, strValue, "specified in the table ", vbTextCompare) > 0 Then
Sheets(6).Rows(lngRowOutput) = Sheets(2).Rows(lngRow).Value
lngRowOutput = lngRowOutput + 1
Else
' MsgBox Sheets(3).Rows(lngRow).Copy
End If
Next lngRow
Application.ScreenUpdating = True
Worksheets(6).Activate
Worksheets(6).Visible = True
Worksheets(6).Select
End sub