Searching within a single cell and summing up relevant values

iambong

New Member
Joined
May 29, 2015
Messages
3
Hi,


i have a single cell that has data in the following format


"name age sex height weight date name(1) age(1) sex(1) height(1) weight(1) date(1) name(2) age(2) sex(2) height(2) weight(2) date(2) name(3) age(3) sex(3) height(3) weight(3) date(3)"


example: "tom 41 male 190 80 5/12/2015 bill 29 male 174 70 5/14/2015 sarah 19 female 140 45 5/14/2015 cheryl 29 female 170 80 5/14/2015"


1. the terms are all separated by a single space
2. i cant use text to column


How may i use excel formula or vba to sum the weight of all the applicants with a particular date?


i.e. i want to sum the weight of bill, sarah and cheryl to obtain 195 as their 3 data series are dated 5/14/2015




Thank you for the help in advance!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi iambong,

you're making it really hard on yourself by stating point 2: no text to columns... That would by far be the easiest approach... So you asked for a complicated answer, this could help you:

From this answer I could find the formula to find the nth space in a text:
http://www.mrexcel.com/forum/excel-questions/72148-formula-find-nth-space-text-string.html
If you put your example in cell A2, the formula would be =SEARCH(CHAR(127);SUBSTITUTE($A2;" ";CHAR(127);5))
That 5th space is the start of your first date (criteria). The answer should be 19, if you then use =TRIM(MID($A2;19;11)), you get the text of the date "5/12/2015". I'm using 11 characters and a TRIM because dates can be 10 characters long (in november & december).
The weight of that first person is between the 4th and 5th space, so =SEARCH(CHAR(127);SUBSTITUTE($A2;" ";CHAR(127);4)) and =SEARCH(CHAR(127);SUBSTITUTE($A2;" ";CHAR(127);5)). That would give 16 and 19. Those two numbers you could use to grab the weight of that person: =MID($A2;16;19-16)*1. Then all there remains is an IF function and a cell with your criterium-date.

Hope these pointers get you started. But as said: it's way easier if you simply do text to columns.

Cheers,

Koen
 
Upvote 0
Hi Rijnsent,


thank you for the help. I am still exploring your solution. But unfortunately i have to state my problem more precisely.


I have to change the way i framed the problem because i realised the data has other problems (sorry, have to desensitise data)



I am looking to




