+ Reply to Thread
Results 1 to 14 of 14

Array/Range lookup

  1. #1
    Registered User
    Join Date
    06-16-2014
    Location
    Colorado
    MS-Off Ver
    2007, 2013
    Posts
    28

    Unhappy Array/Range lookup

    Hey all, I am newish to VBA and am having a tough time with an easy problem (mostly because I can't communicate what I am looking for well).

    I am trying to match a larger column of numbers to a smaller column of numbers so I may interpolate between values.

    Ex:

    Column A: .. Column B
    5 ................ 5
    6 ................ 8
    7 ................ 12
    8
    9
    10

    So the output would interpolate 5,6,7 between 5-8, and 8,9,10 between 8-12

    Does this make sense? any help would be appreciated!

  2. #2
    Registered User
    Join Date
    06-16-2014
    Location
    Colorado
    MS-Off Ver
    2007, 2013
    Posts
    28

    Re: Array/Range lookup

    It is important that I loop, because eventually the "Column A" will be about 8000 rows and "Column B" will be about 100 rows.

  3. #3
    Registered User
    Join Date
    06-16-2014
    Location
    Colorado
    MS-Off Ver
    2007, 2013
    Posts
    28

    Re: Array/Range lookup

    Sub interpolate()

    Dim MD_array() As Double, TVD() As Double, last_row As Double, last_row3 As Double, TVD_2 As Double
    last_row = Range("C3").End(xlDown).Row
    last_row3 = Range("F3").End(xlDown).Row
    ReDim MD_array(last_row)
    ReDim TVD(last_row3)

    Dim i As Integer

    i = 0#

    For i = 0 To last_row
    MD_array(i) = Range("C" & i + 3)
    Next i

    For i = 0 To last_row3
    TVD(i) = Range("F" & i + 3)
    Next i

    Dim depth() As Double, last_row2 As Double, j As Integer
    last_row2 = Range("N3").End(xlDown).Row
    ReDim depth(last_row2)

    For i = 0 To last_row2
    depth(i) = Cells(3 + i, 14).Value

    For j = 1 To (last_row - 3)
    If depth(i) >= MD_array(j - 1) And depth(i) <= MD_array(j) Then
    TVD_2 = TVD(j - 1) + ((TVD(j) - TVD(j - 1)) * (depth(i) - MD_array(j - 1)) / (MD_array(j) - MD_array(j - 1)))
    Cells(3 + i, 16).Value = TVD_2
    Else
    j = j
    End If
    Next j
    Next i


    End Sub
    Last edited by betherzy; 01-06-2015 at 02:05 PM. Reason: tl;dr final i came up with

  4. #4
    Forum Contributor
    Join Date
    03-10-2013
    Location
    Budapest, Hungary
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Array/Range lookup

    Hi Betherzy!

    In connexion with functions I Know , what it means to interpolation.
    Now you mention it in relation to number string interpolation.
    This Is unknown to me. I do not know how the long number series will be a short.
    Explain otherwise, it will be perhaps understandable.


    Thanks

    Pan314

  5. #5
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Array/Range lookup

    Can you attach a Workbook showing some raw data and also your desired result..

  6. #6
    Registered User
    Join Date
    06-16-2014
    Location
    Colorado
    MS-Off Ver
    2007, 2013
    Posts
    28

    Re: Array/Range lookup

    Hey all thanks for the feedback, attached is a sample of the book and the code Ive wrote so far, i get a 'subscript out of range' error.

    essentially, I want to take the "Depth" column and place it within ranges in the "Measured depth" column so I may interpolate what the "TVD" of that specific depth would be. Any help would be much appreciated.

    Thanks
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    06-16-2014
    Location
    Colorado
    MS-Off Ver
    2007, 2013
    Posts
    28

    Re: Array/Range lookup

    Quote Originally Posted by Pan314 View Post
    what it means to interpolation.
    Pan314
    Pan, its just the opposite of extrapolation, so you want to take an approximation between two bounds. I don't think that really is the issue for this, I just need a way to stick a longer data set in between moving bounds of a smaller data set. does that make any sense?

  8. #8
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Array/Range lookup

    betherzy,

    I have been working with the codes you posted (the original from yesterday, and the updated one from today). With some changes to the code, I can get it to run without errors, but, I don't see where it is doing anything.
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  9. #9
    Registered User
    Join Date
    06-16-2014
    Location
    Colorado
    MS-Off Ver
    2007, 2013
    Posts
    28

    Re: Array/Range lookup

    Hey guys, figured it out, thanks a bunch, attached final if anyone is interested
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    06-16-2014
    Location
    Colorado
    MS-Off Ver
    2007, 2013
    Posts
    28

    Re: Array/Range lookup

    The only problem is it runs a few past the last row, so will error out unless told to skip, any ideas on how to fix this one?

  11. #11
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Array/Range lookup

    Nice job!!

  12. #12
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Array/Range lookup

    Hmm, it doesn't error when I run it...what is the error?

  13. #13
    Registered User
    Join Date
    06-16-2014
    Location
    Colorado
    MS-Off Ver
    2007, 2013
    Posts
    28

    Re: Array/Range lookup

    Thanks! it doesn't error, just has a couple rows of nonsense numbers afterward I guess

  14. #14
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Array/Range lookup

    Ahh, ok. I do see where it is going more rows than needed, but haven't figured out why......yet.

+ 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. Can I make the row lookup array/range part in an array formula variable?
    By OLLY-7 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-18-2014, 09:06 AM
  2. [SOLVED] range lookup and array formula
    By wellsw in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-26-2013, 12:57 AM
  3. Replies: 5
    Last Post: 12-24-2011, 12:16 PM
  4. [SOLVED] Lookup Value in Range/Array and Return Column Header Value
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2010, 08:17 AM
  5. Lookup using match in named range stored in array
    By erikhs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-08-2007, 06:06 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