run macro on all rows in the table

T

Tony

I want to execute the macro called on all rows in the table. As the
parameters for the macro I need to use values from first column (this is
assigned to SearchFor) and second column (this is assigned to PlaceNo). I can
not find the syntax to be used for running through all rows in the table and
assigning cells values to those two variables. Below I have placed the code I
am trying to fix.

*****************************************************

Sub ProcessTable()

Dim mytable As Table
Dim mycell As Cell
Dim myrange As Range

Set mytable = ActiveDocument.Tables(1)
With ActiveDocument.Tables(1).Range
intCount = 1
For Each Row In mytable
Set myrange = ActiveDocument.Tables(1).Cell(intCount, 1).Range
myrange.MoveEnd Unit:=wdCharacter, Count:=-1
SearchFor = myrange
Set myrange = ActiveDocument.Tables(1).Cell(intCount, 2).Range
myrange.MoveEnd Unit:=wdCharacter, Count:=-1
PlaceNo = myrange

OpenAllFilesInFolder
Next Row
End With

End Sub

*****************************************************

Can someone help me and advise how to modify the code so that it will work.

Thanks.

Tony
 
J

Jezebel

Dim pRow as long
Dim pSearchFor as string
Dim pPlaceNo as string

with ActiveDocument.Tables(1)
For pRow= 1 to .Rows.Count
pSearchFor = .Cell(pRow,1).Range
pPlaceNo = .Cell(pRow,2).Range

...

Next
End With

You'll probably need to strip the final two characters from both strings,
depending on what you're trying to do with all this (as it stands they'll
contain a paragraph mark and an end-of-cell mark). Also, the code may fail
if the table contains merged cells.
 
T

Tony

Hi Jezebel,

Thank you for your help. In the meantime I have modified my code to be:

*****************************************
Sub ProcessTable()

Dim mytable As Table
Dim mycell As Cell
Dim myrange As Range

Set mytable = ActiveDocument.Tables(1)
For irow = 1 To mytable.Rows.Count
For icol = 1 To mytable.Columns.Count
If icol = 1 Then
Set myrange = mytable.Rows(irow).Cells(icol).Range
myrange.MoveEnd unit:=wdCharacter, Count:=-1
SearchFor = myrange
Else
Set myrange = mytable.Rows(irow).Cells(icol).Range
myrange.MoveEnd unit:=wdCharacter, Count:=-1
PlaceNo = myrange
End If
Next icol

OpenAllFilesInFolder
Next irow

End Sub

****************************************

and this fixed my problem.

Regards,

Tony
 
T

Tony

Hi Jezebel,

Thank you for your help. In the meantime I have updated my code to be:

**************************************

Sub ProcessTable()

Dim mytable As Table
Dim mycell As Cell
Dim myrange As Range

Set mytable = ActiveDocument.Tables(1)
For irow = 1 To mytable.Rows.Count
For icol = 1 To mytable.Columns.Count
If icol = 1 Then
Set myrange = mytable.Rows(irow).Cells(icol).Range
myrange.MoveEnd unit:=wdCharacter, Count:=-1
SearchFor = myrange
Else
Set myrange = mytable.Rows(irow).Cells(icol).Range
myrange.MoveEnd unit:=wdCharacter, Count:=-1
PlaceNo = myrange
End If
Next icol

OpenAllFilesInFolder
Next irow

End Sub

**************************************

and it works perfectly for me.

Regards,

Tony
 

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