Excel 如何使此VBA循环运行得更快?

Excel 如何使此VBA循环运行得更快?,excel,nested-loops,vba,Excel,Nested Loops,Vba,这个为Excel编写的循环获取两个唯一列表的范围,并在不同工作表的表中搜索它们。这是一个两列搜索,列表中的两个值必须显示在一行中,累加器才能计数。它工作得很好,但当我解析大量数据时,我可能会连续等待几分钟。我正在寻找一种方法,使这个循环更快。任何帮助都将不胜感激。提前谢谢 Sub parseTwo(ByVal startRng As Range, ByVal findRng As Range, _ ByVal pasteStartRng As Range, ByVal strTitle As S

这个为Excel编写的循环获取两个唯一列表的范围,并在不同工作表的表中搜索它们。这是一个两列搜索,列表中的两个值必须显示在一行中,累加器才能计数。它工作得很好,但当我解析大量数据时,我可能会连续等待几分钟。我正在寻找一种方法,使这个循环更快。任何帮助都将不胜感激。提前谢谢

Sub parseTwo(ByVal startRng As Range, ByVal findRng As Range, _
ByVal pasteStartRng As Range, ByVal strTitle As String, ByVal findTableColumn As String, _
ByVal startOffset As Integer, ByVal handledOffset As Integer, _
ByVal handledBool As Boolean)
'==========================================================================
'==========================================================================
'Turn off some Excel functionality so code runs faster
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
'==========================================================================
'==========================================================================
Dim x As Long           'Declare accumulator.
x = 0                   'Give x default value.
'==========================================================================
'==========================================================================
Dim firstLoop As Boolean 'Declare boolean value.
firstLoop = True         'Declare initial value of boolean as true.
'==========================================================================
'==========================================================================
Dim pasteFindRng As Range 'Set the paste range for "find" items.
Set pasteFindRng = pasteStartRng.Offset(1, -1
Dim pasteAccum As Range   'Set the paste range for the "accumulator".
Set pasteAccum = pasteStartRng.Offset(1, 0)
'==========================================================================
'==========================================================================
Dim initialFindRng As Range 'Keep track of the initial "find" range to reference it later.
Set initialFindRng = findRng
'==========================================================================
'==========================================================================
Do While startRng.Text <> vbNullString             'Do while there is data in the "start" range.
    Do While findRng.Text <> vbNullString          'Do while there is data in the "find" range.
        With Worksheets("Formatting").Range("FormattingTable[" & findTableColumn & "]")
            Set c = .Find(findRng.Text, LookIn:=xlValues, LookAt:=xlWhole)
                    firstAddress = c.Address
                    Do
                        If handledBool = True Then
                            If c.Offset(0, handledOffset).Text <> vbNullString Then
                                If c.Offset(0, startOffset).Text = startRng.Text Then
                                    x = x + 1
                                End If
                            End If
                        Else
                            If c.Offset(0, startOffset).Text = startRng.Text Then
                                x = x + 1
                            End If
                        End If
                    Set c = .FindNext(c)
                    Loop While Not c Is Nothing And c.Address <> firstAddress
        End With
'==========================================================================
'==========================================================================
        If firstLoop = True Then   'If this is the first time through loop then paste find items
            pasteFindRng.Value = findRng.Text
            Set pasteFindRng = pasteFindRng.Offset(1, 0) 'Set pastefind range down 1
        End If
'==========================================================================
        pasteAccum.Value = x                  'Set x to paste.
        Set pasteAccum = pasteAccum.Offset(1, 0)    'Set accumulator paste range down 1.
        x = 0                                       'Reset x
'==========================================================================
        Set findRng = findRng.Offset(1, 0)          'Set find range down 1.
'==========================================================================
    Loop
    If firstLoop = True Then 'If this is the first time through loop then paste the title.
        pasteStartRng.Offset(0, -1) = strTitle
    End I
'==========================================================================
    pasteStartRng.Value = startRng.Text             'Paste the value of the start range.
'==========================================================================
    Set pasteStartRng = pasteStartRng.Offset(0, 1)   'Set paste start range over to the right 1.
'==========================================================================
    Set pasteAccum = pasteStartRng.Offset(1, 0)     'Reset "accumulator" paste range.
'==========================================================================
    Set startRng = startRng.Offset(1, 0)            'Move "start" range down 1.
    Set findRng = initialFindRng                    'Reset "find" range.
'==========================================================================
    firstLoop = False
Loop
'========================================================================================
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True

End Sub
Sub-parseTwo(ByVal startRng作为范围,ByVal findRng作为范围_
ByVal pasteStartRng作为范围,ByVal strTitle作为字符串,ByVal findTableColumn作为字符串_
ByVal startOffset为整数,ByVal handledOffset为整数_
ByVal handledBool(作为布尔值)
'==========================================================================
'==========================================================================
'关闭某些Excel功能,使代码运行更快
Application.ScreenUpdating=False
Application.DisplayStatusBar=False
Application.Calculation=xlCalculationManual
Application.EnableEvents=False
'==========================================================================
'==========================================================================
尺寸x为“长”表示累加器。
x=0'给出x的默认值。
'==========================================================================
'==========================================================================
将firstLoop设置为布尔值“声明布尔值”。
firstLoop=True'将布尔值的初始值声明为True。
'==========================================================================
'==========================================================================
Dim pasteFindRng As Range“设置“查找”项目的粘贴范围。
设置pasteFindRng=pasteStartRng.Offset(1,-1
Dim pasteAccum As Range“设置“累加器”的粘贴范围。
设置pasteAccum=pasteStartRng.Offset(1,0)
'==========================================================================
'==========================================================================
Dim initialFindRng As Range'跟踪初始“查找”范围,以便以后参考。
设置initialFindRng=findRng
'==========================================================================
'==========================================================================
开始时执行。Text vbNullString“开始”范围内有数据时执行。
findRng时执行。Text vbNullString“查找”范围内有数据时执行。
使用工作表(“格式化”).Range(“格式化表格[”&findTableColumn&“]))
设置c=.Find(findRng.Text,LookIn:=xlValues,LookAt:=xlWhole)
firstAddress=c.地址
做
如果handledBool=True,则
如果c.Offset(0,handledOffset).Text vbNullString,则
如果c.Offset(0,startOffset).Text=startRng.Text,则
x=x+1
如果结束
如果结束
其他的
如果c.Offset(0,startOffset).Text=startRng.Text,则
x=x+1
如果结束
如果结束
集合c=.FindNext(c)
循环而不是c为Nothing,c.Address为firstAddress
以
'==========================================================================
'==========================================================================
如果firstLoop=True,则“如果这是第一次通过循环,则粘贴查找项
pasteFindRng.Value=findRng.Text
Set-pasteFindRng=pasteFindRng.Offset(1,0)'将pastefind范围设置为向下1
如果结束
'==========================================================================
pasteAccum.Value=x'将x设置为粘贴。
设置pasteAccum=pasteAccum.Offset(1,0)'将累加器粘贴范围设置为向下1。
x=0'重置x
'==========================================================================
设置findRng=findRng.Offset(1,0)'将查找范围向下设置1。
'==========================================================================
环
如果firstLoop=True,则“如果这是第一次通过循环,则粘贴标题。
粘贴开始偏移量(0,-1)=标准件
完一
'==========================================================================
pasteStartRng.Value=startRng.Text'粘贴起始范围的值。
'==========================================================================
Set pasteStartRng=pasteStartRng.Offset(0,1)'将粘贴开始范围设置到右侧1。
'==========================================================================
设置pasteAccum=pasteStartRng.Offset(1,0)“重置”累加器“粘贴范围。
'==========================================================================
设置startRng=startRng.Offset(1,0)'将“start”范围向下移动1。
Set findRng=初始化findRng'重置“查找”范围。
'==========================================================================
firstLoop=False
环
'========================================================================================
Application.ScreenUpdating=True
Application.DisplayStatusBar=True
Application.Calculation=xlCalculationAutomatic
Application.EnableEvents=True
端接头

作为提示,尝试使用

Range("FormattingTable[" & findTableColumn & "]")
值,并避免在循环内执行此操作。或者最好替换:

Worksheets("Formatting").Range("FormattingTable[" & findTableColumn & "]")

通过循环中的范围值。

有没有理由不使用SUMIFS或DSUM或任何其他内置机制来实现这类功能?谢谢。我会检查这些函数。我以前做过一些研究,但没有