MS word 2016 Macro

杀马特。学长 韩版系。学妹 提交于 2019-12-12 04:37:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!