> I'm having problems when trying to acess a sheet in a workbook if this
> sheet
> does'nt exist. I tried some "On Error" tricks....but it dodn't worked.
> Here is the piece of code where I try to activate the sheet:
> ...
> genesis_wb.Activate
> genesis_wb.Sheets(nome_al).Activate 'nome_al is a string
> ...
>
> Is there a way to check if the sheet(nome_al) exist before activating it?
> (without on error techniques please).
Since you asked for a solution that doesn't use On Error techniques, here is
a function that should do that for you (the header is modeled after the
function from Chip Pearson that Cleber Inacio posted)...
Public Function SheetExists(SheetName As String, _
Optional ByVal WB As Workbook) As Boolean
Dim WS As Worksheet
If WB Is Nothing Then Set WB = ThisWorkbook
For Each WS In WB.Sheets
If StrComp(WS.Name, SheetName, vbTextCompare) = 0 Then
SheetExists = True
Exit For
End If
Next
End Function
However, I just want to point out that there is nothing wrong with using On
Error techniques to help in coding. Perhaps you are put off by the word
"error"; but you shouldn't be, there is nothing inherently "bad" about using
On Error techniques to filter out undesirable results... it's just another
way to get information for your code to work with.
Rick
|