2017-01-09 98 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