【问题标题】:Word vba multiple table selectionWord vba 多表选择
【发布时间】:2017-01-11 04:46:22
【问题描述】:

我需要一个用于 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

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

【问题讨论】:

    标签: vba ms-office ms-word


    【解决方案1】:

    您可以使用以下模式(我删除了一些数据,但它会给您提供思路)来更轻松地处理它:

    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
    

    【讨论】:

      猜你喜欢
      • 2017-12-26
      • 2020-04-10
      • 2015-10-17
      • 1970-01-01
      • 2021-03-22
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多