PDA

View Full Version : [SOLVED:] remove a tab and put in a space



mikewi
07-26-2016, 07:31 AM
Hello I'm new to this (VBA) and to this site. Is there any way to program VBA to remove all tabs and replace with a space?

gmayor
07-26-2016, 07:54 AM
You don't need a macro for that. You can use the replace function - CTRL+H.

mikewi
07-26-2016, 08:05 AM
I am running another macro that I was given by my boss, that does some sort of formatting, to my documents and was Hoping to just add another sub to it.

Paul_Hossler
07-26-2016, 09:46 AM
Using the macro recorder to generate the code fragment for just this single operation is a good way to learn, as well give you a starting point.




Option Explicit
Sub Macro2()
'
' Macro2 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^t"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

mikewi
07-26-2016, 09:58 AM
Thanks Paul works excellent.

gmaxey
07-27-2016, 05:56 AM
You can also do that inline you existing macro. Just add:
ActiveDocument.Range.Text = Replace(ActiveDocument.Range.Text, Chr(9), " ")

However, Graham's suggestion is the only one that is going to get "all" tabs. Paul's and mine will only get the one's in the main text storyrange (or in the case of Paul's the storyrange containing the selection).