Left function with variable length

vortec

New Member
Joined
Mar 22, 2006
Messages
3
Hopefully this is an easy one for anyone that actually knows what they are doing.

I am trying to figure out how to extract a Text String from within a cell.

Example:
a1: aaaada, aaabb, aa
a2: ababs, aamnm, aaa
a3: aaabbnhadda, aammm, aacbb
a4: aaabb, annnn, aaaa

I just want to extract everything left of the first ",". The number of characters will vary from cell to cell.

I know it seems simple but I just cant seem to get it right. Any help would be appreciated.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Good afternoon vortec

Something like this should work OK :

=LEFT(A1,FIND(",",A1)-1)

HTH

DominicB
 
Upvote 0
If you are using VBA:

You can use the InStr function to return the position of a character within a string. E.g:

Code:
 InStr(1, "aaaada, aaabb, aa", ",")

Then use len to return the length of the whole string:

Code:
 Len("aaaada, aaabb, aa")

Subtracting the two will give you the number of characters you need to take:

Code:
reqChars = InStr(1, "aaaada, aaabb, aa", ",") - Len("aaaada, aaabb, aa")

Then use "left" to return those characters from the string:

Code:
 mystring = Left("aaaada, aaabb, aa", reqChars)

Obviously, you don't have to put the string directly into the code. You could put:

Code:
 Left(cells(1, 1).value, reqChars)
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,304
Members
448,564
Latest member
ED38

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