how to select one table in a document with multiple tables?

O

OliverTwist

hi all,

i'm looking forward to find a possibility to select one table out of a known
number of tables in my document, and then i want to apply a macro to this
table.
e.g. my document consists of 14 tables an i want the macro to be applied to
table no. 12 using the following procedure:

Sub Verdict_Return_Oliver_Small_Template()
'
' Verdict_Return Macro
' Macro created 26/04/2005
'
ActiveWindow.ScrollIntoView Obj:=Selection.Range, Start:=True

For i = 2 To ActiveDocument.Tables(1).Rows.Count

Selection.Tables(1).Cell(i, 6).Select
LowerLimit = Selection.Calculate
Selection.Tables(1).Cell(i, 7).Select
UpperLimit = Selection.Calculate
Selection.Tables(1).Cell(i, 8).Select
Meas = Selection.Calculate

Selection.Tables(1).Cell(i, 9).Select

If LowerLimit <= Meas And Meas <= UpperLimit Then

Selection.Font.Color = wdColorGreen
Selection.TypeText Text:="PASS"

Else: Selection.Font.Color = wdColorRed
Selection.TypeText Text:="FAIL"
End If

Next i

End Sub

i tried to replace the no "1" in "ActiveDocument.Tables(1).Rows.Count" by 13
but it didn't work.
does anyone know another solution or do i only make a small mistake?

thanks so far, this is one of the best newsgroups i ever saw an my small
procedure was already created with help of you all.

BR,
Oliver
 
G

Greg

Oliver,

Replacing Tables(1) with Tables(12) throughout should work.

BTW, you don't need to do all of that "selecting" in your procedure.
Consider something like this as an alternative:

Sub Verdict_Return_Oliver_Small_Template()

Dim oTable As Table
Dim LowerLimit As Single
Dim UpperLimit As Single
Dim Meas As Single
Dim i As Long

ActiveWindow.ScrollIntoView Obj:=Selection.Range, Start:=True

Set oTable = ActiveDocument.Tables(12)
For i = 2 To oTable.Rows.Count
LowerLimit = oTable.Cell(i, 6).Range.Calculate
UpperLimit = oTable.Cell(i, 7).Range.Calculate
Meas = oTable.Cell(i, 8).Range.Calculate

If LowerLimit <= Meas And Meas <= UpperLimit Then
oTable.Cell(i, 9).Range.Font.Color = wdColorGreen
oTable.Cell(i, 9).Range.Text = "PASS"
Else
oTable.Cell(i, 9).Range.Font.Color = wdColorRed
oTable.Cell(i, 9).Range.Text = "Green"
End If
Next i


End Sub
 
G

Greg

Oliver,

As a further refinement, you could use With and End With:

Sub Verdict_Return_Oliver_Small_Template()

Dim oTable As Table
Dim LowerLimit As Single
Dim UpperLimit As Single
Dim Meas As Single
Dim i As Integer

ActiveWindow.ScrollIntoView Obj:=Selection.Range, Start:=True

Set oTable = ActiveDocument.Tables(12)
For i = 2 To oTable.Rows.Count
LowerLimit = oTable.Cell(i, 6).Range.Calculate
UpperLimit = oTable.Cell(i, 7).Range.Calculate
Meas = oTable.Cell(i, 8).Range.Calculate

With oTable.Cell(i, 9).Range
If LowerLimit <= Meas And Meas <= UpperLimit Then
.Font.Color = wdColorGreen
.Text = "PASS"
Else
.Font.Color = wdColorRed
.Text = "FAIL"
End If
End With
Next i


End Sub
 

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