首页 文章

Excel宏 - 查找在表格外使用的单元格

提问于
浏览
0

我使用标准的财务单元格格式,其中输入为蓝色,引用任何外表的单元格为绿色,其他所有内容均为黑色 .

一切都很好 - 我有能力开发基本上做GoTo - >常量 - >数字和GoTo - >公式的宏,然后在公式文本中查找“!”符号 .

但是有没有办法选择并突出显示(例如,紫色)所有在表格外使用的单元格,无论它们是作为常量或公式输入还是原始表单上的任何内容?

即:我希望能够快速找到并识别通过宏使用表格外的任何单元格 . 我擅长制作宏,但是不能想出任何可以实现这一目标的功能 . 任何人都可以给我一个暗示让我开始朝着正确的方向前进吗?

编辑:我到目前为止:

Sub Offsheet_Dependents()
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:", "EDI macro", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
' Need to modify the below for loop to only highlight cells where the reference is offsheet.  Currently higlights entire range.
' also need to add a cell.cleararrows command somewhere and have it work
For Each cell In xRg
    cell.ShowDependents
    Worksheet.cell.NavigateArrow TowardPrecedent:=False, ArrowNumber:=1, LinkNumber:=1
    If ActiveCell.Worksheet.Name <> Worksheet.cell.Worksheet.Name Then
        cell.Interior.Color = RGB(204, 192, 218)
    End If
    xRg.Select.ActiveSheet.ClearArrows
Next
End Sub

另一种可能性,但第二个宏没有成功应用范围内的第一个:(:

Sub Color_Dependents()
Dim rLast As Range, iLinkNum As Integer, iArrowNum As Integer
Dim stMsg As String
Dim bNewArrow As Boolean
Application.ScreenUpdating = False
ActiveCell.ShowDependents
Set rLast = ActiveCell
iArrowNum = 1
iLinkNum = 1
bNewArrow = True
Do
    Do
        Application.Goto rLast
        On Error Resume Next
        ActiveCell.NavigateArrow Towardprecedent:=False, ArrowNumber:=iArrowNum, LinkNumber:=iLinkNum
        If Err.Number > 0 Then Exit Do
        On Error GoTo 0
        If rLast.Address(External:=True) = ActiveCell.Address(External:=True) Then Exit Do
        bNewArrow = False
        If rLast.Worksheet.Parent.Name = ActiveCell.Worksheet.Parent.Name Then
            If rLast.Worksheet.Name = ActiveCell.Parent.Name Then
                 ' local
                stMsg = stMsg & vbNewLine & Selection.Address
            Else
                stMsg = stMsg & vbNewLine & "'" & Selection.Parent.Name & "'!" & Selection.Address
            End If
        Else
             ' external
            stMsg = stMsg & vbNewLine & Selection.Address(External:=True)
        End If
        iLinkNum = iLinkNum + 1 ' try another  link
    Loop
    If bNewArrow Then Exit Do
    iLinkNum = 1
    bNewArrow = True
    iArrowNum = iArrowNum + 1 'try another arrow
Loop
rLast.Parent.ClearArrows
Application.Goto rLast
If stMsg Like "*!*" Then
    ActiveCell.Interior.Color = RGB(204, 192, 218)
End If
End Sub


Sub Purple_Range()
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:", "EDI macro", xTxt, , , , , 8)
Set xRg = Application.Union(xRg, ActiveSheet.UsedRange)
If xRg Is Nothing Then Exit Sub
For Each cell In xRg
    Call Color_Dependents
Next cell
End Sub

1 回答

  • 1

    在Sub Purple_Range()中

    更换:

    For Each cell In xRg
        Cell.Select 
    Next cell
    

    有:

    For Each cell In xRg
        Cell.Select 
        Call Color_Dependents 
    Next Cell
    

    第二个宏失败的原因是因为Color_Dependents()正在更新当前ActiveCell的颜色,而Purple_Range()循环遍历该范围而没有更新ActiveCell的位置以使其成为当前 .

    否则宏工作正常 .

相关问题