VBA Macro to find matching value in another workbook. can you help me please?

instanceoftime

Board Regular
Joined
Mar 23, 2011
Messages
103
workbook "invoice" column A has item numbers. workbook "price" column A has item numbers and column H price (should have price)


if H does contain the price then place that info in H "invoice" ... then continuing the fill-in process until last row
if the H doesn't contain a price, continue same search of same item# until the last row. (and left blank if no price was found)

"price" has multiple instances of the same item#. Just need to find the first one that has price in column H
there are no blanks in column A at all
"price has 50,000 rows

I've used the following search method in a different workbook but unsure how to adapt it.

Code:
[COLOR=#ff0000]'This is old code that searched for item# input by user on the form, retrieves B, C, & D and puts it on the form' (I'm not using a form or anything for this new project .. just need to place price(H) into invoice(H)
'This is old code that I could adapt?  An array that pulls three columns of numbers but I only need column (8)[/COLOR]


Private Sub cmdSearch_Click()


    Dim Response As Long
    Dim NotFound As Integer
    Dim arr As Variant
    Dim I As Long
    Dim str1 As String, str2 As String, str3 As String  




    NotFound = 0
    
    ActiveWorkbook.Sheets("Items").Activate
    
    Response = Val("0" & Replace(txtItemNumber.Text, "-", ""))  [COLOR=#ff0000]'with this code the user inputs item#[/COLOR]


    ItemNumber = Response
    
    If Response <> False Then


        With ActiveSheet
            arr = .Range("A2:D" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        End With


        For I = 1 To UBound(arr)
            If arr(I, 1) = Response Then
                str1 = IIf(str1 = "", arr(I, 6), str1 & "|" & arr(I, 6))
                str2 = IIf(str2 = "", arr(I, 7), str2 & "|" & arr(I, 7))
                str3 = IIf(str3 = "", arr(I, 8), str3 & "|" & arr(I, 8))
            End If
        Next


        If str1 = "" Then
            MsgBox "Item Number Not Found!", vbExclamation
            NotFound = 1
            txtItemNumber.Text = ""
            txtItemNumber.SetFocus
        Else           
            lbxDescription.List = Split(str1, "|")   [COLOR=#ff0000]'fills in user form[/COLOR]
            lbxCost.List = Split(str2, "|")
            ListBox3.List = Split(str3, "|")
            lbxCost.ListIndex = 0
        End If


    End If
    
End Sub
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
while going over this old code ...i've come to the conclusion it isn't the best way to go as I need to step through column A in "invoice" - find it in "price" and place the price in the same row but column H of "invoice". So I'm looking into vlookup and will paste my attempt at code that way soon.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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