Listing Using a Function to Check for the Existence of a Code Name

' Determines if a given worksheet name exists in a workbook ' Checks by looking for the code name rather than the name

Function WorksheetCodeNameExists(wb As Workbook, sCodeName As String) As Boolean Dim s As String Dim ws As Worksheet

WorksheetCodeNameExists = False For Each ws In wb.Worksheets

If StrComp(ws.CodeName, sCodeName, vbTextCompare) = 0 Then WorksheetCodeNameExists = True Exit For End If


Set ws = Nothing End Function

You may be wondering why Listing 7.3 looks so much different than Listing 7.2 considering they both check for the existence of a given name. The reason is that you can't directly check the validity of a code name as you can for a worksheet name.

It's possible to rewrite Listing 7.2 so that it is exactly like Listing 7.3, except that instead of comparing a given name to ws.CodeName, you'd use ws.Name. This isn't quite as fast as Listing 7.2 nor is the performance difference that big of a deal for this function.

0 0

Post a comment