【问题标题】:VBA - Get Index of Table depending on Values in the cellsVBA - 根据单元格中的值获取表索引
【发布时间】:2020-04-17 16:22:35
【问题描述】:

我想根据cell(1,2) and cell(1,1) 中的值获取一个表的索引号,然后我想自动调整这个表。 我试过了,但它不起作用:

Dim wApp As Object
Set wApp = CreateObject("Word.Application")
Dim wDoc As Object
Dim i as Integer
For i = 1 To wDoc.Tables.Count
    If wDoc.Tables(i).Cell(1, 1).Value = "Value1" And wDoc.Tables(i).Cell(1, 2).Value = "Value2" Then
       wDoc.Tables(i).AutoFitBehavior (2)
    End If
Next

但它不起作用..有人发现语法错误吗?

感谢您的帮助!

【问题讨论】:

  • wDoc 永远不是Set
  • 我试过Set wDoc = wApp.Documents.Open(/pathname) 但还是不行
  • 你得到什么错误信息?
  • 对象不支持此属性或方法。调试器显示此行:wDoc.Tables(i).Cell(1, 1).Value = "Value1" And wDoc.Tables(i).Cell(1, 2).Value = "Value2" Then
  • .Range.Text 而不是 .Value 我相信。

标签: excel vba ms-word word-table


【解决方案1】:

您需要消除单元格结尾标记,该标记显示为“¤”符号,由 Chr(13) 和 Chr(7) 组合组成。试试:

Dim wApp As Object, wDoc As Object, i As Long
Set wApp = CreateObject("Word.Application")
With wDoc
  For i = 1 To .Tables.Count
    With .Tables(i)
      If Split(.Cell(1, 1).Range.Text, vbCr)(0) = "Value1" Then
        If Split(.Cell(1, 2).Range.Text, vbCr)(0) = "Value2" Then
          .AutoFitBehavior (2)
        End If
      End If
    End With
  Next
End With

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多