0

I found a similar question in a different post called "In Excel I need to find data matches between two arrays - one horizontal and one vertical" and I found part of my answer there. Unfortunately, I did not see how I could add a sub-question to this post. So here is the case.

I have an Excel workbook with two sheets:

1) In Sheet "Vendors" in column A, I listed all my vendor names (around 40).

2) In Sheet "Data" I have different data filled from column B until column Z. In this "Data" sheet in column F and/or column L the vendor name is listed for each row BUT the vendor name is embedded in more text. This text is never the same. It could be the vendor name with a space followed by numbers or the vendor name followed by a dash without space in between and then followed by numbers (e.g. "Vendor name-233" or "Vendor name / gfjd").

What I need the VBA to do is to use the vendor names in cloumn A of the "Vendor" sheet and to match it with the vendor names that are contained in the text strings of columns F and/or L of the "Data" sheet. The VBA should then return the found vendor name for each row in column A of the "Data" sheet.

This is the VBA that I tried from the previous post. The big problem is that it would only return the vendor name if the exact value is found in the "Data" sheet. The VBA cannot work with the vendor name embedded in the text string.

Sub newtest() Dim data, reference As Range Dim skipsome As Boolean skipsome = False

Set reference = Worksheets("Vendors").Range("A1", "A40")

Set data = Worksheets("Data").Range("B2", "F6")
For Each dataCell In data
    For Each referenceCell In reference
        If dataCell.Value = referenceCell.Value Then
                Worksheets("Data").Cells(dataCell.Row, 1).Value = dataCell.Value
                skipsome = True
                Exit For
        End If

        If skipsome = True Then
            skipsome = False
            Exit For
        End If
    Next
Next

End Sub

I would really appreciate if you could help me.

Thanks, Pia

2
  • Can vendor names be contained in others? For example, [Apple] and [Apple. Inc] both listed? OR even, if a Vendor's name is really short, such as [ab] then everything containing "ab" such as "Crab shop" and "Cab station" will not work well.
    – David G
    Commented Jul 9, 2015 at 15:36
  • If I look at all columns the vendor name can occur as Apple or Apple Inc. But the vendor name should always be starting from the first position of a cell. However, the vendor name in the "vendors" sheet in column A can consist of several words, such as "Big company Ltd.". In the "Data" sheet the entire name "Big company Ltd." is displayed but followed by more text, such as "Big company Ltd. London, transaction1"
    – Pia
    Commented Jul 9, 2015 at 16:07

1 Answer 1

0

You need to use Instr to see if the vendor name is CONTAINED in the value. Replace

If dataCell.Value = referenceCell.Value Then

with

If Instr(dataCell.Value, referenceCell.Value) <> 0 Then

This only works as is if there are no vendor names than englobe others.

4
  • Hi David, thanks for your response. Unfortunately your suggestion does not work. It returns values but those values are not the vendor names. What I noticed in my data is that within the text string the vendor name is usually displayed at the beginning (from the left side). Maybe this makes a solution easier.
    – Pia
    Commented Jul 9, 2015 at 16:04
  • Well... what values does it return? Maybe I didn't understand what each cell contains.
    – David G
    Commented Jul 9, 2015 at 16:39
  • If anything the problem is with the [[[[[[ Worksheets("Data").Cells(dataCell.Row, 1).Value = dataCell.Value ]]]]]statement but I couldn't know without looking at your sheet. If it returns values from your reference sheet but not the right ones, then you need to change the statement I just mentioned. Maybe changing the =dataCell.Value to =referenceCell.value
    – David G
    Commented Jul 9, 2015 at 16:41
  • I would have liked to send a picture of my output but unfortunately I can't post pictures here. What I receive as output is with your suggested change is some of the vendor names and in some rows I get the value that is the last column of the data sheet, which in my case is a number
    – Pia
    Commented Jul 9, 2015 at 21:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.