views:

21

answers:

1

I have a whole bunch of tables that were formatted weird, because when a bunch of people make tables out of tabs, one cannot expect the tabbing to be consistent on all of them.

For example... I have this table: 2 cols by 11 rows... but I would like a macro that didnt care about the number of columns and rows

space space space **column header** tab tab tab tab **column header**
tab **data** tab tab tab **data** tab tab
tab **data** tab tab **data**
tab **data** tab tab tab tab **data**
tab **data** tab tab **data**

Currently, I have this macro

Sub ConvertTextToATable()

     Selection.ConvertToTable Separator:=wdWhite, AutoFitBehavior:=wdAutoFitFixed

End Sub

I've tried the tab separator... but both give similar results. It creates a new column every time a new white space character is encountered. What I want is ignoring all white space, and creating a new cell every time text is encountered, and creating a now row every time \n or \r is encountered.

And since I cant guarantee that every table with have the same number of column headers as the data, I think the number of columns should be determined by the second or third row in the text of original pseudo table.

Thank you so much!!

+1  A: 

DerNalia, this calls for a Regular Expression. Add a reference to Microsoft VBScript Regular Expressions 5.5 in your Office VBA editor: Image of VBA references

Then, in your Macro use the Regex objects to replace all multiple instances of a tab with just one tab. Then you can use your ConvertToTable command like normal.

Segfault
That works amazing... but I still need to know how to ignore the white space before text starts =/
DerNalia
Why regex? You can use the macro recorder to record a Word macro to find tab tab and replace with tab.
Remou
Because I have lines likespace space **data** tab tab **data**tab **data** tab tab **data**I need regex to remove the first occurrence of white space and replace the rest with commas... its time for a new question.link: http://stackoverflow.com/questions/2975490/using-regex-to-remove-the-first-occurrence-of-white-space-and-then-replace-the-re
DerNalia