Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract the final word in a string in Excel (variable length) 2

Status
Not open for further replies.

Alboppo1

MIS
Dec 20, 2021
3
GB
Hi,
I'm trying to extract the final word in a string of text - This could be of a variable length but there are consistently special characters in front of the the word and after the word. I'm so close but can't quite get it right - Any help would be greatly appreciated.
Sample string:
[{"colour":"Silver/Beige","texture":"Brittle","material":"mastic"}] From this I would need to extract mastic
[{"colour":"Brown","texture":"Hard","material":"plasterboard"}] From this I would need to extract plasterboard

I have got this formula so far: =TRIM(MID(AD2,FIND("#",SUBSTITUTE(AD2,":","#",3))+2,255)) this gives me a result of mastic"}]
 
Is that what you have?

TT_TT_zl4olg.png


---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Just making a guess--you need to end up with...
[pre]
colour texture material

Silver/Beige Brittle mastic
Brown Hard Plasterboard
[/pre]

If so, use Find/Replace to get rid of
"[", "]"
"{", "}" and """

Then use Data>Text to columns > Delimited on COMMA, to parse color, texture, material.

Finally use Data>Text to columns > Delimited on COLON and DO NOT IMPORT COLUMN 1, for each column: color, texture, material.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein

You Matter...
unless you multiply yourself by the speed of light squared, then...
You Energy!
 
Your data looks suspiciously like a python dictionary.

VBA supports dictionaries, and collections, which are similar. It should not be difficult to parse your data in VBA into a dictionary or collection.

That would give you much more functionality than you ask for however.
 
Thanks very much everyone, much appreciated.
It is JSON and is provided to me on a weekly basis from another business.
As I have to do this each week, I was hoping for a formula to pull out the last word so I can drop the data in my template and generate the report I need without the need to find and replace (if possible - and only because I'm lazy!).
I literally just need the last word in the string and can ignore the rest of it.
 
If you are truly lazy, you should ask the ‘another business’ to give you just what you need in the format you want. This way you wouldn’t have to do anything. That is lazy to me. Or is it 'efficient'... [ponder]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Depending on a bunch of things that you don't control, and the "another business" may or may not control, the order of the elements may or may not be the same in the future.

Today you need the last word, but in the future it could become the middle word.

A fundamental concept of JSON is name: value, so that you can ask for and get what you want without worrying about the order.

I didn't know about this before, but might make use of it in the future now that I do:
 
Other option would be to use this class VbsJson Create class module and then write function for getting JSON value for specific attribute

Tried this
Code:
Function json_value(json_array_str, json_attribute)
    'input is json array string with one json element, e.g.:
    '[{"colour":"Silver/Beige","texture":"Brittle","material":"mastic"}]
    
    'extract json from json array with one json element
    json_str = Replace(json_array_str, "[", "")
    json_str = Replace(json_str, "]", "")
    
    Set vbj = New VbsJson
    Set json = vbj.Decode(json_str)
    json_value = json(json_attribute)
End Function

works good
2021-12-21_17h39_47_oolapi.png
 
>Excel365

64bit or 32bit? Script control only comes in a 32bit version
 
Thank you for all your help @mikrom your solution worked a treat!
 
> I currently prefer to stick with 32-bit Office
You're right, not the most modern is always the best. Unfortunately I have no choice, I got my version of windows+office from my employer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top