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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Spaces and adding Tabs

Status
Not open for further replies.

dsaad

Technical User
Jun 3, 2004
15
US
On the block of copy below, how do I easily remove spaces and add tabs. Unless I do it line for line - I can't do it as a block and automatically remove spaces and add tabs.
Please advise.

Sat 07-09-05 9:00 MARINERS vs MARLINS at GAGNE
Sat 07-09-05 9:00 REDS vs BREWERS at RIC
Sat 07-09-05 9:00 at BRYANT
Sat 07-09-05 9:00 at WHEELER
Sat 07-09-05 12:00 METS vs YANKEES at GAGNE
Sat 07-09-05 3:00 ROCKIES vs HURRICANES at GAGNE
Sat 07-09-05 12:00 FIREFIGHTERS vs PAWTUCKET at RIC
Sat 07-09-05 12:00 at BRYANT
Sat 07-09-05 12:00 at WHEELER

Sun 07-10-05 9:00 at GAGNE
Sun 07-10-05 9:00 at RIC
Sun 07-10-05 9:00 at BRYANT

Sun 07-10-05 9:00 at WHEELER
Sun 07-10-05 12:00 at GAGNE
Sun 07-10-05 3:00 at GAGNE
Sun 07-10-05 12:00 at RIC
Sun 07-10-05 12:00 at BRYANT
Sun 07-10-05 12:00 at WHEELER

Sat 07-16-05 9:00 YANKEES vs MARLINS at GAGNE
Sat 07-16-05 9:00 FIREFIGHTERS vs REDS at RIC
Sat 07-16-05 9:00 at BRYANT
Sat 07-16-05 9:00 at WHEELER
Sat 07-16-05 12:00 HURRICANES vs METS at GAGNE
Sat 07-16-05 3:00 ROCKIES vs BREWERS at GAGNE
Sat 07-16-05 12:00 PAWTUCKET vs MARINERS at RIC
Sat 07-16-05 12:00 at BRYANT
Sat 07-16-05 12:00 at WHEELER
 
The following assumes that you with to have only ONE space converted to tab. e.g. date on one side of the tab, Text on the other, with trailing spaces removed.


Kink of difficult with Word.

With Excel (no code required).

Place all data on column A. If you copy from another place make sure you do a "paste special- Text only"

Place the following on Cell B1
=MID(A1,1,18)

place the following on Cell C1
=TRIM(MID(A1,18,100))

copy the contents of cell B1 and C1 to all rows of data.

Select all Cells B1 and C1, copy back to Word.

It will copy as a table, so to have it separated by Tabs you need to do a convert table to text, with separator "tab".


If you wish to have ALL spaces converted to tabs then you can do as follows.

1- Select all text
2- Select convert text to table with delimiter " " (space).
3- Select the resulting table
4- Select convert table to text with delimiter "tab".

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
It's pretty easy in Word actually!

If you can give a few more details of the format of the data, this can be tweaked, but for what you have posted - with a time ending in :00 and multiple spaces following it:

Press Ctrl+H to bring uo the Replace Dialog
Press More, if need be, to show the full details
Check Use Wildcards
Enter [blue][tt]:)00) {1,}[/tt][/blue] in Find What
Enter [blue][tt]\1^t[/tt][/blue] in Replace With
Press Replace All
Press OK in response to the message
Press Close to close the Dialog

Just to be sure: The find what string is:
[blue]left parenthesis, colon, zero, zero, right parenthesis, space, left brace, one, comma, right brace[/blue]
And the replace with string is:
[blue]back slash, one, caret, letter t[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Hi

I would first make a copy of the doc and work on the copy to make sure everything works out OK.

You can try highlighting the text, go to Edit/Replace. In the Find box, hit your space bar once. Put cursor in the Replace With box, and click on the More button. Click on the Special button and select Tab Character from the list. Click Replace All and that ought to do it.

HTH,

Best,
Blue Horizon [2thumbsup]
 
Try this. I am sure that Tony will find a shorter way. Unfortunately while it is possible to use Range rather than Selection for some of this, some other parts I can not seem to be able to use Range as well. But it does replace all your spaces in your example.

Essentially it looks for a space, expands the selection into the next character; if THAT character is another space, it extends itself again until there are no more CONSEQUETIVE spaces. It then deletes the whole lot of spaces, types in a Tab, and renews the search. If it finds a space followed by anything other than another space, it ignores that space.

Code:
Sub SpaceIntoTab()
Selection.HomeKey unit:=wdStory
With Selection.Find
Do While .Execute(findtext:=" ", Forward:=True) = True
    If .Execute(findtext:=" ", Forward:=True) = True Then
        Selection.MoveEnd unit:=wdCharacter
        If Right(Selection.Text, 1) = " " Then
            Selection.MoveEndWhile Cset:=" ", Count:=wdForward
            Selection.Delete
            Selection.TypeText Text:=vbTab
            Selection.Collapse direction:=wdCollapseEnd
        End If
    End If
Loop
End With
End Sub

Gerry
 
Hi,

Check out Get MS Word Copy/Paste-text to word wrap properly faq68-1449

Basically you would Edit/Replace two spaces with one space and a tab (^t)

Skip,
[sub]
[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue][/sub]
 
First you need to replace three spaces with two. Iterative process.

Then replace two spaces with space tab

Skip,
[sub]
[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue][/sub]
 
The code in my post expands the selection to how many consecutive spaces there are and replaces them with one tab. So 3 spaces, 2 spaces, 4 spaces or 13 spaces - don't matter, just as long as there is more than one.

Hopefully you have tab stops set up.

Gerry
 
Children, children,

My post, without VBA or iteration, does the job with a single Find/Replace operation. [wink]

Let's wait for the OP to confirm exactly which multiple spaces (if there are multiple sets) he wants to replace with tabs and we can tweak it accordingly.

Gerry,

I'm afraid my code would be longer because you need to set all the properties of the Find object because Word remembers them and you have no guarantees there isn't, say, a style set in there which would limit the scope of the Find operation. Apart from that, of course, you can find (1 or more) or (2 or more) or whatever variable number of spaces you like with a single find - no need to extend the selection (and hence no need to use Selection either).

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
or you could just cut & paste each column into a seperate TXT file, double click in the spaces, hit delete, save it, then bring it back into word or excel minus the spaces.

 


Tony,

Could you explain how your solution works?



Skip,
[sub]
[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue][/sub]
 
Hi Skip,

The Find string:

The first part is [blue]:00[/blue]. This is the end of the time - I just used it make sure that only multiple spaces after the time were replaced. It is enclosed in parentheses to delimit it as separate substring.

Next is a single space qualified by the numbers in the braces. The 1 before the comma means at least 1 (of the character before the braces - the space) and the absence of a second number mens there is no upper limit to the number to find.

Put it together nad it's looking for :00 followed by one or more spaces.

The replace string:

[blue]\1[/blue] means the first parenthesised (sub)string in the Find string - in this case :00

[blue]^t[/blue] is a tab character.

So :00 followed by one or more spaces is replaced by :00 followed by a tab.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top