+ Reply to Thread
Results 1 to 16 of 16

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

  1. #1
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    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.

    Code begins:
    ------------------------------------------------------------------------------------
    Function lookupdiv(dimd As String) As Variant
    Dim rngs As Range
    Dim m As Integer

    '--- 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. Here i am able to use only named range called
    '-----"Dim_2G3G". However, i have 21 name ranges like this. Also, i think the range reference
    '------will require indirect functionality to be used

    Set rngs = ActiveWorkbook.Names("Dim_2G3G").RefersToRange


    '--- 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
    --------------------------------------------------------------
    Code ends

    This is my first post in the forum and i am eagerly looking forward to the support. Thanks

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Where is the user input for the named range coming from?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    Hi Norie, i have defined the named range through Excel functionality of Formula's-> Name manager-> Define names.
    Data for name ranges is in a dedicated worksheet in the same workbook.
    In another worksheet, user in column G in multiple rows input strings which will define which name range should be used. Since this is a user defined function, i will need to redefine the title of the function as well and pass one more parameter in the title like Function lookupdiv(dimd As String, dimtech as string --- "refering to user input") As Variant.

    Also i am able to do a separate function for step 1, but unable to combine it with my main function.

    -------------------------------------------------------------------------
    code:
    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

    code ends...-------------------------------------------------------------------



    Does it helps?
    Attached Files Attached Files
    Last edited by pnegi; 08-21-2013 at 10:39 AM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

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

    You've kind of answered your own question but instead of passing a string or variant pass the actual range.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    Hi, my range is not one single range. But there are multiple range. Which range should be used is decided basis user input. Now i am unable to do the code for "rngs" variable range which will change everytime user will give some input. I need to define what would be the name "named range" of "rngs".
    Hope i am able to clarify.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

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

    Sorry I really don't follow and I'm afraid the workbook doesn't really clarify things.

    How exactly is the user going to specify/input the named range to look in?

  7. #7
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    Hi, apologies for not being very clear. The user is going to specify input in cell in sheet "calculation". The input type will fall in different categories. these categories are different ranges or name ranges. for e.g. user input could be computer, tv, refridgerator then the range name is "electronics". If user input is bed or sofa or chair then range name is "furniture". My name ranges are "Dim_2G3G", "Dim_LTE", "Dim_Infra" which are corresponding to electronics, furniture in the above eg. Now with some formula or function, or sub, i will need to determine which if the input is TV then the range name is "electronics" and if the input is chain then the range name is "furniture".

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

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

    Doesn't this code you posted work?
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    Hi, yes, this code works but when i run it as separate function. When i did a common code for both steps, the output of function is #value!

    here is the code i did:


    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

  10. #10
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    Hi, i am sure this is not very tough... anybody who can help me out on this.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Have you tried stepping through the code?

    Then you might find out what the problem is.

    Can you upload a sample workbook?


    PS When posting code please use code tags.
    Last edited by Norie; 08-25-2013 at 09:56 AM.

  12. #12
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    Attached the new file.
    I do not know when you say tag code.. Am sorry.

    i think the problem is in the line Set rngs = ActiveWorkbook.Names(dimrange).RefersToRange,
    here i think indirect function is to be added since dimrange is not referring directly to the cells intend to use.

    Thanks in advance.
    Attached Files Attached Files

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    To find out about code tags take a look at the forum rules.

    You could also try the link in my signature.

  14. #14
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    Ok got it. here is the code with code tags

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    Hi Noori, any luck on the above code..?

  16. #16
    Registered User
    Join Date
    12-10-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    25

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

    Hello, can anyone help me with the code above...?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replacement of Indirect Function for Variable Size Range
    By Andrew Blundon in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 05-01-2013, 08:55 AM
  2. Place a variable in the Range() for dynamic Range
    By qed59 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-15-2010, 11:26 PM
  3. Dynamic range setting
    By KeithO in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-22-2009, 02:15 PM
  4. Indirect and Dynamic Range
    By Graham Haughs in forum Excel Formulas & Functions
    Replies: 16
    Last Post: 08-03-2006, 03:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1