Find a text within a cell and return a text in the adjacent column using VBA

Shawn23

New Member
Joined
Mar 6, 2013
Messages
23
Hi,

How do I search column B (Products), for a certain word and return it to column C (Item) using VBA?
In VBA, I would like it to search for certain words such as "Small", "Medium", and "Large". If none of these words are found, I would like it to return "Other".

So if the first product is SMALL CARS, it will return SMALL under ITEM. CLOTHES will return OTHER.

I know I could just use the LOOKUP function, but I would like to build this under VBA.

Thank you!

CompanyPRODUCTSITEMPRICE
ABCSMALL CARS100
ABCMEDIUM CARS200
ABCLARGE CARS300
ABCCLOTHES400
ABCELECTRONICS500

<tbody>
</tbody>
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
colaps,

Here is a macro solution for you to consider, that will run in worksheet S1.

Please try the following macro on a copy of your raw data workbook.

Code:
Sub FindFamily()
'hiker95 , 6/20/2019
'http://www.mrexcel.com/forum/excel-questions/696585-find-text-within-cell-return-text-adjacent-column-using-visual-basic-applications.html
Dim c As Range, f, i As Long
f = Array("flower", "car", "tree")
With Sheets("S1")
  For Each c In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    For i = LBound(f) To UBound(f)
      If InStr(c, f(i)) > 0 Then c.Offset(, 1) = f(i)
      If InStr(UCase(c), UCase(f(i))) > 0 Then c.Offset(, 1) = f(i)
    Next i
  Next c
End With
End Sub
 
Upvote 0
Hello,

This works but my request was to not use f = Array("flower", "car", "tree") inside the code ( i will need to modify this when new families will be introduced and ofc not everyone will be able to read the code and made the adjustments as needed. From here the idea to read a desired column that contains the arrays, a range.

i need to read "flower","car","tree" from Column E. I tried to modify the code to read the arrays from a range ( E2:E10 ) but without luck.

Thanks for your effort.
 
Upvote 0
colaps,

Here is a new macro solution for you to consider, that will run in worksheet S1.

Please try the following macro on a copy of your raw data workbook.

Code:
Sub FindFamily_V2()
'hiker95 , 6/21/2019
'http://www.mrexcel.com/forum/excel-questions/696585-find-text-within-cell-return-text-adjacent-column-using-visual-basic-applications.html
Dim c As Range, f, i As Long, lr As Long
With Sheets("S1")
  lr = .Cells(Rows.Count, 5).End(xlUp).Row
  f = .Range("E2:E" & lr)
  For Each c In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    For i = LBound(f) To UBound(f)
      If InStr(c, f(i, 1)) > 0 Then c.Offset(, 1) = f(i, 1)
      If InStr(UCase(c), UCase(f(i, 1))) > 0 Then c.Offset(, 1) = f(i, 1)
    Next i
  Next c
End With
End Sub
 
Upvote 0
colaps,

The latest macro will adjust to the number of entries in worksheet S1, in column E, E2 to the last used row in column E.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,804
Messages
6,121,652
Members
449,045
Latest member
Marcus05

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