Results 1 to 8 of 8

Thread: Excel Lookup Problem!!!!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679

    Question Excel Lookup Problem!!!!

    I am trusting on the best of minds in VBA to assist on this problem!!!


    OK! I have account numbers in column "A" that I need to compare with similar account numbers in column "B".

    Once I locate a match I need to program the spreadsheet to grab the ID number in column "C" that are associated with the account numbers in col. "B"!

    HAVE I CONFUSED YOU YET!!

    Now I attempted, with no success, to loop through the range of cells in col. "A", and look for a match in col. "B". If rngACell.value = rngBCell.value then perform a VLookUp for the ID # in col. "C".

    I am confident that someone has come across this problem before, or it may not even be a problem....I must be getting close to the solution...

    Thank you in advance..

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    This is the code I have so far:

    Code:
    Dim shtSurvey As Worksheet, rngCell As Range, rngCurrent As Range
    Dim rngID As Range, rngNew As Range
    Set shtSurvey = Application.Workbooks("srvy1.xls").Worksheets("srvy1")
    
    Set rngCurrent = shtSurvey.Range("n2:n32")
    Set rngID = shtSurvey.Range("r2:r50")
    Set rngNew = shtSurvey.Range("p2:p32") ' p2 - p32
    
    For Each rngCell In rngCurrent
        rngNew.Value = Application.WorksheetFunction.VLookup(rngCell, Range("ID"), 2, False)
        
        
    Next rngCell
    What is happening, is that it is filling the Range("p232")' p2 - p32 with just one matching account number....it looks as if it is running through all the matchs on the VLookUP function, but it is not filling the individual cells on the range...?????



    Any suggestions?

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    NOW I'm a little confused !!!?!?!?
    Okay to make this simpler, we'll work off this example, I've created a spreadsheet with the following:
    Code:
    1	6	a
    2	2	b
    3	7	c
    4	4	d
    5	8	e
    I'm taking it that the you want to see if each cell in the first range is within the second? i.e. So you'll check whether the value for cell A1 is within range B1 to B5?

    Or are we on about adjacent cells here, i.e. rather than the whole range of column B, are you just evaluating the value of cell A1 against B1, the value of cell A2 against B2 etc?
    Last edited by alex_read; Mar 18th, 2003 at 03:17 PM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    You got it, Alex.....sorry about confusing you...

    Your example will work...I need to start at the first cell in the first column and check for a match within the cells of B1 to B5( from your example).

    Then if there is a match found like A1 & B3, then take the ID number "c" (from your example) and place it in a cell next to "A1"...

    What do you think...crazy hugh!

    Thanx

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Okay then, create a new spreadsheet & paste into the cells the 3 columns I've put above, then place a commandbutton on the sheet and add this code to it...
    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     Dim intCellCounter As Integer
    3.    
    4.     For intCellCounter = 1 To 5
    5.         Range("D" & intCellCounter).FormulaR1C1 = "=LOOKUP(RC[-3],RC[-2]:R[4]C[-2])"
    6.         If IsNumeric(Range("D" & intCellCounter).Value) Then
    7.             MsgBox "The value of cell A" & intCellCounter & " was found between " & _
    8.             "the range B1:B5, in row number " & intCellCounter & "." & vbCrLf & _
    9.             "The associated value within the 3rd column is " & Range("C" & intCellCounter)
    10.         End If
    11.     Next intCellCounter
    12. End Sub
    Let me know if this is what you're after!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Just saw the second part to your post there, this will place only the matched/correct values into column D, and won't show you any messageboxes...
    VB Code:
    1. Private Sub CommandButton1_Click()
    2.     Dim intCellCounter As Integer
    3.    
    4.     For intCellCounter = 1 To 5
    5.         Range("D" & intCellCounter).FormulaR1C1 = "=LOOKUP(RC[-3],RC[-2]:R[4]C[-2])"
    6.         If Not IsNumeric(Range("D" & intCellCounter).Value) Then
    7.             Range("D" & intCellCounter).Value = ""
    8.         End If
    9.     Next intCellCounter
    10. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    OK Alex I took your suggestion and placed the table starting at cell "D1" and entered the code you created into a Command Button...

    The result is the column "D" went blank! I wonder if has to do with the FormulaR1C1????

  8. #8
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Ah sorry, should've put that better okay, place the values above into the cell starting at cell A1 - the returned values get created in column D, whereas the data evaluated is held in the first 3 columns.

    Please see the attached file & this should make a bit more sense!
    Attached Files Attached Files

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

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



Click Here to Expand Forum to Full Width