Purge trailing space in a table

J

Janine

Below does not purge trailing space in a table:-

Fred Smith trailing space (no merge cells and anywhere in table rows)

What vba do tables require to clean up properly please?

Many thanks.

Sub Purge_Trailing_Space()
Dim s As String
With ActiveDocument
If Len(.Range) = 1 Then Exit Sub
s = .Characters.Last.Previous
While s = " " Or s = Chr(13) Or s = Chr(11)
.Characters.Last.Previous = ""
s = .Characters.Last.Previous
Wend
End With
End Sub
 
M

macropod

Hi Janine,

That code does not relate to tables per se, but to a document generally. In part, the code attempts to delete the penultimate
character if it's a space or a paragraph break. However, there must always be at least one paragraph (empty or otherwise) following
the last table in a document.

What, exactly, are you trying to achieve?
 
J

Janine

Hi Macropod - yes I figured that our (duh!).

I have a trailing autocorrect space in 3rd column but it could be any column
and the paragraph mark is non existent as I use styles and no carriage
returns (hardly ever).

Any way to search a trailing space with nothing following it?

macropod said:
Hi Janine,

That code does not relate to tables per se, but to a document generally.
In part, the code attempts to delete the penultimate character if it's a
space or a paragraph break. However, there must always be at least one
paragraph (empty or otherwise) following the last table in a document.

What, exactly, are you trying to achieve?

--
Cheers
macropod
[Microsoft MVP - Word]


Janine said:
Below does not purge trailing space in a table:-

Fred Smith trailing space (no merge cells and anywhere in table rows)

What vba do tables require to clean up properly please?

Many thanks.

Sub Purge_Trailing_Space()
Dim s As String
With ActiveDocument
If Len(.Range) = 1 Then Exit Sub
s = .Characters.Last.Previous
While s = " " Or s = Chr(13) Or s = Chr(11)
.Characters.Last.Previous = ""
s = .Characters.Last.Previous
Wend
End With
End Sub
 
G

Graham Mayor

Dim oRng As Range
Dim oTable As Table
Dim acell As Cell
For Each oTable In ActiveDocument.Tables
With oTable
For Each acell In oTable.Range.Cells
Set oRng = acell.Range
oRng.End = oRng.End - 1
oRng.Text = RTrim(oRng.Text)
Next acell
End With
Next oTable

will clear trailing spaces from all the cells in all the tables in your
document.
If you want to clear leading and trailing spaces change RTrim for Trim

Note that if you select the table and Click CTRL+E then CTRL+L all
leading and trailing white space will be cleared from the table.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



Janine said:
Hi Macropod - yes I figured that our (duh!).

I have a trailing autocorrect space in 3rd column but it could be any
column and the paragraph mark is non existent as I use styles and no
carriage returns (hardly ever).

Any way to search a trailing space with nothing following it?

macropod said:
Hi Janine,

That code does not relate to tables per se, but to a document generally.
In part, the code attempts to delete the penultimate character if it's a
space or a paragraph break. However, there must always be at least one
paragraph (empty or otherwise) following the last table in a document.

What, exactly, are you trying to achieve?

--
Cheers
macropod
[Microsoft MVP - Word]


Janine said:
Below does not purge trailing space in a table:-

Fred Smith trailing space (no merge cells and anywhere in table rows)

What vba do tables require to clean up properly please?

Many thanks.

Sub Purge_Trailing_Space()
Dim s As String
With ActiveDocument
If Len(.Range) = 1 Then Exit Sub
s = .Characters.Last.Previous
While s = " " Or s = Chr(13) Or s = Chr(11)
.Characters.Last.Previous = ""
s = .Characters.Last.Previous
Wend
End With
End Sub
 
J

Janine

Many thanks Graham I will try the macro. Using Alt 5 to select whole table
and Ctrl E and Ctrl L do bugger all in 2010 tables in my Public Beta.

But middle/left work fine outside of a table (go figure).


Graham Mayor said:
Dim oRng As Range
Dim oTable As Table
Dim acell As Cell
For Each oTable In ActiveDocument.Tables
With oTable
For Each acell In oTable.Range.Cells
Set oRng = acell.Range
oRng.End = oRng.End - 1
oRng.Text = RTrim(oRng.Text)
Next acell
End With
Next oTable

will clear trailing spaces from all the cells in all the tables in your
document.
If you want to clear leading and trailing spaces change RTrim for Trim

Note that if you select the table and Click CTRL+E then CTRL+L all
leading and trailing white space will be cleared from the table.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



Janine said:
Hi Macropod - yes I figured that our (duh!).

I have a trailing autocorrect space in 3rd column but it could be any
column and the paragraph mark is non existent as I use styles and no
carriage returns (hardly ever).

Any way to search a trailing space with nothing following it?

macropod said:
Hi Janine,

That code does not relate to tables per se, but to a document generally.
In part, the code attempts to delete the penultimate character if it's a
space or a paragraph break. However, there must always be at least one
paragraph (empty or otherwise) following the last table in a document.

What, exactly, are you trying to achieve?

--
Cheers
macropod
[Microsoft MVP - Word]


Below does not purge trailing space in a table:-

Fred Smith trailing space (no merge cells and anywhere in table rows)

What vba do tables require to clean up properly please?

Many thanks.

Sub Purge_Trailing_Space()
Dim s As String
With ActiveDocument
If Len(.Range) = 1 Then Exit Sub
s = .Characters.Last.Previous
While s = " " Or s = Chr(13) Or s = Chr(11)
.Characters.Last.Previous = ""
s = .Characters.Last.Previous
Wend
End With
End Sub
 
J

Janine

Thank you Graham Trim did the trick.

Graham Mayor said:
Dim oRng As Range
Dim oTable As Table
Dim acell As Cell
For Each oTable In ActiveDocument.Tables
With oTable
For Each acell In oTable.Range.Cells
Set oRng = acell.Range
oRng.End = oRng.End - 1
oRng.Text = RTrim(oRng.Text)
Next acell
End With
Next oTable

will clear trailing spaces from all the cells in all the tables in your
document.
If you want to clear leading and trailing spaces change RTrim for Trim

Note that if you select the table and Click CTRL+E then CTRL+L all
leading and trailing white space will be cleared from the table.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



Janine said:
Hi Macropod - yes I figured that our (duh!).

I have a trailing autocorrect space in 3rd column but it could be any
column and the paragraph mark is non existent as I use styles and no
carriage returns (hardly ever).

Any way to search a trailing space with nothing following it?

macropod said:
Hi Janine,

That code does not relate to tables per se, but to a document generally.
In part, the code attempts to delete the penultimate character if it's a
space or a paragraph break. However, there must always be at least one
paragraph (empty or otherwise) following the last table in a document.

What, exactly, are you trying to achieve?

--
Cheers
macropod
[Microsoft MVP - Word]


Below does not purge trailing space in a table:-

Fred Smith trailing space (no merge cells and anywhere in table rows)

What vba do tables require to clean up properly please?

Many thanks.

Sub Purge_Trailing_Space()
Dim s As String
With ActiveDocument
If Len(.Range) = 1 Then Exit Sub
s = .Characters.Last.Previous
While s = " " Or s = Chr(13) Or s = Chr(11)
.Characters.Last.Previous = ""
s = .Characters.Last.Previous
Wend
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top