Vba Excel - 如果值=值过滤并复制到正确工作表上 - 加速

标签 vba excel

我需要一些建议。
我的代码检查工作表“总计”中的单元格“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/

相关文章:

vba - Excel VBA 调试 : Loop not searching through the whole range

ms-access - Access SQL 中的 CDec 的行为与从 Access VBA 使用时不同

excel - 循环并计算包含数据的 Excel 单元格,然后循环执行某个操作多次

python - 如何在不加载整个文件的情况下从 XLS 文件中获取工作表名称?

c# - 使用c#在Excel中生成动态生成的范围名称

excel - 带有 AND/OR 函数的 VBA Excel If 语句

vba - Excel 2010 VBA : find out if formatting style exists

excel - 对筛选值求和 Excel

vba - Excel 无法删除长破折号并替换为长破折号

python - 如何在python中为excel电子表格执行F9?