LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Extracting Data out of a string

If the OP is not comfortable with using the Split function, then there is
always this VB code approach (there can be one or more blank spaces after
the state abbreviation and it will still work)...

Sub foo()
Const sAdr As String = "Las Vegas, NV 89103"
Dim aAdr(1 To 3) As String
Dim i As Long

aAdr(1) = Left(sAdr, InStr(sAdr, ",") - 1)
aAdr(2) = Trim(Mid(sAdr, InStr(sAdr, ",") + 1, 4))
aAdr(3) = Mid(sAdr, InStrRev(sAdr, " ") + 1)

For i = 1 To 3
Debug.Print aAdr(i)
Next
End Sub

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
Actually, he shows two blank spaces after the state abbreviation; that is,
I see this...

<city<comma<space<state abbrev<space<space<zip code

If that is correct, the code gets much easier.

Sub foo()
Const sAdr As String = "Las Vegas, NV 89103"
Dim aAdr() As String
Dim i As Long

aAdr = Split(Replace(sAdr, " ", ", "), ", ")

For i = 0 To UBound(aAdr)
Debug.Print aAdr(i)
Next
End Sub

But even if you are right and there are not two blank spaces after the
state abbreviation, we can still use the above structure with a slightly
longer Split function call...

Sub foo()
Const sAdr As String = "Las Vegas, NV 89103"
Dim aAdr() As String
Dim i As Long

aAdr = Split(Replace(sAdr, Right(sAdr, 6), "," & Right(sAdr, 6)), ", ")

For i = 0 To UBound(aAdr)
Debug.Print aAdr(i)
Next
End Sub

Rick


"Ron Rosenfeld" wrote in message
...
On Thu, 31 Jul 2008 19:11:00 -0700, ranswrt

wrote:

I tried your code but I'm not familiar with how to use 'split'. What I
am
trying to do is get the following array:

address(1) = city
address(2) = state
address(3) = zip


Here's one way that assumes your address strings are in the manner you
presented it in your first post:

<city<comma<space<state abbrev<space<zip code

==================
Option Explicit
Sub foo()
Const sAdr As String = "Las Vegas, NV 89103"
Dim aAdr(1 To 3) As String
Dim aTemp1, aTemp2
Dim i As Long

aTemp1 = Split(Trim(sAdr), ",")
aTemp2 = Split(Trim(aTemp1(1)), " ")

aAdr(1) = aTemp1(0)
aAdr(2) = aTemp2(0)
aAdr(3) = aTemp2(1)

For i = 1 To 3
Debug.Print aAdr(i)
Next i
End Sub
===================
--ron



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting Data from a string of text in a cell swalker Excel Worksheet Functions 4 August 7th 08 10:52 PM
extracting data from a string Iguss Excel Worksheet Functions 4 November 19th 07 09:50 PM
Formula for extracting data from a string LFM Excel Worksheet Functions 10 April 12th 07 03:59 AM
Extracting Numeric Data from a Delimited Text String [email protected] Excel Worksheet Functions 5 February 10th 06 10:29 PM
extracting data from a text string of varying length andy from maine Excel Discussion (Misc queries) 4 March 28th 05 07:11 PM


All times are GMT +1. The time now is 09:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"