Jean-Guy
Thu Sep 29 15:12:30 CDT 2005
Krakmup was telling us:
Krakmup nous racontait que :
> G'Day
>
> Is it possible to reduce the lines of code for the followng, assuming
> I want to find and delete rows in a table that contain "0", "-1",
> "-2", "-3" in column 1.
>
> CODE:
> If Selection.Information(wdWithInTable) = False Then Exit Sub
> TargetText = "0"
> For Each oRow In Selection.Tables(1).Rows
> If oRow.Cells(1).Range.Text = TargetText & vbCr & Chr(7) Then
> oRow.Delete Next oRow
>
> TargetText = "-1"
> For Each oRow In Selection.Tables(1).Rows
> If oRow.Cells(1).Range.Text = TargetText & vbCr & Chr(7) Then
> oRow.Delete Next oRow
>
> Repeating 4 lines of code seems like a waste of space.
> Thanks
> Krakmup
If TargetText always equals 0, -1, -2 and -3 then the most simple is to do
something like:
'_______________________________________
Dim oRow As Row
If Selection.Information(wdWithInTable) = False Then Exit Sub
With Selection.Tables(1)
For Each oRow In .Rows
If oRow.Cells(1).Range.Text = "0" & Chr(13) & Chr(7) Then
oRow.Delete
ElseIf oRow.Cells(1).Range.Text = "-1" & Chr(13) & Chr(7) Then
oRow.Delete
ElseIf oRow.Cells(1).Range.Text = "-2" & Chr(13) & Chr(7) Then
oRow.Delete
ElseIf oRow.Cells(1).Range.Text = "-3" & Chr(13) & Chr(7) Then
oRow.Delete
End If
Next oRow
End With
'_______________________________________
You could have a function, but since all you are doing is deleting the row,
it is not really worth it.
Or, if TargetText is defined during the procedure, then you can have a
function, like:
'_______________________________________
Sub CallDeleteRow()
Dim TargetText As String
If Selection.Information(wdWithInTable) = False Then Exit Sub
'Some code would generate TargetText,
'here we will set it "manually"
TargetText = 0
Select Case TargetText
Case "0", "-1", "-2", "-3"
DeleteRow Selection.Tables(1), TargetText
End Select
End Sub
'_______________________________________
'_______________________________________
Function DeleteRow(MyTable As Table, TargetText As String)
Dim oRow As Row
With MyTable
For Each oRow In .Rows
If oRow.Cells(1).Range.Text = TargetText _
& Chr(13) & Chr(7) Then oRow.Delete
Next oRow
End With
End Function
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site:
http://www.word.mvps.org