string manipulation

keith05281967

Board Regular
Joined
May 6, 2011
Messages
68
Greetings,

I have cells with data like this: "Houston TX" or "Phoenix AZ" but occasionally it comes in like "AustinTX" with no spacing.

In order to pass the database test these cells should have at least 3 spaces between city and the 2 digit state. I think i can make this work if I can start from the right of the cell, then move 2 spaces just to the left of the 2 digit XX state abbv. then going right to left count the number of spaces. If < 3 spaces then add 3 spaces.

Can this be done?

Excel '07

thanks,
Keith
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try having your list in column A, and use the following formula:

Code:
=LEFT(SUBSTITUTE(A1," ",""),LEN(SUBSTITUTE(A1," ",""))-2)&[COLOR=red][B]"   "[/B][/COLOR]&RIGHT(A1,2)

Note that the red-highlighted area needs to be 3 spaces.
 
Upvote 0
Try

=LEFT(A1,LEN(A1)-2)&REPT(" ",3-(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))&RIGHT(A1,2)
 
Upvote 0
Try this regular formula:
Code:
=TRIM(LEFT(A1,LEN(A1)-2))&"   "&RIGHT(A1,2)

Is that something you can work with?
 
Upvote 0
My apologies for not being clear. Can you state this same thing in vba code?

what is the equivalent of this:
=LEFT(A1,LEN(A1)-2)&REPT(" ",3-(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))))&RIGHT(A1,2)

in vba code?

thanks again,
Keith
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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