I have a recurring "Import Word to Excel" task that I do manually weekly and would like to automate with VBA the insertion of tabs for certain spaces.
I get ~200 lines of data in 2010 Word files that I need to import into Excel 2010. My problem is that the data is space delimited and I need to use Word to find and only replace certain
spaces (most but not all) with tabs. I need to find the first digit ("^#") in a line, then backward two spaces, then replace this and the next 6 spaces with tabs (^t"). The end of each line needs some space unchanged, but if I can do this, I think I can continue
on my own.
I have 2 lines of Example Data below and below that Word 2010 VBA code that I have created.
I get it to work occasionally but usually no. It seems the backward search after finding the first "^#" does not work most of the time.
It may be problems with spaces. What is the exact space character that should be used? Or it may be that I don't understand the find and how the selection changes after find? Or whatever?
I also cannot step through my VBA and see the cursor all the time. I don't understand why or what to set/do?
I would appreciate help in solving this problem.
Thank you.
Example Data
Description of XXX xxx 11 a+ 123 456 789 Supporting Data A+ 0.9 P
Description of YYY yyy 18 b- 981 654 321 More Supporting Date B- 0.75 Q
Sub test_TabAdd()
Selection.HomeKey unit:=wdStory
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.Find.ClearFormatting
Selection.Find.Execute Findtext:="^#", MatchWholeWord:=False, Forward:=True
Selection.Find.Execute Findtext:=" ", MatchWholeWord:=False, Forward:=False
Selection.Find.Execute Findtext:=" ", MatchWholeWord:=False, Forward:=False
Selection.Text = "^t"
For i = 1 To 7
Selection.Find.Execute Findtext:=" ", MatchWholeWord:=False, Forward:=True, Replacewith:="^t"
Next i
End Sub