=> sum all the "variable length" values which comes before a "desired" date. and unfortunately the data is all within a single cell (can't delimit it due to other reasons).
=> in this case (in the logic of data), i am referring to the change in wealth

===============

Data and desired output
===============

DataInput - Desired DateOutput
Tristan Watson Canada 60000 0 -20 5/03/2015 Geraldine Italy 4500 0.5 1500 5/03/2015 Melissa Italy 55000 10 50000 5/03/2015 Tristan Watson Canada 60000 0 -20 5/03/2015 Desmond Hutu Italy 30 0.5 10 5/01/2015 Vincent Dexter Alex Chris Italy 7200 0.013 90 5/03/2015 Geraldine Italy 4500 0.5 1500 5/03/2015 Bill Christian Canada 12830 -0.004 -56 5/02/2015 Jonathan Loda Lucas Canada 75 175 65 5/02/2015 Tristan Watson Canada 34 177 70 5/03/20155/3/2015-20
Tristan Watson Canada 60000 0 -20 5/03/2015 Geraldine Italy 4500 0.5 1500 5/03/2015 Melissa Italy 55000 10 50000 5/03/20155/3/201551480
Tristan Watson Canada 60000 0 -20 5/03/2015 Desmond Hutu Italy 30 0.5 10 5/01/2015 Vincent Dexter Alex Chris Italy 7200 0.013 90 5/03/2015 Geraldine Italy 4500 0.5 1500 5/03/2015 Bill Christian Canada 12830 -0.004 -56 5/02/20155/3/20151570
Sheralyn Lim Canada 3450 0.012 40 5/02/2015 Justin Italy 30 0.5 10 5/01/20155/3/20150
Tristan Watson Canada 60000 0 -20 5/03/20155/3/2015-20
Bill Christian Canada 12830 -0.004 -56 5/02/2015 Geraldine Italy 4500 0.5 1500 5/03/20155/3/20151500
Justin Italy 30 0.5 10 5/01/2015 Melissa Italy 55000 10 50000 5/03/20155/3/201550000

<tbody>
</tbody>





===============
Logic of the data
===============

NamePlaceExisting wealth% of wealthChange in wealthDate
TomUSA1000.111102-May
Bill ChristianCanada12830-0.004-562-May
Sheralyn LimCanada34500.012402-May
Cheryl WeathermanCanada34-0.726-902-May
Sylvia bin mohammad ashishCanada3-1.064502-May
Nikki FlowerCanada40000.018702-May
Jonathan Loda LucasCanada120000.220002-May
Michael TanCanada3330.51113-May
Tristan WatsonCanada600000.000-203-May
GeraldineItaly45000.515003-May
MelissaItaly5500010.000500003-May
Vincent Dexter Alex ChrisItaly72000.013903-May
Desmond HutuItaly300.5101-May
JustinItaly300.5101-May

<tbody>
</tbody>



Note:

Note that the Length of the name is variable
Note that the existing wealth and change in wealth is of variable length
Note that the change in wealth could be positive or negative sign
Note that the % of wealth could be up to 3 decimal places (it will try to have as few as possible)
The data field comprises of different concatenated combinations of concatenations of the rows of data




Thank you in advance
 
Upvote 0
Hi iambong,

that is indeed a nasty problem. I assume, having seen this bit of data, that the values you want to add are located just before the date. So the logic is: if the date can be found, take the value before it. That value has a space before it and a space after it, the space after is being the separator between it and the date. Maybe I'm describing it in a complicated way, but for programming one has to be exact :).
Given those parameters, I created the follwing function:

Code:
Function GetTotalsForDate(InputString As String, DateString As String) As Double

GetTotalsForDate = 0

If Len(InputString) > 0 And Len(DateString) > 0 Then
    DtFound = InStr(InputString, DateString)
    If DtFound > 0 Then
        GetTotalsForDate = 0
        Do While DtFound > 0
            EndPoint = DtFound - 1
            StartPoint = InStrRev(InputString, " ", EndPoint - 1)
            ValueFound = Mid(InputString, StartPoint, EndPoint - StartPoint) * 1
            GetTotalsForDate = GetTotalsForDate + ValueFound
            DtFound = InStr(EndPoint + 2, InputString, DateString)
        Loop
    End If
End If

End Function

This should extract the right values based on your data. Note that the date (5/03/2015) should be entered as text (a string). Add this function to a module in your workbook and then use the formula =GetTotalsForDate(A2;B2).

I hope that solves your issue!

Cheers,

Koen
 
Upvote 0
This formula works if the date to process is entered into col B:

Enter into C1 with Ctrl-Shift-Enter and copy down:

=SUM(IFERROR(1*TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",99)),FIND("#",SUBSTITUTE(SUBSTITUTE(A1," ",REPT(" ",99)),B1,"#",ROW($1:$30)))-199,198)),0))
Excel Workbook
ABC
1Tristan Watson Canada 60000 0 -20 5/03/2015 Geraldine Italy 4500 0.5 1500 5/03/2015 Melissa Italy 55000 10 50000 5/03/20155/03/201551480
2Sheralyn Lim Canada 3450 0.012 40 5/02/2015 Justin Italy 30 0.5 10 5/01/20155/03/20150
3Tristan Watson Canada 60000 0 -20 5/03/20155/03/2015-20
4Justin Italy 30 0.5 10 5/01/2015 Melissa Italy 55000 10 50000 5/03/20155/03/201550000
5Tristan Watson Canada 60000 0 -20 5/03/2015 Geraldine Italy 4500 0.5 1500 5/03/2015 Melissa Italy 55000 10 50000 5/03/2015 Tristan Watson Canada 60000 0 -20 5/03/2015 Desmond Hutu Italy 30 0.5 10 5/01/2015 Vincent Dexter Alex Chris Italy 7200 0.013 90 5/05/03/201553120
6Bill Christian Canada 12830 -0.004 -56 5/02/2015 Geraldine Italy 4500 0.5 1500 5/03/20155/03/20151500
7Justin Italy 30 0.5 10 5/01/2015 Melissa Italy 55000 10 50000 5/03/20155/03/201550000
Sheet
 
Upvote 0
my apologies for the cross-posting, am very new to the forums. thank you rijnsent and Istvan hirsch! both of your solutions work. thank you so much
 
Upvote 0

Forum statistics

Threads
1,215,373
Messages
6,124,562
Members
449,171
Latest member
jominadeo

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