MS Word VBA删除空行

时间:2017-03-28 14:04:54

标签: vba ms-word word-vba

我是VBA Macros的新手,但我有一个单词2016模板,它有以下几个部分:

Base Amount: $ 
Service Fee: $ 
Sales Tax % $
Other tax %: $
Tax 2 %: $ 

我的目标是,如果任何一行中缺少金额,则删除该行。我尝试录制宏,然后进行更改,但没有运气。

Image of Word table

1 个答案:

答案 0 :(得分:0)

更新:以下是代码的修改版本,它将通过表格的所有行(在MS-Word中)旋转,并删除仅包含' $'第3列和第4列是空的。

Option Explicit

' This subroutine will spin thru all rows of data in a table in MS-Word (except the first row, which is the header).
' If column 3 contains only a '$' AND column 4 is empty, the row will be deleted.
' All rows with only the '$' in column 3 and empty cell 4 will be deleted.

Sub TEST()
Dim i       As Long
'Dim char    As String

    With Selection.Tables(1)
        For i = .Rows.Count To 1 Step -1
            'char = .Cell(i, 3).Range.Text
            'Debug.Print Asc(Mid(char, 1, 1)) & vbTab & Asc(Mid(char, 2, 1)) '& vbTab & Asc(Mid(char, 3, 1))
            'Debug.Print ">" & .Cell(i, 2).Range.Text & "<"
            If Len(.Cell(i, 4).Range.Text) = 2 And Left(.Cell(i, 3).Range.Text, 1) = "$" Then
                .Rows(i).Delete
            End If
        Next i
    End With
End Sub