Log in

View Full Version : Code to indent text within multiple cells in a table



phrankndonna
12-04-2011, 11:25 AM
Hi,


I've got a routine whereby data is copied from Excel into a table in MS Word. Multiple rows are copied/pasted at one time. When the data gets opied, the text that's in the first column is oriented left in each cell. I need to then indent each of these cells two tab stops. After I select the block of cells that I need indented, I run the code below, and it does run through one time for the first cell. The code I've got below runs, and the Selection.Cells.Count code 'sees' the number of cells that are selected, and it does indent the first cell two tab stops, but then the code ends; it does not loop through each subsequent cell. How can I get it to continue the loop and indent all the selected cells? Thanks for any help/advice.

Sub IndentTextInCell()
Dim p As Paragraph
If Selection.Cells.Count > 0 Then
For Each p In Selection.Cells(1).Range.Paragraphs
p.Indent
p.Indent
Next p
End If
End Sub

Frank

gmaxey
12-04-2011, 12:53 PM
You need to loop through the cells:

Sub IndentTextInCell()
Dim p As Paragraph
Dim i As Long
For i = 1 To Selection.Cells.Count
For Each p In Selection.Cells(i).Range.Paragraphs
p.Indent
p.Indent
Next p
Next i
End Sub

phrankndonna
12-06-2011, 08:12 PM
This worked perfectly! Thank you VERY much!

Frank