View Full Version : help with For Each loop
For some reason I can't seem to refernce cells with a for each Loop in Word. I must be missing something simple. The line in red errors. I am trying to replace the text in cell 5 of each row with a static value.
Private Sub cmdSignOff_Click()
Dim CurrTable As Table
Dim myRow As Rows
Set CurrTable = FindTable("Req #")
For Each myRow In CurrTable.Range.Rows
Cells(5).Range.Text = "X"
Next
End Sub
I have tried
myRow.Cells(5).Range.Text = "X"
CurrTable.Cell(myRow,5).Range.Text = "X"
I know I could rewrite it as:
For I = 1 to Currtable.Rows.Count
CurrTable.Cell(I,5).Range.Text = "X"
Next I
but it's bugging me I can't get teh for each loop to work.
Greetings tla,
I rarely write in Word, but hopefully the below will assist.
Mark
Private Sub cmdSignOff_Click()
Dim CurrTable As Table
'//Rows is a property; returns a collection //
'Dim myRow As Rows
Dim myRow As Long
'// I'm not sure what your function 'FindTable' does, as it's not included; or, //
'// if this is a newer 2007 function of some type? (I have 2000 at home and //
'// 2003 at work. //
'Set CurrTable = FindTable("Req #")
'// I used below to check remainder... //
Set CurrTable = ActiveDocument.Tables(1) ' = FindTable("Req #")
'// I found trying to rotate thru CurrTable.Range.Rows resulting in an error, //
'// so not sure about this... //
'For Each myRow In CurrTable.Range.Rows
'// Again, I don't often use word, but as Cells is another property, I don't //
'// see how it could work? //
'Cells(5).Range.Text = "X"
'Next
'// How about: //
For myRow = 1 To CurrTable.Rows.Count
CurrTable.Cell(myRow, 5).Range.Text = "X"
Next
End Sub
fumei
11-25-2008, 10:58 AM
Your error is here.
Dim myRow As Rows
It should not be plural. It should be Row, not Rows.
Further,
CurrTable.Range.Rows
compounds the problem. Here is the proper code, and a demo attached. Click "Cell Change" on the top toolbar.
Option Explicit
Sub yadda()
Dim CurrTable As Table
Dim myRow As Row
Set CurrTable = ActiveDocument.Tables(1)
For Each myRow In CurrTable.Rows
myRow.Cells(5).Range.Text = "X"
Next
End Sub
I am assuming that the instruction:
Set CurrTable = FindTable("Req #")
works correctly, and does in fact make a fully qualified table object.
@fumei:
Thank you much :-) I didn't realize you could use Cells.
Mark
fumei
11-26-2008, 10:45 AM
You bet. Not only that but you can declare a Cell object, and use that. Here is another version. You can use the same click - "Cell Change" on the top toolbar. As you can see, it does the same thing...BUT...the difference is the code does not hard code the 5th cell. It actions the last cell of each row.
Sub yadda()
Dim CurrTable As Table
Dim myRow As Row
Dim myCell As Cell
Set CurrTable = ActiveDocument.Tables(1)
For Each myRow In CurrTable.Rows
Set myCell = myRow.Cells(myRow.Cells.Count)
myCell.Range.Text = "X"
Next
End Sub
Technically, you do not actually need the Cell object, as the code will still work as:
Sub yadda()
Dim CurrTable As Table
Dim myRow As Row
Set CurrTable = ActiveDocument.Tables(1)
For Each myRow In CurrTable.Rows
myRow.Cells(myRow.Cells.Count).Range.Text = "X"
Next
End Sub
However, I mention this because if you ever want to action multiple instructions on that cell, it is easier to use an object. Something like:Sub yadda_blah()
Dim CurrTable As Table
Dim myRow As Row
Dim myCell As Cell
Set CurrTable = ActiveDocument.Tables(2)
For Each myRow In CurrTable.Rows
Set myCell = myRow.Cells(myRow.Cells.Count)
With myCell
With .Range
.Text = "SecondTable"
.ParagraphFormat.Alignment = 1
End With
With .Shading
.Texture = wdTexture12Pt5Percent
.BackgroundPatternColorIndex = wdYellow
.ForegroundPatternColorIndex = wdBlack
End With
End With
Next
End Sub
Demo attached. I added a second table to demonstrate use of the Cell object. I call the second procedure (yadda_blah) at the end of the first one, so both tables are actioned.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.