VBA:如果单词表中的第一个单元格有一定的值,则执行操作,否则转到下一个表



我试图通过Excel的VBA代码操纵多个Word表。Word文档中有多个需要填充的表,所以我认为我应该编写一个代码,遍历每个表,并识别第一个具有字符串" attquot;在第一个单元格(1,1)中。如果是这样,那么我将把Excel中的单元格复制到特定的表中。完成后,它将转到下一个表,并从电子表格中的另一个位置进行粘贴。这是我到目前为止想出的代码,但问题是它不能识别表格单元格中的文本,所以它一直到i=30。它只是跳过了有"att"在第一个细胞里。有什么建议吗?

Sub UpdateTables()
Dim documentname As String
Dim aktDocument As Document
Dim tbl As Word.Table
Dim i As Integer
Dim g As Integer
Dim xWB As Workbook
documentname = InputBox("Paste or Type the name of the cycle report word document")
Documents(documentname).Select
i = 1

Do Until i > 30      ' Loop through each table in the document and stop when the first cell says attribute in it 
Set aktDocument = ActiveDocument
Set tbl = aktDocument.Tables(i)

tbl.Select

If tbl.Cell(1, 1).Range.Text = "Att" Then ' Not recognizing text inside cell with this
'Populate this graph and then populate the next few graphs with summary table info
'Exit Loop below by making i = 31
i = 31
Else 'If first cell is not Att then end the if statement and loop again
End If

i = i + 1
Loop

End Sub

不要测试是否等价(=),而是测试是否存在(Instr) "Att"in Cell(1,1) of the Table.

Sub UpdateTables()

Do

Dim documentname As String
documentname = InputBox("Paste or Type the name of the cycle report word document")

Loop Until Len(documentname) > 0

Dim myDoc As Word.Document
Set myDoc = Documents(documentname)

Dim myTable As Word.Table
For Each myTable In myDoc.StoryRanges(wdMainTextStory).Tables   ' Loop through each table in the document and stop when the first cell says attribute in it

If InStr(myTable.Cell(1, 1).Range.Text, "Att") > 0 Then ' Not recognizing text inside cell with this

Dim myWb As Excel.Workbook
'Populate this graph and then populate the next few graphs with summary table info

' If you need to process additional tables before exiting the loop 
' not as part of the loop then 
' consider using the next method to get the next table in the document.
' Set myTable = myTable.Range.Next(unit:=wdTable)
Exit For

Else 'If first cell is not Att then end the if statement and loop again

End If

Next

End Sub

最新更新