我正在嘗試在 Word 中使用 VBA for 回圈來回圈遍歷表的行。常量列設定為表的第四列。如果給定行中的特定單元格具有文本“是”,我想將 1 添加到 X,我在代碼開頭將其初始化為 0。我的代碼不進行增量,并不斷回傳零或我啟動 X 的任何其他值。下面附上我當前的 VBA 代碼。
Sub Row_Iter()
Dim otable As Table
Set otable = ActiveDocument.Tables(1)
Dim Row As Integer
Dim Col As Integer
Col = 4
Dim x As Integer
''Section 1
With otable.Columns(Col)
x = 0
For Row = 4 To Row = 7
If otable.Cell(Row, Col).Range.Text = "Yes" Then
x = x 1
End If
Next Row
End With
結束子
uj5u.com熱心網友回復:
您的代碼有兩個問題。
第一個For Row = 4 To Row = 7應該是For Row = 4 To 7
第二個是If otable.Cells(Row, Col).Range.Text = "Yes"永遠不會評估為True。這是因為每個單元格都包含一個由兩個字符組成的單元格標記的非列印結尾。您可以通過檢查立即視窗中的空單元格的長度來清楚地看到這一點,例如
?len(ActiveDocument.Tables(1).Cell(1,1).Range.Text)
所以,你的代碼應該是:
Sub Row_Iter()
Dim otable As Table
Set otable = ActiveDocument.Tables(1)
Dim Row As Integer
Dim Col As Integer
Col = 4
Dim x As Integer
x = 0
For Row = 4 To 7
If left(otable.Cell(Row, Col).Range.Text, 3) = "Yes" Then
x = x 1
End If
Next Row
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456888.html
