PDA

View Full Version : Solved: Macro for manipulating text in a table



clhare
04-24-2007, 09:35 AM
I'm working on a macro to manipulate where text is added in a Word table and I can't figure out how to do the following:

Background: A userform will allow the user to type in the text they want added to the table. The macro will determine where in the table the text will be added. Only one column in each row should contain text. I need the macro to:



1) Check each column in the current row and determine if any of the columns contain text. If so, need to know which column contains the text. Based on the answer:
If none of the columns contain text, insert new text in first column of current row.

If one of the columns does have text, insert new text in the next column of the next row. (Example: If text is already in column 2 of the current row, a new row is added below the current row and the new text will go in column 3 of the new row. If text is already in the last column of the current row, a new row is added below the current row and the new text will go in first column of new row.)


2) Check entire table and make sure no rows have text in more than one column. If they do, can a macro create new rows below the "offending" row and reposition text in the new rows so that each row only has one column containing text?
Example: Row 10 of the current table has text in columns 1, 2, and column 3. Can macro add 2 new rows below row 10, move text from row 10 column 2 to new row 11 column 2, and move text from row 10 column 3 to new row 12 column 3 so that each row now has only one column with text in it?


Any help is greatly appreciated!

shasur
04-27-2007, 11:52 PM
Hope this gives you a hint Cheryl

Sub Search_TextIn_Tables()

Dim MyTab As Table
Dim sCellText
Dim bTextFound As Boolean


Set MyTab = ActiveDocument.Tables(1)

For i = 1 To MyTab.Columns.Count
bTextFound = False
For j = 1 To MyTab.Rows.Count
sCellText = Trim$(MyTab.Cell(j, 1).Range.Text)

' You need to trim the characters and then do the checking

' Here do the checking
If Len(sCellText) <> 0 Then
bTextFound = True
Exit For
End If
Next j
If sCellText = False Then
' Here your code if text is notfound in any column
Else
' Here your code if text is found in any column
End If
Next i

End Sub