Complex String Operations without using Text to Columns

excelsion

New Member
Joined
Jan 18, 2016
Messages
34
Hi,

I have a data source that contains the names of companies and is stored on one of the core applications used in my company.when I copy that information on Excel, it comes in the following format:

Amazon UK Ltd Google Inc British Gas Pallermo Ltd

Basically, there are a lot of spaces between two company names ( approx. 25 to 30 spaces). I cant use text to columns as it will separate all the texts that have a space between them. I want the final output to be :

Amazon UK Ltd
Google Inc
British Gas
Pallermo Ltd

I am not familiar with VB scripting so I wont be able to use that. IS there a solution to this that anyone can recommend?

Thanks
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Is there only single space (or less than between company names) between first part and second part of company name? If so you can use SUBSTITUTE like below.

Assuming string is copied into A1.
In B1
=SUBSTITUTE(A1," ",";")

Copy result and paste as value. Then use text to column using ";" as delimiter and check "Treat consecutive delimiters as one".
 
Upvote 0
Note: Between double quote, enter about 5 spaces or as many as needed.

The Company names are separate wide apart by spaces. Hence, I am unable to separate them with your solution.

For Example, After Amazon Uk, there will be 20 - 30 spaces followed by the name of the next company.Then another 20-30 spaces followed by the name of the another company. However , the number of spaces between the words of the same company is always 1. i.e between Amazon and UK..
 
Last edited:
Upvote 0
what you can do is use
=SUBSTITUTE(A1, " ","~") {there is a double space in the second argument}

To convert all you double spaces into ~, but leave the single spaces (e.g. between Amazon UK and Ltd) in place.

Then TextToColumns on ~ will split the different fields appropriately.

You may need to use TRIM to clean leading/trailing spaces off of the result.
 
Upvote 0
Assuming your data is in Column A starting on Row 1, here is a macro you can use to split the company names out into adjacent columns...
Code:
[table="width: 500"]
[tr]
	[td]Sub CompanyNames()
  Dim R As Long, vNum As Variant, Data As Variant
  Data = Range("[B][COLOR="#0000FF"]A1[/COLOR][/B]", Cells(Rows.Count, "[B][COLOR="#0000FF"]A[/COLOR][/B]").End(xlUp))
  For R = 1 To UBound(Data)
    Data(R, 1) = Replace(Data(R, 1), Space(2), Chr(1))
    Data(R, 1) = Replace(Data(R, 1), Chr(1) & " ", Chr(1))
  Next
  Range("[B][COLOR="#800080"]B[/COLOR][/B][B][COLOR="#0000FF"]1[/COLOR][/B]").Resize(UBound(Data)) = Data
  Columns("[B][COLOR="#800080"]B[/COLOR][/B]").TextToColumns , xlDelimited, , True, False, False, False, False, True, Chr(1)
End Sub[/td]
[/tr]
[/table]

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (CompanyNames) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Last edited:
Upvote 0
what you can do is use
=SUBSTITUTE(A1, " ","~") {there is a double space in the second argument}

To convert all you double spaces into ~, but leave the single spaces (e.g. between Amazon UK and Ltd) in place.

Then TextToColumns on ~ will split the different fields appropriately.

You may need to use TRIM to clean leading/trailing spaces off of the result.
Instead of having to do the step highlighted in red, just do one more substitution...

=SUBSTITUTE(SUBSTITUTE(A1, REPT(" ",2),"~"),"~ ","~")

Note I replaced the double space with a REPT function call to avoid this forum's problem displaying multiple spaces. This is basically the same method I used in my macro in Message #6.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,962
Messages
6,122,482
Members
449,088
Latest member
Melvetica

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