Copying every other Cell Word 2003

J

Jim

I have been using the macro below to copy the first cell and then copy to
all other cells on the Sheet. However with Labels I actually need to copy to
every other
cell horizontally, and then miss the next row of cells, as they are used
for vertical spacing
and then copy every other cell again, as the gap is needed to space the
label horizontally,
so it becomes tedious to delete the contents of every other Cell. Can this
macro be easily modified?
Jim

Sub TableFillWithThisCell()
' Fills all cells in current table with contents of current cell
Dim objSelObj As Cell

Application.ScreenUpdating = False

On Error Resume Next ' In case insertion point is not within a table or
selected range is not within one cell
Selection.SelectCell
If Err.Number <> 0 Then
Beep
Exit Sub
End If
On Error GoTo 0

Set objSelObj = Selection.Cells(1)

Selection.Copy
Selection.SelectRow
Selection.SelectColumn
Selection.Paste

objSelObj.Select
Selection.Collapse

Application.ScreenUpdating = True

End Sub
 
G

G.G.Yagoda

Assumes that cursor is within the table and that odd numbered rows will
contain text while even numbered rows will remain blank.

Dim Tbl As Table, Rw As Row, C As Cell
Dim Rng As Range, RwNo As Integer, n As Byte
Set Tbl = Selection.Tables(1)
Set Rng = Tbl.Rows(1).Cells(1).Range
Rng.End = Rng.End - 1
Rng.Copy
For RwNo = 1 To Tbl.Rows.Count Step 2
Set Rw = Tbl.Rows(RwNo)
For Each C In Rw.Cells
C.Range.Text = ""
C.Range.Paste
Next
Next
 
J

Jim

Hi
Many thanks for your macro which works fine for the rows.
However, the columns only need to paste into alternate cells (ie:
col1,3,5,7etc)
and leave the even ones blank (ie: col2,4,6,8)fill in when having
material pasted into the sheet.
Thanks for your help so far.
Jim
 
G

G.G.Yagoda

See if this does the trick, Jim:

Dim Tbl As Table, Rw As Row, C As Cell
Dim Rng As Range, RwNo As Integer, n As Byte
Set Tbl = Selection.Tables(1)
Set Rng = Tbl.Rows(1).Cells(1).Range
Rng.End = Rng.End - 1
Rng.Copy
For RwNo = 1 To Tbl.Rows.Count Step 2
Set Rw = Tbl.Rows(RwNo)
For n = 1 To Rw.Cells.Count Step 2
Set C = Rw.Cells(n)
C.Range.Text = ""
C.Range.Paste
Next
Next
 
J

Jim

Yes, that works well. I notice in error trapping on say using a blank page
I get an error 91 variable not set which refers to object variable not set:
Rng.End = Rng.End - 1
What should I set the variable Rng.End as?
Below is your modified code.
Many thanks for your help
Jim

On Error Resume Next
Dim Tbl As Table, Rw As Row, C As Cell
Dim Rng As Range, RwNo As Integer, n As Byte
Set Tbl = Selection.Tables(1)
Set Rng = Tbl.Rows(1).Cells(1).Range
On Error GoTo 0
Application.ScreenUpdating = False
Rng.End = Rng.End - 1
Rng.Copy
For RwNo = 1 To Tbl.Rows.Count Step 2
Set Rw = Tbl.Rows(RwNo)
For n = 1 To Rw.Cells.Count Step 2
Set C = Rw.Cells(n)
C.Range.Text = ""
C.Range.Paste
Next
Next
Application.ScreenUpdating = True
 
G

G.G.Yagoda

Each table cell has an end of cell mark in it as represented by the
little square box. Rng.End = Rng.End - 1 means "don't include the end
of cell mark when you copy the text."

I can't seem to duplicate the error message; as long as I'm in a table
and there's text in the first cell, it works.

Maybe someone else will see the cause of the error and explain it to us
both.
 
J

Jim

Yes, that is right, where it falls over is if you are on a blank page
and someone who doesn't understand how to get out of a
macro panics!
Only trying to make it idiot proof!!!
Many thanks for your input. Much appreciated.
Jim
 
G

G.G.Yagoda

Add this at the beginning of the macro. It should prevent user panic:

If Not Selection.Information(wdWithInTable) Then
MsgBox "Cursor must be in a table. Quitting.", , "Not in Table"
Exit Sub
End If
 
Top