2017-01-09 99 views
0

我需要一个MS Word的VBA代码,选择多个表(我选择)并输入一个基于文本框对象的字符串。我已经写了一个代码来做到这一点,但是如果我选择大量的表,它会变得重复。Word vba多表选择

Private Sub Claim_Change() 

    Dim j As Table 
    Set j = ActiveDocument.Tables(2) 
    Dim k As Table 
    Set k = ActiveDocument.Tables(3) 

'Input claim 
j.Select 
Selection.Delete 

With j 
.Cell(1, 1).Range.InsertAfter "Claim #: " & Claim 
.Cell(1, 2).Range.Text = Format(Date, "MMMM DD, YYYY ") 
.Columns.AutoFit 
End With 

k.Select 
Selection.Delete 

With k 
.Cell(1, 1).Range.InsertAfter "Claim #: " & Claim 
.Cell(1, 2).Range.Text = Format(Date, "MMMM DD, YYYY ") 
.Columns.AutoFit 

End With 
Claim.Select 

End Sub 

有没有更简单的方法来把这段代码放在一起?

回答

0

您可以使用以下方式(我删除了一些数据,但它会给你的想法)更容易地处理它:

Option Explicit 

Private Sub Claim_Change() 

Dim myTable As Table 
Dim tables() As Variant 
Dim tableCounter As Long 

tables = Array(1, 2, 5, 21) 

For tableCounter = LBound(tables) To UBound(tables) 

    ActiveDocument.tables(tables(tableCounter)).Select 
    Selection.Delete 

Next tableCounter 

End Sub