Вы можете использовать Regex, чтобы найти ссылку в формуле на другой лист:
Option Explicit
Function GetFormulaReference(sFormula As String) As String
Dim sPattern As String, sRng As String
Dim oRegex As RegExp, oMatches As Object, oMatch As Object
Debug.Print sFormula
sPattern = "\w+!\w{1,}\d{1,}"
Set oRegex = New RegExp
With oRegex
.Pattern = sPattern
Set oMatches = .Execute(sFormula)
For Each oMatch In oMatches
sRng = oMatch
Next
End With
Set oMatch = Nothing
Set oMatches = Nothing
Set oRegex = Nothing
GetFormulaReference = sRng
End Function
Использование:
Sub Test()
Dim sRng As String
sRng = GetFormulaReference("=IF(A1<0; ""Be careful A1 is negative!""; ""Sheet2!A1+2"");")
Debug.Print "Address: = '" & sRng & "'"
sRng = GetFormulaReference("=IF(A1<0; ""Be careful A1 is negative!""; ""OK"");")
Debug.Print "Address: = '" & sRng & "'"
End Sub
Результат:
=IF(A1<0; "Be careful A1 is negative!"; "Sheet2!A1+2");
Address: = 'Sheet2!A1'
=IF(A1<0; "Be careful A1 is negative!"; "OK");
Address: = ''
Примечание №1: вам необходимо добавить ссылку на Microsoft VBScript Regular Expressions 5.5
Примечание №2: шаблон выше будет работать для стиля A1
адресации, но не для R1C1
!
Final note: If GetFormulaReference
function returns empty string, then there's no reference to another sheet.
[EDIT]
For именованных диапазонов , вы можете использовать что-то вроде этого:
Function GetNamedRangeReference(sFormula) As String
Dim nms As Object, sName As String, sRetVal As String
Set nms = ActiveWorkbook.Names
For i = 1 To nms.Count
sName = nms(r).Name
If InStr(1, sFormula, sName, vbTextCompare)>0 Then sRetVal = nms(r).RefersToRange.Address 'return address instead of name
Next
GetNamedRangeReference = sRetVal
End Function
Примечание: Я не тестировал вышеуказанную функцию.
Удачи!