In the context of an error handling code, I would like to verify if the user has given to the current sheet the same name of another one into the same workbook (action forbidden, of course). So the way I intuitively tried to verify this was simply to loop through all the sheets and comparing the names:
For Each sh In ThisWorkbook.Sheets
If sh.Name = ThisWorkbook.ActiveSheet.Name Then
'error handling here
End If
Next sh
However, this is a huge logic fall in the case when:
1) The user is editing, let's say, the sheet number 3; 2) The sheet with the same name is at the position number 5;
In that case, the condition sh.Name = ThisWorkbook.ActiveSheet.Name
would be met for sure because the sheet is compared to itself.
So, I wonder: how to understand if sh
is not ThisWorkbook.ActiveSheet
?
I had thought the task it could have simply been solved with a simple object comparison:
If (sh Is Not ThisWorkbook.ActiveSheet) And (sh.Name = ThisWorkbook.ActiveSheet.Name) Then
but this raises a compile error, namely Object does not support this property or method. Could anyone please help me finding the lack of logic in my code's structure?
OTHER INFORMATION
I have tried to manage the case through the Err.Description
and the Err.Number
, but the first is OS-language dependent and the second is the same for other types of error I need to handle differently.
Moreover, the sheets (names and contents) are contained into a .xlam
add-in so the user can change the contents through custom user-forms but not through the Excel Application.
More in general, let's say that I would like to know how can I perform the comparison, even if a work-around in this specific case is possible, in order to use this method for future developments I already plan to do and that cannot be managed through the default VBA error handler.