问题
I am new to VBA but I have a word 2016 template and it has the following sections:
Base Amount: $
Service Fee: $
Sales Tax % $
Other tax %: $
Tax 2 %: $
My goal is if an amount is missing from any of the lines, then delete that line.
I tried recording the macro and then making changes but no luck.
Sub TEST()
Dim i As Long
With Selection.Tables(1)
For i = .Rows.Count To 1 Step -1
If Len(.Cell(i, 2).Range.Text) = 2 Then
.Rows(i).Delete
End If
Next i
End With
End Sub
回答1:
I've added :
- the function
Trim
to remove useless spaces, so that you'll only have data left - changed the
=
to<=
Code :
Sub TEST()
Dim i As Integer
With Selection.Tables(1)
For i = .Rows.Count To 1 Step -1
If Len(Trim(.Cell(i, 2).Range.Text)) <= 2 Then
.Rows(i).Delete
End If
Next i
End With 'Selection.Tables(1)
End Sub
来源:https://stackoverflow.com/questions/43053248/ms-word-2016-macro