我需要一些建议。
我的代码检查工作表“总计”中的单元格“E”与工作表“列表”中的单元格“B”,如果值相等,则读取工作表“列表”中的单元格“A”(其中包含所有我的床单),并将匹配线复制到正确的床单中。
我的脚本有效,但速度很慢。你对如何加快这个过程有什么建议吗?
目前脚本逐行读取和复制,我想通过应用自动过滤器来加快进程,但不知道从哪里开始......
提前致谢。
这是我的实际脚本:
Sub copystatus()
Dim LR As Long
Dim LC As Integer
Dim LB As Long
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim cLista As String
Set ws = ThisWorkbook.sheets("totale")
Set ws2 = ThisWorkbook.sheets("liste")
LR = ws.Cells(Rows.Count, 5).End(xlUp).Row
LC = ws2.Cells(Rows.Count, 2).End(xlUp).Row
With ws
For x = 2 To LR
For i = 2 To LC
If .Cells(x, 5).value = ws2.Cells(i, 2).value Then
cLista = ws2.Cells(i, 1).value
Set ws3 = ThisWorkbook.sheets(cLista)
On Error GoTo ErrorHandler
LB = ws3.Cells(Rows.Count, 1).End(xlUp).Row
ws3.Rows(LB + 1).value = .Rows(x).value
ws3.Rows(1).value = .Rows(1).value
End If
Next i
Next x
End With
ErrorHandler:
End Sub
最佳答案
检查一下 - 增加应该是可见的:
Sub copystatus()
Dim LR As Long
Dim LC As Integer
Dim LB As Long
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim cLista As String
Application.ScreenUpdating = False
Application.EnableEvents = False
Set ws = ThisWorkbook.sheets("totale")
Set ws2 = ThisWorkbook.sheets("liste")
LR = ws.Cells(Rows.Count, 5).End(xlUp).Row
LC = ws2.Cells(Rows.Count, 2).End(xlUp).Row
With ws
For x = 2 To LR
For i = 2 To LC
If .Cells(x, 5).value = ws2.Cells(i, 2).value Then
cLista = ws2.Cells(i, 1).value
Set ws3 = ThisWorkbook.sheets(cLista)
On Error GoTo ErrorHandler
LB = ws3.Cells(Rows.Count, 1).End(xlUp).Row
ws3.Rows(LB + 1).value = .Rows(x).value
ws3.Rows(1).value = .Rows(1).value
End If
Next i
Next x
End With
Application.ScreenUpdating = True
Application.EnableEvents = True
ErrorHandler:
End Sub
最后将 ws、ws2、ws3 设置为 Nothing like this:
设置 ws = 无
设置 ws2 = 无
关于Vba Excel - 如果值=值过滤并复制到正确工作表上 - 加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39724038/