VBA字。如何在Word表中查找第一个空单元格?

问题描述 投票:0回答:3

我一直在尝试使用VBA在Word表中找到第一个空单元格。

“在单词表中找到单元格”

我在下面放置的代码将查找所有空单元格,而我想在填充一个单元格后找到第一个单元格。如何解决这个问题?

For Each oRow In Selection.Tables(1).Rows
For Each oCell In oRow.Cells
    If Selection.Text = Chr(13) & Chr(7) Then
    oCell.Select
    'Selection.PasteSpecial DataType:=wdPasteText
        MsgBox oCell.RowIndex & " " & oCell.ColumnIndex & " is empty."
    End If

Next oCell
Next oRow
vba ms-word
3个回答
0
投票

这是您的初衷吗?

Sub FindNextBlank()

    Dim Tbl As Table
    Dim TblRow As Row
    Dim HasText As Boolean
    Dim LookForText As Boolean, Done As Boolean
    Dim R As Long, C As Long
    Dim Txt As String

    LookForText = True
    With ThisDocument.Tables(1)
        For R = 1 To .Rows.Count
            Set TblRow = .Rows(R)
            For C = 1 To TblRow.Cells.Count
                HasText = (Len(TblRow.Cells(C).Range.Text) > 2)
                If HasText = LookForText Then
                    If LookForText Then
                        LookForText = Not LookForText
                    Else
                        Done = True
                        TblRow.Cells(C).Range.Select
                        Exit For
                    End If
                End If
            Next C
            If Done Then Exit For
        Next R

        If Done Then
            Txt = "Cell #" & C & " in row " & R & " is free."
        Else
            Txt = "No free cell was found that" & vbCr & _
                   " follows one that has text."""
        End If
    End With
    MsgBox Txt, vbInformation, "Search result"
End Sub

For ... Each更快,但我本能地不信任它,因为其中的项目顺序通常由其创建顺序决定。从上到下,从左到右,可能是也可能不是。通过它们的坐标调用单元格可能需要一点时间,但是您可以控制顺序。


0
投票

您可能已经发现,在Word中确定一个空单元格并不像看起来那样简单。下面的代码在删除所有空格,制表符和vbCr之后,查找第一个单元格中该单元格中文本长度为1的单元格。您可以将其扩展为也查找vbLF,手动换行符和其他可能在单元格中但如果您关闭了视图文本标记则不可见的字符。

表范围的.Cells方法是此处最适合使用的工具,因为即使表已合并单元格,它也将起作用。如果表中有合并的单元格,则使用单元格坐标搜索表将失败。使用.Cells方法,从左上角到右下角(逐列)搜索表。

Option Explicit

Sub Test()

    Dim myCell As Word.Range
    Set myCell = FirstEmptyCell(ActiveDocument.Tables(1).Range)
    myCell.Select

End Sub

' Returns the first cell that has a text length of 1
' after removing spaces and tab characters from the cell text
Public Function FirstEmptyCell(ByVal TableRange As Word.Range) As Word.Range

    Dim myCell As Word.Cell
    For Each myCell In TableRange.Tables(1).Range.Cells

        Dim CellText As String
        CellText = myCell.Range.Text
        CellText = Replace(CellText, vbTab, vbNullString)
        CellText = Replace(CellText, " ", vbNullString)
        CellText = Replace(CellText, vbCr, vbNullString)

        If Len(CellText) = 1 Then

            Set FirstEmptyCell = myCell.Range
            Exit Function

        End If

    Next

End Function

0
投票

该解决方案确实比其他“答案”建议简单得多:

Dim i As Long
With Selection
  If .Information(wdWithInTable) = True Then
    With .Tables(1).Range
    For i = 1 To .Cells.Count
      With .Cells(i)
        If Len(.Range.Text) = 2 Then
          MsgBox " Row " & .RowIndex & ", Column " & .ColumnIndex & " is empty."
          .Range.PasteSpecial DataType:=wdPasteText
          Exit For
        End If
      End With
    Next
    End With
  Else
    MsgBox "No table selected", vbExclamation
  End If
End With

我什至还添加了一些错误检查。

© www.soinside.com 2019 - 2024. All rights reserved.