+ Reply to Thread
Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    83

    Using cell values to address an array

    I have a column in a worksheet where each value has an integer value between 1 and 6. I also have an array defined in size from 1 to 6.

    I want to be able to create a new colum in the worksheet where the value for each row is one of the values held in the array. In fact it's the same element number of the array as number held in the first column.

    So I want to use the first column to address the array and fill the new column. I keep getting syntax errors and could use some help.

    Thanks
    Last edited by jlt199; 03-20-2010 at 12:02 AM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Using cell values to address an array

    Show us what you mean.

    Click GO ADVANCED and use the paperclip icon to post up your workbook.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Using cell values to address an array

    Hang on, I'll make a simplified version

  4. #4
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Using cell values to address an array

    Ok, I think that's it. At least it's one of the ways I have tried, others have just been a variation of this.

    I did forget to mention earlier that I'm trying to do this using a macro, as it's a small part in a big file I need to generate automatically
    Attached Files Attached Files
    Last edited by jlt199; 03-19-2010 at 07:14 PM.

  5. #5
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Using cell values to address an array

    This is the programming forum, so VBA is assumed. I'll look at your file.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Using cell values to address an array

    Put this formula in B2 and copy down:

    =INDEX(Lengths!$C$4:$C$9, MATCH($A2, Lengths!$B$4:$B$9, 0))

    Here's two other formulas:

    =VLOOKUP($A2, Lengths!$B$4:$C$9, 2, 0)
    =LOOKUP($A2, Lengths!$B$4:$B$9,Lengths!$C$4:$C$9)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  7. #7
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Using cell values to address an array

    Thanks,

    Does thhis method result in the other spreadsheet being called for evey row? That is what I was trying to avoid by creating an array.

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Using cell values to address an array

    INDEX/MATCH is one of the most robust worksheet functions available. Also, it's not volatile, so performance should be excellent.

    I know this is the programming forum, but your need is clearly resolved via super-fast formula, so in this case I would use it.

    =========
    If there is some reason you cannot use the better option of the ready-to-go formula, then I would simply create a macro that typed in that formula for me, then removed the formulas leaving the value behind.

    Code:
    Option Explicit
    
    Sub GetLengths()
    Dim LR As Long
    Application.ScreenUpdating = False
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    With Range("B2:B" & LR)
        .FormulaR1C1 = "=INDEX(Lengths!R4C3:R9C3, MATCH(RC1, Lengths!R4C2:R9C2, 0))"
        .Value = .Value
    End With
    
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Using cell values to address an array

    Perfect!

    Many thanks

  10. #10
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Using cell values to address an array

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  11. #11
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Using cell values to address an array

    Suppose we go back to the original problem, and instead of the array being filled with cell values it was defined in VBA, so for example

    Code:
    dim pLength as double
    pLength = Array(100, 150, 200, 400, 560, 700)
    How would you solve the problem then?
    Thanks

  12. #12
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Using cell values to address an array

    Using your array would require looping, evaluating every row one by one to see what the value in A is and matching that to the value in your array. Looping is not the way to solve this efficiently, so the answer to your question:

    "....instead of the array being filled with cell values it was defined in VBA...how would you solve it then?"

    I would write the values into a table on the worksheet temporarily and use the instantaneously finished formula approach already shown, but adjusting the formula to use the range of cells I'd written in.

    Inherently, worksheet functions are simply faster, as you would expect them to be. I wouldn't loop through 100s, 1000s of rows of data doing one-at-a-time comparisons when it can be done all at once this way.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  13. #13
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Using cell values to address an array

    That's interesting... Intuiatively I would've thought arrays would have been faster.

    Thanks again for your help

Thread Information

Users Browsing this Thread

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

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.2.0