VBA for setting range to variable carrying dynamic name range & with indirect function

pnegi

New Member
Joined
Sep 2, 2013
Messages
1
Hi, i am working on a user defined function.
The function has 2 tasks:
1st to identify the name range out of 21 name ranges basis user input (user input 1) and use it in set range = variable (this is where i need help)
I think here directly referring to the range name also will need indirect function support which i am again not able to do and need help.
2nd then to match the string (user input 2) in the name range defined above and return the adjacent columns (which i am able to do).

The function code i have written does pretty much the task 2, but with only one name range which stays static.


Function lookupdiv(dimd As String, dimtech As String) As Variant
Dim rngs As Range
Dim m As Integer
Dim dimrange As Range

'--step 1 to identify the name range

Select Case dimtech
Case "2G", "3G", "General", "Combined"
dimrange = "Dim_2G3G"
Case "LTE"
dimrange = "Dim_LTE"
Case "Fixed"
dimrange = "Dim_Fixed"
Case Else
dimrange = "0"
End Select

'--- Load the table (range) from the global name
'--- the problem is in the below code line where i do not know how to set rngs to
'-----multiple names basis some user input. the below variable does not work and
'------ the return value for function is #value!

Set rngs = ActiveWorkbook.Names(dimrange).RefersToRange - the problem i think is in this code row


'step 2 to look into the name range for string match
'--- Iterate through all members in the table
'--- (skip first header row),

For m = 2 To rngs.Rows.Count
If (dimd = rngs(m, 1)) Then
'--- A match has been found. Load data end exit the function
lookupdiv = rngs(m, 3)
Exit Function
End If
Next m

'--- if the exeution reach this point, there was no match
'--- Return "error" value
lookupdiv = 0
End Function
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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