# Calculation with comma separated values in a cell

#### lind33

##### New Member
I have an example ordered comma separated list in a cell. Values are comma separated. they can be 1 digit or 2 digits.

Book2.xlsx

I'm trying to get the difference between the first value and the last value, and add 1 to the result. This is what I tried to do:

B2: = (RIGHT(A2,1)-LEFT(A2,1))+1

This works fine if all the values are single digit, but if the last value is two digits, then it doesn't give the correct result.

Is there a better way of writing this formula which works fine both for single and double digits? please. thanks

On the link to excel file above, there are some data and expected results, and how the expected results obtained.

### Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

##### Well-known Member
Hi
Try
Excel Formula:
``B2==(MID(A2,FIND(",",A2)+1,255)-LEFT(A2,FIND(",",A2)-1))+1``

#### lind33

##### New Member
I tried it but I'm getting #VALUE! error.

##### Well-known Member
All right
I see
Use this UDF
Rich (BB code):
``````Function calc(targetcell As Variant)
If targetcell <> "" Then
x = Split(targetcell, ",")
calc = (x(UBound(x)) - x(0)) + 1
Else: calc = 0: End If
End Function``````

C1=calc(a1)

#### lind33

##### New Member

Thanks, Mohadin. the UDF works for all cells.

##### Active Member
another option

Excel Formula:
``=(IF(LEFT(RIGHT(A2,SEARCH(",",A2)),1)=",",RIGHT(A2,SEARCH(",",A2)-1),RIGHT(A2,SEARCH(",",A2)))-LEFT(A2,SEARCH(",",A2)-1))+1``

##### Well-known Member

You are very welcome
And thank you for the feedback
Be happy

#### lind33

##### New Member
another option

Excel Formula:
``=(IF(LEFT(RIGHT(A2,SEARCH(",",A2)),1)=",",RIGHT(A2,SEARCH(",",A2)-1),RIGHT(A2,SEARCH(",",A2)))-LEFT(A2,SEARCH(",",A2)-1))+1``
Thanks, Fadee2. yes this formula works too.

##### Active Member
Your welcome and thanks for the followup.

##### Well-known Member
@lind33
Mind you The other formula should be some thing like
Excel Formula:
``=IFERROR((IF(LEFT(RIGHT(A1,SEARCH(",",A1)),1)=",",RIGHT(A1,SEARCH(",",A1)-1),RIGHT(A1,SEARCH(",",A1)))-LEFT(A1,SEARCH(",",A1)-1))+1,0)``

Replies
5
Views
170
Replies
20
Views
425
Replies
3
Views
78
Replies
13
Views
472
Replies
1
Views
67

1,127,120
Messages
5,622,851
Members
415,934
Latest member

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