# Left function with variable length

#### vortec

##### New Member
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:
a2: ababs, aamnm, aaa
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
That would be:

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

Good afternoon vortec

Something like this should work OK :

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

HTH

DominicB

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)``

Replies
8
Views
1K
Replies
1
Views
463
Replies
2
Views
714
Replies
5
Views
189
Replies
8
Views
496

### Forum statistics

1,221,052
Messages
6,157,628
Members
451,426
Latest member
VinnyDoesntKnowExcelCode

### 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.

### Which adblocker are you using?

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

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