The alternative is to dive into programming the VBE itself. This function loops through the declaration section of the standard modules in the passed project to find any line that declares the passed variable name. It only checks the declarations section because scope matters and any procedural variables won't be seen by this function anyway.
Private Function DoesVariableExist(book As Workbook, varName As String) As Boolean
Const ProcKind As Integer = 0 'sub or function
Const vbext_ct_StdModule As Integer = 1 'standard modules only
Const DIMSTMNT As String = "Dim"
Const PUBLICSTMNT As String = "Public"
Const PRIVATESTMNT As String = "Private"
Const CONSTSTMNT As String = "Const"
Dim VBProj As Object
Dim VBComp As Object
Dim codeMod As Object
Dim procName As String
Dim lineNum As Long
Dim lineText As String
DoesVariableExist = False
'set object to components within the project
Set VBProj = book.VBProject.VBComponents
'loop through components
For Each VBComp In VBProj
'find standard modules
If VBComp.Type = vbext_ct_StdModule Then
'set object to code module
Set codeMod = VBComp.CodeModule
With codeMod
'loop through lines of code in the declaration section
For lineNum = 1 To .CountOfDeclarationLines
lineText = .Lines(lineNum, 1)
'check for declarartion statement and passed variable name (case-insensitive)
If (InStrB(lineText, DIMSTMNT) > 0 Or _
InStrB(lineText, PUBLICSTMNT) > 0 Or _
InStrB(lineText, PRIVATESTMNT) > 0 Or _
InStrB(lineText, CONSTSTMNT) > 0) And _
InStrB(LCase(lineText), LCase(varName)) > 0 Then
DoesVariableExist = True
Exit Function
End If
Next lineNum
End With
End If
Next VBComp
End Function
This comes with its own negatives, of course. You need to trust access to the VBA project object model (found in the Trust Center Settings), which can open you up for malicious code. But in an environment where you are not opening files from unknown sources, the trade-off can be well worth it.
As an aside, I'm using late binding here, so you don't have to set a reference to access the VBE objects.
Option Explicitand fix the code. You are asking for trouble here by having different code blocks do the same thing depending on a variable declaration.