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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

DominicB

Well-known Member
Joined
Oct 3, 2005
Messages
1,569
Good afternoon vortec

Something like this should work OK :

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

HTH

DominicB
 

kjharve

Board Regular
Joined
Jan 4, 2006
Messages
206
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)
 

Forum statistics

Threads
1,136,504
Messages
5,676,254
Members
419,616
Latest member
quickflip

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
Top