Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the current Table Index value in my VBA Macro?

2,160 views
Skip to first unread message

vrk1

unread,
Feb 27, 2008, 5:29:00 PM2/27/08
to
Hi,

I have a macro that splits the cells that have Hard returns inside all the
tables in my current document into separate rows. Someone sent me this macro
below to perform this function.

If I need this macro to operate only on the current Cell where my cursor is
and not all the tables in my document, how should I modify this?


Macro Below:

Sub RowSplit()

For x = 1 To ThisDocument.Tables.Count

For Each r In ThisDocument.Tables(x).Rows

If InStr(1, r, Chr(13)) < Len(r.Cells(1).Range) Then

rowsArray = Split(r.Cells(1).Range, Chr(13))
nSubRowCount = UBound(rowsArray)

If nSubRowCount > 1 Then
r.Select

For i = nSubRowCount To 1 Step -1
If Len(Replace(rowsArray(i), Chr(7), "")) > 0 Then
Selection.InsertRowsBelow
ThisDocument.Tables(x).Cell(r.Index + 1,
1).Range = rowsArray(i)
r.Select
End If
Next
Selection.Range = rowsArray(0)
End If

End If

Next
Next

End Sub

Peter A

unread,
Feb 27, 2008, 8:06:12 PM2/27/08
to
In article <37CECE3B-34C5-4124...@microsoft.com>, vrk1
@discussions.microsoft.com says...

> Hi,
>
> I have a macro that splits the cells that have Hard returns inside all the
> tables in my current document into separate rows. Someone sent me this macro
> below to perform this function.
>
> If I need this macro to operate only on the current Cell where my cursor is
> and not all the tables in my document, how should I modify this?

<snipped>

You should ask this question on one of the VBA groups.

--
Peter Aitken
Author, MS Word for Medical and Technical Writers
www.tech-word.com

Doug Robbins - Word MVP

unread,
Feb 28, 2008, 4:32:36 AM2/28/08
to
Replace

For x = 1 To ThisDocument.Tables.Count

For Each r In ThisDocument.Tables(x).Rows

With

For Each r In Selection.Tables(1).Rows

and delete the final Next

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"vrk1" <vr...@discussions.microsoft.com> wrote in message
news:37CECE3B-34C5-4124...@microsoft.com...

vrk1

unread,
Feb 28, 2008, 10:32:00 AM2/28/08
to
My apologies. I will try to move this to the Word programming area, if I can.

Just to follow through on your response, when I tried what you suggested I
get an error message:

"The requested member of the collection does not exist."

Any ideas? Thank you.

Doug Robbins - Word MVP

unread,
Feb 28, 2008, 2:34:19 PM2/28/08
to
You probably did not have the selection in a table when you ran it.

Enclose the routine in

If Selection.Information(wdWithInTable) = True Then
'code for routine here
End If

so that the code will only run if the selection is in a table.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"vrk1" <vr...@discussions.microsoft.com> wrote in message

news:F4FC2607-6EA1-46B9...@microsoft.com...

0 new messages