Share via

Converting an object's name, passed as a string, back to an object

Anonymous
2012-01-12T14:45:36+00:00

I have a UDF with one required parameter (ByVal WrkshtCodeName): a worksheet's codename.  If a user inadvertently puts double quotes around the parameter, is there a way to convert the string (e.g., "Sheet1") back to an object (e.g., Sheet1) before my UDF proceeds with the rest of its statements?

Thanks in advance for any assistance.

EDIT:  I should have said "VBA function" rather than "UDF" . . . my apologies.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2012-01-16T05:44:38+00:00

Hi,

We can convert string of Worksheet name to object because it's hard to convert string of Worksheet code name.

Here's some codes for you, please copy them to a Module.

Option Explicit

Public ws As Worksheet

Sub UDF(WrkshtName)

    If VarType(WrkshtName) = vbString Then

        Set ws = Sheets(WrkshtName)

    ElseIf VarType(WrkshtName) = vbObject Then

        Set ws = WrkshtName

    End If

End Sub

Sub TestForConvertingStringToOjbect()

    Call UDF("Sheet1") 'Here the parameter 'Sheet1' is a Worksheet name.

    MsgBox ws.CodeName, vbInformation, "String to Object"

End Sub

Sub TestForObject()

    Call UDF(Sheet1) 'Here the parameter 'Sheet1' is a code name of Worksheet.

    MsgBox ws.CodeName, vbInformation, "Direct Use of Ojbect"

End Sub

The  above two macros('TestForConvertingStringToOjbect' and 'TestForObject') will show you the result.

Hope this help you.

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

Answer accepted by question author

Anonymous
2012-01-16T06:27:28+00:00

I have a UDF with one required parameter (ByVal WrkshtCodeName): a worksheet's codename.  If a user inadvertently puts double quotes around the parameter, is there a way to convert the string (e.g., "Sheet1") back to an object (e.g., Sheet1) before my UDF proceeds with the rest of its statements?

Thanks in advance for any assistance.

It's hard to imagine someone inadvertently adding double quotes since it would require them to use 3 double-quotest, but...

maybe, replace the leading and trailing double-quote, if any with nothing?  Something like

function myUDF(byval WSName as string)

    if left(WSName,1 )="""" then wsname=mid(wsname,2)

    if right(WSName,1)="""" then wsname=left(wsname,len(wsname)-1)

    ...

    end function

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-01-16T13:11:29+00:00

    Thanks for your solution.  In my original post, I should have said "VBA function" rather than "UDF".  My apologies.

    Unfortunately, your solution causes an error on the line:

         If Left(WSName,1 )="""" Then wsname=Mid(wsname,2) 

    if a user forgets to include the double quotes.  And in my original post, I specifically showed the parameter's syntax (ByVal WrkshtCodeName) because my VBA function normally expects the codename to be inputted as an object (since a worksheet's codename is, by definition, an object).  But just in case a user inadvertently enclosed the codename in double quotes, I wanted to know if there was a way to convert a string parameter back to an object parameter.  Hence, the reason for my original post.

    Thanks all the same.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-01-16T13:00:04+00:00

    Thanks for your solution.  In my original post, I should have said "VBA function" rather than "UDF".  My apologies.

    As you rightfully pointed out in your first sentence, it's much easier to convert a worksheet's name (a string) to an object.  But  I really need a way to convert a worksheet's codename, that has been (inadvertently) inputted as a string, to an object.

    Thanks again for your help.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2012-01-16T06:28:53+00:00

    Hi,

     

    We can convert string of Worksheet name to object because it's hard to convert string of Worksheet code name.

    Here's some codes for you, please copy them to a Module.

    Option Explicit

    Public ws As Worksheet

    Sub UDF(WrkshtName)

        If VarType(WrkshtName) = vbString Then

            Set ws = Sheets(WrkshtName)

        ElseIf VarType(WrkshtName) = vbObject Then

            Set ws = WrkshtName

        End If

    End Sub

    Sub TestForConvertingStringToOjbect()

        Call UDF("Sheet1") 'Here the parameter 'Sheet1' is a Worksheet name.

        MsgBox ws.CodeName, vbInformation, "String to Object"

    End Sub

    Sub TestForObject()

        Call UDF(Sheet1) 'Here the parameter 'Sheet1' is a code name of Worksheet.

        MsgBox ws.CodeName, vbInformation, "Direct Use of Ojbect"

    End Sub

    The  above two macros('TestForConvertingStringToOjbect' and 'TestForObject') will show you the result.

    Hope this help you.

    A UDF in Excel has a very specific meaning.  It is a "user defined function" that one can use in an Excel formula as though it were a native function.  It is *not* a sub named UDF. ;(

    Was this answer helpful?

    0 comments No comments