Hi!
I wonder if anyone can help me understand this in my opinion very strange VBA behavior (or if I miss something obvious). I have a Macro where I check if the content in an Excel cell can be interpreted as a cell reference. If so I do something if not I do something Else. A simple test example is submitted below.
As long as the active sheet when I execute the macro is a "normal" sheet it's no problem and the "If IsCellRef() Then" part of the code is executed. However if I execute the macro from a Chart-sheet the "Else" part of the code is executed even if (and this is the part I can't understand) the debugger tells the If-condition is True??
When running TestCellRef below from a Chart-Sheet and a break-point at the bold line it stops at the break-point and the debugger shows:
Watch : : IsCellRef(CellToTest) : True : Boolean : Module1.TestCellRef
Code example Excel 2013, Windows 7:
Strange or ??
I wonder if anyone can help me understand this in my opinion very strange VBA behavior (or if I miss something obvious). I have a Macro where I check if the content in an Excel cell can be interpreted as a cell reference. If so I do something if not I do something Else. A simple test example is submitted below.
As long as the active sheet when I execute the macro is a "normal" sheet it's no problem and the "If IsCellRef() Then" part of the code is executed. However if I execute the macro from a Chart-sheet the "Else" part of the code is executed even if (and this is the part I can't understand) the debugger tells the If-condition is True??
When running TestCellRef below from a Chart-Sheet and a break-point at the bold line it stops at the break-point and the debugger shows:
Watch : : IsCellRef(CellToTest) : True : Boolean : Module1.TestCellRef
Code example Excel 2013, Windows 7:
Rich (BB code):
Sub TestCellRef()
Set WsList = ThisWorkbook.Sheets("Sheet2")
Dim CellToTest As String
CellToTest = WsList.Range("A1").Value
If IsCellRef(CellToTest) Then 'Cell contains CellRef
' Do Something. In this simple example I just write OK in the cell referenced
WsList.Range(CellToTest).Value = "OK"
Else ' Cell is not a valid ref
' In this case I write NOK.
' Of course writing that using the not valid reference does not work if it's really not valid.
'WsList.Range("B1").Value = "NOK"
WsList.Range(CellToTest).Value = "NOK"
a = "Just for Breakepoint"
End If
End Sub
Function IsCellRef(CellRef As String) As Boolean
Dim TestRange As Range
On Error Resume Next
Set TestRange = Range(CellRef) 'Try to use the string as a referenced range, is address valid?
If Err.Number > 0 Then
IsCellRef = False
Else
IsCellRef = True
End If
End Function
Strange or ??
Last edited by a moderator: