我需要一个用于MS 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 :(得分: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