Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-23-2018, 02:14 PM
soldat452002 soldat452002 is offline Extract States from a String Windows 7 64bit Extract States from a String Office 2010 64bit
Novice
Extract States from a String
 
Join Date: Apr 2018
Location: Los Angeles
Posts: 6
soldat452002 is on a distinguished road
Default Extract States from a String

Hello



I have a database that generates the following string based on a web form selection.

I need to extract the state names only from these strings.

How can I achieve that in Excel? I tried everything


[{"licenseexpdate":"","licensenumber":"ysdksnjdkl", "state":"Pennsylvania"},{"licenseexpdate":"08/12/2020","licensenumber":"lic1235","state":"Florida"} ,{"licenseexpdate":"","licensenumber":"gsdgfsef"," state":"South carolina"}]
Reply With Quote
  #2  
Old 04-23-2018, 04:22 PM
NoSparks NoSparks is offline Extract States from a String Windows 7 64bit Extract States from a String Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Assuming the sample string is in A1
Code:
Sub GetStates()
    Dim str As String, i As Integer
    Dim ar As Variant, arr As Variant
    
    str = Range("A1").Text
    str = Replace(Replace(Replace(Replace(Replace(str, "}", ""), "[", ""), "]", ""), """", ""), ",", "")
    ar = Split(str, "{")
    For i = LBound(ar) To UBound(ar)
        If InStr(ar(i), "state") > 0 Then
            arr = Split(ar(i), ":")
            MsgBox arr(UBound(arr))
        End If
    Next i
End Sub
Reply With Quote
  #3  
Old 04-23-2018, 04:26 PM
soldat452002 soldat452002 is offline Extract States from a String Windows 7 64bit Extract States from a String Office 2010 64bit
Novice
Extract States from a String
 
Join Date: Apr 2018
Location: Los Angeles
Posts: 6
soldat452002 is on a distinguished road
Default

That is perfect. How can I append the values to a excel spreadsheet
Reply With Quote
  #4  
Old 04-23-2018, 04:53 PM
NoSparks NoSparks is offline Extract States from a String Windows 7 64bit Extract States from a String Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Sorry, I must have missed what was to be done with the results so just displayed them as they were being extracted in a message box in order to see the results.

You need to replace the MsgBox line with the cell address(s) you're wanting the result(s) to go in to.
For a more accurate/specific answer more information is required.
What's the sheet name? What column? All in one cell or ???
Reply With Quote
  #5  
Old 04-24-2018, 06:04 AM
soldat452002 soldat452002 is offline Extract States from a String Windows 7 64bit Extract States from a String Office 2010 64bit
Novice
Extract States from a String
 
Join Date: Apr 2018
Location: Los Angeles
Posts: 6
soldat452002 is on a distinguished road
Default

Quote:
Originally Posted by NoSparks View Post
Sorry, I must have missed what was to be done with the results so just displayed them as they were being extracted in a message box in order to see the results.

You need to replace the MsgBox line with the cell address(s) you're wanting the result(s) to go in to.
For a more accurate/specific answer more information is required.
What's the sheet name? What column? All in one cell or ???
I want to insert the results in a separate column called states next to the string.sheet name is extract.
Reply With Quote
  #6  
Old 04-24-2018, 08:16 AM
NoSparks NoSparks is offline Extract States from a String Windows 7 64bit Extract States from a String Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

Sub in attached is commented line by line, hopefully makes sense.
Comment out or remove the lines of the result you don't want.

Hope that helps.
Attached Files
File Type: xlsm soldat452002.xlsm (18.8 KB, 20 views)
Reply With Quote
  #7  
Old 04-24-2018, 09:11 AM
soldat452002 soldat452002 is offline Extract States from a String Windows 7 64bit Extract States from a String Office 2010 64bit
Novice
Extract States from a String
 
Join Date: Apr 2018
Location: Los Angeles
Posts: 6
soldat452002 is on a distinguished road
Default

Okay thanks.

Im assuming I need to change this to my spreadsheet location.

MsgBox arr(UBound(arr))
Reply With Quote
  #8  
Old 04-24-2018, 09:39 AM
NoSparks NoSparks is offline Extract States from a String Windows 7 64bit Extract States from a String Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 831
NoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really niceNoSparks is just really nice
Default

I'm not sure what (or why) you're asking.

That line isn't in the altered and commented procedure.
It was only in the first procedure to show what the result would be as they were not being written to anywhere.
Reply With Quote
  #9  
Old 04-25-2018, 09:50 AM
soldat452002 soldat452002 is offline Extract States from a String Windows 7 64bit Extract States from a String Office 2010 64bit
Novice
Extract States from a String
 
Join Date: Apr 2018
Location: Los Angeles
Posts: 6
soldat452002 is on a distinguished road
Default

I was trying to have the code put into a colum cell on the spreadsheet instead of a msgbox. Im just not familiar with VBA .
Reply With Quote
  #10  
Old 04-26-2018, 04:16 AM
ArviLaanemets ArviLaanemets is offline Extract States from a String Windows 8 Extract States from a String Office 2016
Expert
 
Join Date: May 2017
Posts: 873
ArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud ofArviLaanemets has much to be proud of
Default

In case you get the strings on separate rows for every entry, like
{"licenseexpdate":"","licensenumber":"ysdksnjdk l", "state":"Pennsylvania"}
{"licenseexpdate":"08/12/2020","licensenumber":"lic1235","state":"Florida"}
{"licenseexpdate":"","licensenumber":"gsdgfsef" ," state":"South carolina"}
then this formula will do:
Code:
=MID(A2,FIND("state",A2)+8,FIND("}",A2)-FIND("state",A2)-9)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract States from a String Extract a string from a paragraph kirkm Word VBA 7 09-11-2016 06:13 PM
How to extract only numbers from a STRING? Learner7 Excel 3 07-02-2013 06:25 AM
Extract States from a String Extract Numbers from Alphanumeric String OTPM Excel 6 05-13-2011 12:52 AM
Extract numbers from a text string aleale97 Excel 4 02-10-2011 10:33 AM
Extract from String using Wildcard whousedmy Word 0 05-21-2009 01:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:41 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft