使用VBA

时间:2018-08-17 01:18:50

标签: vba word-vba

这是我现在拥有的代码。

Do While IsEmpty(objTable.cell(intNoOfRow,2).Range.Text) = False
    intNoOfRows = intNoOfRows + 1
Loop

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

IsEmpty测试以查看Variant是否为空。对于False,它将始终返回String。针对vbNullString进行测试:

Do While objTable.cell(intNoOfRow,2).Range.Text = vbNullString
    intNoOfRows = intNoOfRows + 1
Loop

答案 1 :(得分:0)

我们不能使用以下代码: IsEmpty(objTable.cell(intNoOfRow,2).Range.Text)将始终为True,因此代码将陷入无限循环。虽然 Len(.Cell(intNoOfRows,2).Range.Text)<> 0 总是也返回True。我们不确定这是设计使然还是错误,但是新单元格始终包含字符。 Cell(intNoOfRow,2).Range.Text = vbNullString 也不起作用。

在我这边找到一种可行的方法( InStr([开始],字符串,子字符串,[比较] ):

With objTable
   Dim strCellText As String
   strCellText = .Cell(intNoOfRow, 2).Range.Text

Do While InStr(1, strCellText, "", vbBinaryCompare) <> 1  
           MsgBox "OK"
Loop

结尾为

一些旧尝试:

With objTable
For Each c In .Range.Rows
intNoOfRows = intNoOfRows + 1
Next
End With

您可以使用 .Range.Rows.Count ,但以下代码仍需要调整

With objTable
Do While intNoOfRows < .Range.Rows.Count
     intNoOfRows = intNoOfRows + 1
     'MsgBox .Range.Rows.Count
     'If intNoOfRows > .Range.Rows.Count Then
      '  Exit Do
     'End If

Loop
End With

答案 2 :(得分:0)

尝试根据以下内容进行尝试:

Sub Demo()
Dim r As Long, i As Long
With ActiveDocument.Tables(1)
  For r = 1 To .Rows.Count
    With .Rows(r)
      If Len(.Range.Text) = .Cells.Count * 2 + 2 Then i = i + 1
    End With
  Next
End With
MsgBox "There are " & i & " empty rows in the table"
End Sub

类似地,要在非空行之前添加一行,您可以使用类似以下内容的方法:

Sub Demo()
Dim r As Long, i As Long
With ActiveDocument.Tables(1)
  For r = 1 To .Rows.Count
    If Len(.Rows(r).Range.Text) > .Rows(r).Cells.Count * 2 + 2 Then
      .Rows.Add .Rows(r)
    End If
  Next
End With
End Sub