PDA

View Full Version : Selecting non adjacent columns in a word table



Vbsponge
11-26-2008, 02:56 PM
Hi all. I'm new here and have only just started learning vb.

i've got a 5 column word table that i need to edit. I'm trying to replace spaces with the tab character for only the 2nd and 5th columns. I've only got the one table in the document.

Its probably really simple and i've been able to apply the changes to the 2nd column only by using
selection.column(2).select

this however doesn't work if i try to repeat it with the 5th column using the same code and changing (2) to (5).

Any help from all the gurus out there will be greatly appreciated.

Many thanks

TonyJollans
11-26-2008, 03:49 PM
Difficult to know without seeing the code, or at the very least, knowing what error you get. From what you say it sounds as if it ought to work, so there must be something more to it.

Vbsponge
11-27-2008, 01:58 AM
hi tony and thanks for your reply.

i don't know why either. i've recorded the macro and the only reason i'm tweaking this section is because i don't seem to be able to select the columns i want while recording the macro. i've had to pause the recording to select the columns. however when i rerun the macro, the columns i selected to perform tasks on aren't the only ones selected. The whole table is selected hence the need to tweek the code.

i've got a list of addresses in word that i've got to put in excel, but need to separate the data into 7 different columns. My macro is meant to find and replace certain spaces with the tab character whilst it is in a table so that not all spaces in the addresses are removed completely.

Here's what i've got at the mo.


Selection.Find.Execute Replace:=wdReplaceAll
Selection.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=5, _
NumRows:=60, AutoFitBehavior:=wdAutoFitContent
With Selection.Tables(1)
.Style = "Table Grid"
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Selection.Columns(2).Select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = "^t"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll



i know there's a lot of unneccessary code in there, but i figured i'd clean it up after i got the macro to work.

hope i haven't confused you.

Vbsponge
11-27-2008, 02:01 AM
When i repeat the selection.columns(2).Select part replacing the (2) with (5), i get the following error message

"Run-time error '5941'
The requested member of the collection does not exist."

TonyJollans
11-27-2008, 05:20 AM
I suspect your problem is because you are using the Selection - and it (as opposed to the Table) does not have 5 columns.

I'll go with you about making it work first and cleaning it up later, so you have:

With Selection.Tables(1)
' do some stuff
End With
Selection.Columns(2).Select

Try changing it to:

With Selection.Tables(1)
' do some stuff
.Columns(2).Select
End With

fumei
11-27-2008, 12:19 PM
You need to clearly define: "My macro is meant to find and replace certain spaces with the tab character "

WHICH certain spaces?

If it is the first space, this is not terribly difficult. Demo attached. Click "Change Table" on the top toolbar. Notice that the text in columns 2 and 5 (ONLY) are changed from:

<space>Space and other text Row 1 2 Col2

to

<Tab>Space and other text Row 1 2 Col2

Here is the code:
Option Explicit

Sub yadda()
Dim aTable As Table
Dim ColCells As Cells
Dim aCell As Cell
Dim j As Long

Set aTable = ActiveDocument.Tables(1)

Set ColCells = aTable.Columns(2).Cells
For j = 1 To ColCells.Count
Set aCell = ColCells(j)
aCell.Range.Text = _
vbTab & Right(aCell.Range.Text, _
Len(aCell.Range.Text) - 1)
Next

Set ColCells = aTable.Columns(5).Cells
For j = 1 To ColCells.Count
Set aCell = ColCells(j)
aCell.Range.Text = _
vbTab & Right(aCell.Range.Text, _
Len(aCell.Range.Text) - 1)
Next
End Sub

fumei
11-27-2008, 12:22 PM
Note that Selection is not used at all.

Once you have the logic that determines what "certain" spaces actually means, I am pretty sure the code can be adjusted to fit.