VBA to search a column by defined string case and write to next cell

bettlejus

New Member
Joined
Apr 4, 2017
Messages
32
Hello,

I have the following table, column A contains string that repeats in some form, like front, front-l30, the word "front" is found in all, and I want a code that loops each row and it it finds the whole specified word in a case loop than it write to column B a specific value.
To make it clear, for the bellow example:

If search string="front" then write "10"
else if search string="front-l30 then write "20"
else if search string='front-r30 then write "30"

I tried to combine some code found here but without success.

Any help is appreciated.

Thank you!

Front10
Front-L3020
Front-R3030
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Why do you need code instead of a simple formula, i.e.
Excel Formula:
=IF(A1="Front",10,IF(A1="Front-L30",20,IF(A1="Front-R30",30,"")))

If you really want to use VBA, I would recommend using a CASE statement.
See: MS Excel: How to use the CASE Statement (VBA)
 
Upvote 0
there are more text strings in cells, I cannot use search, find funtions as they don’t use whole word search.

I want to search only those string inside a bigger string like “side front-l30”, it’s not only “front-30” there
 
Upvote 0
So you are saying that your data is NOT like what you posted in your original thread?
Can you post a more realistic data example?

Also, exactly how many values are there to look for?
Where is the list of values that should be returned for each one?
Is this in a table somewhere, or do you plan to code for each one explicitly in VBA?
 
Upvote 0
Sorry, I now realized reading post that it's incomplete, was
xxxfrontxxx10
yyyfront-l30yyy20
zzzfront-r30zzz30
aaaback-l30aaaa40
in a hurry, let me show you exactly.

there will be a fixed and hardcoded in the case funtion mapping legend like this:

front=10
front-l30=20
front-r30=30
back-l30=40

About 10 mapping values, mapping string are fixed, like alway "front-l30" not "front l30" or "l30 front" etc.

Thank you!
 
Upvote 0
Using UDF (user defined function)
VBA Code:
Option Explicit
Function GetValue(ByVal colA As Range)
Dim val, str, i&, item As String
str = Array("front", "front-I30", "front-r30", "back-I30") ' add more as per actual situation
val = Array(10, 20, 30, 40) ' values (val) in same order with string (str)
    For i = 0 To UBound(str)
        If InStr(1, colA, str(i)) Then GetValue = val(i)
    Next
End Function

With A1 contains string, in B1:
=GetValue(A1)

Capture.JPG
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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