If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > personal function

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-24-09, 16:34
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
personal function

I created a function that returns the worksheet name, but for some reason when i open the file the functions cells all show 0, if i recall they still show 0, if i go to the cell hit f2 and enter the formula works or if i change the cell that the formula refers to it works. Whats up?


Public Function GetSheetName(codeName As String)
Dim wks As Worksheet
codeName = WorksheetFunction.Proper(codeName)
For Each wks In ActiveWorkbook.Sheets
If wks.codeName = codeName Then
GetSheetName = wks.Name
Exit Function
End If
Next

End Function
Reply With Quote
  #2 (permalink)  
Old 11-25-09, 13:40
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 477
Hi,

0 implies that the sheet can't be found. This could be caused by all sorts of things.

Another problem is that if a codename is updated in the VBE, the function will not update because the codenames aren't in the function's argument list. I think the best you can hope for is making it volatile. I've also corrected a few other things which could cause a problem in a UDF:
Code:
Option Explicit

Public Function GetSheetName(ByVal cName As String) As Variant
    
    Const NOTFOUND As String = "Not Found"
    Dim wks As Worksheet
    
    Application.Volatile
    
    On Error GoTo Errorhandler
    
    GetSheetName = NOTFOUND
    
    For Each wks In ThisWorkbook.Worksheets
        If UCase$(wks.codeName) = UCase$(cName) Then
            GetSheetName = wks.Name
            Exit For
        End If
    Next
    
ErrorExit:
    
    Exit Function
    
Errorhandler:
    
    GetSheetName = CVErr(xlErrValue)
    Resume ErrorExit
    
End Function
Does that make any difference?

(By the way, I'm not sure why you would want to return this information to a worksheet?!)

Hope that helps...
Reply With Quote
  #3 (permalink)  
Old 11-27-09, 16:09
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
I am not sure what you mean :

"Another problem is that if a codename is updated in the VBE, the function will not update because the codenames aren't in the function's argument list."

but i think the Application.Volatile is what i need, never heard of that before!

the reason i need the worksheet names is we are not supposed to have anything hard coded, so this way someone can change worksheet names and not break the code. The code reads data on one sheet and then uses that for processing all the others If they are detialed on the 1st sheet
Reply With Quote
  #4 (permalink)  
Old 11-27-09, 19:52
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 477
Quote:
I am not sure what you mean :

"Another problem is that if a codename is updated in the VBE, the function will not update because the codenames aren't in the function's argument list."
Changing a codename in the VBE will not raise a calculate event. The formula's result will only update when the formula is recalculated.

Quote:
the reason i need the worksheet names is we are not supposed to have anything hard coded, so this way someone can change worksheet names and not break the code. The code reads data on one sheet and then uses that for processing all the others If they are detialed on the 1st sheet
If you refer to the sheet codenames (rather than the sheet names) in the main code, then you won't have this problem? That's what I do and it works a treat.

Quote:
but i think the Application.Volatile is what i need, never heard of that before!
Making the function volatile is a 'last resort'. It will recalculate every time, even when a recalculation isn't required (no inputs have changed). This is obviously inefficient but for some functions it is required by definition, for example the NOW() function which returns the current date and time.
__________________
Colin

My Excel articles

Other tutorials:
Array Formulas | Deleting Rows with VBA

Last edited by Colin Legg; 11-27-09 at 19:56.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On