Using Named Fields

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
I have a function similar to this...

Code:
Function Test_Num (x as String)
    Text_Num = [YTD_CY_Num]
End Function

In this instance, the x will either be "C" or "P". Is it possible to utilize the x String to determine the "CY" or "PY" of the named field?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What exactly are you trying to do with this function?
Return the value of named range?
For what purpose (where is it going to be used)?
 
Upvote 0
What exactly are you trying to do with this function?
Return the value of named range?
For what purpose (where is it going to be used)?

This isn't the exact function. Depending on the input from the user, I am trying to create or alter which named field might be used.

For example, the choices could possibilities could be...

YTD_CY_Num or YTD_PY_Num

...and since the user would be providing the P or C....
 
Upvote 0
OK, but when you say "named field", do you really mean "named range"?
Or if this some field variable stored somewhere in another procedure?
 
Upvote 0
OK, but when you say "named field", do you really mean "named range"?
Or if this some field variable stored somewhere in another procedure?

Yes, they are specifically named cells on the various worksheets.
 
Upvote 0
Try this:
Code:
Function Test_Num(x As String)
 
    Select Case x
        Case "C"
            Test_Num = Range("YTD_CY_Num").Value
        Case "P"
            Test_Num = Range("YTD_PY_Num").Value
        Case Else
            Test_Num = 0
    End Select
    
End Function
 
Upvote 0
That was the option I have implemented, although, I used, for example...

Test_Num = [YTD_PY_Num]



However, my curiosity is whether or not the "P" or "C" from the variable could be directly used in the above example as a 1 line calculation...similar to this

Rich (BB code):
Function Test_Num(x As String)
    Test_Num = Range("YTD_" & x & "Y_Num").Value
End Function
 
Upvote 0
However, my curiosity is whether or not the "P" or "C" from the variable could be directly used in the above example as a 1 line calculation...similar to this
There's one way to find out, try it out! A lot of programming is trial and error. So don't be afraid to try things out.
Does it work?
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top