Vba 仅复制和粘贴未受保护的单元格花费的时间太长
我写了这个宏,但运行起来真的太长了。我想不出如何让它跑得更快 它的作用:它检查工作表中的每个单元格(Roh),如果它受到保护(锁定),那么它将跳过此单元格并转到下一个单元格,如果它不受保护,它将复制并粘贴另一个工作表中的值(导入)。两张纸的格式相同。我使用它将当前项目更新为新版本。硬拷贝粘贴不起作用,因为某些公式会更改。 谁能帮我找到一条更快的路吗 谢谢!:)Vba 仅复制和粘贴未受保护的单元格花费的时间太长,vba,excel,Vba,Excel,我写了这个宏,但运行起来真的太长了。我想不出如何让它跑得更快 它的作用:它检查工作表中的每个单元格(Roh),如果它受到保护(锁定),那么它将跳过此单元格并转到下一个单元格,如果它不受保护,它将复制并粘贴另一个工作表中的值(导入)。两张纸的格式相同。我使用它将当前项目更新为新版本。硬拷贝粘贴不起作用,因为某些公式会更改。 谁能帮我找到一条更快的路吗 谢谢!:) 您可以尝试使用数组来提高例程的速度。只需使用您的Import范围更新inrn Option Explicit Sub import()
您可以尝试使用数组来提高例程的速度。只需使用您的
Import
范围更新inrn
Option Explicit
Sub import()
Dim srcSht As Worksheet, destSht As Worksheet
Dim inRng As Range
Dim inArr As Variant, lockArr As Variant
Dim i As Long, j As Long
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer
With ThisWorkbook
Set srcSht = .Sheets("Import")
Set destSht = .Sheets("Roh")
End With
With srcSht
Set inRng = .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, .Cells(.Cells(1, .Columns.Count).End(xlToLeft).Column)))
End With
inArr = inRng.Value2
ReDim outArr(LBound(inArr, 1) To UBound(inArr, 1), LBound(inArr, 2) To UBound(inArr, 2))
For i = 1 To inRng.Rows.Count
For j = 1 To inRng.Columns.Count
If Not inRng.Cells(i, j).Locked Then
outArr(i, j) = inArr(i, j)
End If
Next j
Next i
With destSht
.Cells(1, 1).Resize(UBound(outArr, 2), UBound(outArr, 1)).Value2 = outArr
End With
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "Dieser Code wurde in " & MinutesElapsed & " Minuten erfolgreich ausgeführt.", vbInformation
End Sub
您可以尝试使用数组来提高例程的速度。只需使用您的
Import
范围更新inrn
Option Explicit
Sub import()
Dim srcSht As Worksheet, destSht As Worksheet
Dim inRng As Range
Dim inArr As Variant, lockArr As Variant
Dim i As Long, j As Long
Dim StartTime As Double
Dim MinutesElapsed As String
StartTime = Timer
With ThisWorkbook
Set srcSht = .Sheets("Import")
Set destSht = .Sheets("Roh")
End With
With srcSht
Set inRng = .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, .Cells(.Cells(1, .Columns.Count).End(xlToLeft).Column)))
End With
inArr = inRng.Value2
ReDim outArr(LBound(inArr, 1) To UBound(inArr, 1), LBound(inArr, 2) To UBound(inArr, 2))
For i = 1 To inRng.Rows.Count
For j = 1 To inRng.Columns.Count
If Not inRng.Cells(i, j).Locked Then
outArr(i, j) = inArr(i, j)
End If
Next j
Next i
With destSht
.Cells(1, 1).Resize(UBound(outArr, 2), UBound(outArr, 1)).Value2 = outArr
End With
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "Dieser Code wurde in " & MinutesElapsed & " Minuten erfolgreich ausgeführt.", vbInformation
End Sub
为了提高速度,您可以在文件末尾添加以下两个内容:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
xxxxxxxxxxx
{ Code }
xxxxxxxxxxx
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False
屏幕更新禁用实时操作可视化(例如,当您在不同的工作表上复制粘贴时,请避免屏幕闪烁)
计算避免在每次操作时重新计算数据 为了提高速度,您可以在文件的开头和结尾添加以下两个内容:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
xxxxxxxxxxx
{ Code }
xxxxxxxxxxx
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = False
屏幕更新禁用实时操作可视化(例如,当您在不同的工作表上复制粘贴时,请避免屏幕闪烁)
计算避免在每次操作时重新计算数据 开始时使用
Application.Calculation=xlCalculationManual
,结束时使用Application.Calculation=xlCalculationAutomatic
。这将有助于提高速度。非常感谢!这工作做得很好!而不是等待55分钟,现在只需要3分钟开始时使用Application.Calculation=xlCalculationManual
,结束时使用Application.Calculation=xlCalculationAutomatic
。这将有助于提高速度。非常感谢!这工作做得很好!而不是等待55分钟,现在只需要3分钟