加速大型数据集的 VBA

问题描述 投票:0回答:1

我正在使用 VBA 代码通过使用 Excel 中的箭头来显示先例和依赖范围。该代码运行良好,但当我在大范围内使用它时,需要花费大量时间并挂起。请建议我如何更改代码以使其有效地处理大范围。

Sub TraceDependentsAndPrecedents()
      Dim xRg As Range
    Dim xCell As Range
    Dim xTxt As String
    
    On Error Resume Next
    xTxt = ActiveWindow.RangeSelection.Address
    Set xRg = Application.InputBox("Please select the data range:", , xTxt, , , , , 8)
    
    If xRg Is Nothing Then Exit Sub
    
    For Each xCell In xRg
        xCell.ShowPrecedents
        xCell.ShowDependents
    Next
End Sub

我想加快代码速度,使其更有效地处理大数据范围。代码使用箭头突出显示 Excel 中的先例和依赖项。

excel vba office-addins excel-addins
1个回答
1
投票

这是修改后的代码以限制正在检查的单元格:
我不知道您是否还可以做很多其他事情来加快速度。

Sub TraceDependentsAndPrecedents()
      Dim xRg As Range
    Dim xCell As Range
    Dim xTxt As String
    
    On Error Resume Next
    xTxt = ActiveWindow.RangeSelection.Address
    Set xRg = Application.InputBox("Please select the data range:", , xTxt, , , , , 8)
    
'limit cells to search
    Set xRg = Union(xRg.SpecialCells(xlCellTypeFormulas), xRg.SpecialCells(xlCellTypeConstants))
    
    If xRg Is Nothing Then Exit Sub
    
    For Each xCell In xRg
        xCell.ShowPrecedents
        xCell.ShowDependents
    Next
    
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.