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