Calculation with comma separated values in a cell

lind33

New Member
Joined
Jun 25, 2019
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi
Try
Excel Formula:
B2==(MID(A2,FIND(",",A2)+1,255)-LEFT(A2,FIND(",",A2)-1))+1
 
Upvote 0
I tried it but I'm getting #VALUE! error.

1605956564292.png
 
Upvote 0
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)
 
Upvote 0
Solution
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
 
Upvote 0
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.
 
Upvote 0
@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)
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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