如何优化在Excel 2010 for 2016中编译的此宏?

时间:2019-01-14 21:51:38

标签: excel vba excel-2010 excel-2016

我有一个宏来搜索一个字符串,当它找到它时,它就会复制并粘贴值和格式。

它在2016年(当然是2010年)的运行速度非常缓慢。我一直无法弄清楚如何解决它。

const testRoutine2 = <T, U>(myFn: myFunctionType<T,U>) => { };

1 个答案:

答案 0 :(得分:0)

也许这样的事情将很快为您工作:

Sub CommandButton1_Click()


    Dim wb As Workbook
    Dim wsData As Worksheet
    Dim wsDest As Worksheet
    Dim rFind As Range
    Dim rCopy As Range
    Dim sFind As String
    Dim sFirst As String

    sFind = InputBox("Enter the string to search for:")
    If Len(sFind) = 0 Then Exit Sub 'Pressed cancel

    Set wb = ActiveWorkbook
    Set wsData = wb.ActiveSheet
    Set wsDest = wb.Worksheets("Report")

    With wsData.Range("G1:Z" & wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row)
        Set rFind = .Find(sFind, .Cells(.Rows.Count, .Columns.Count), xlValues, xlPart)
        If Not rFind Is Nothing Then
            sFirst = rFind.Address
            Set rCopy = rFind
            Do
                Set rCopy = Union(rCopy, rFind)
                Set rFind = .FindNext(rFind)
            Loop While rFind.Address <> sFirst

            Intersect(rCopy.Parent.Range("A:F"), rCopy.EntireRow).Copy
            wsDest.Range("A190").PasteSpecial xlPasteValues
            wsDest.Range("A190").PasteSpecial xlPasteFormats
        End If
    End With

End Sub