Если какая-либо из ячеек в столбце таблицы (MS Word) содержит определенный текст, отобразите MsgBox - PullRequest
0 голосов
/ 13 мая 2019

Я пытаюсь выполнить код проверки ошибок, который проверяет, правильно ли завершены некоторые конкретные ячейки, перед запуском более сложного кода.

Пока что я написал следующий код:

Dim tabel As Range
Set tabel = ActiveDocument.Sections(2).Range
tabel.Select
Dim oCel As Cell
With ActiveDocument.Tables(1).Columns(3)
  For Each oCel In .Cells
   Selection.Find.Execute FindText:="Petent"
    If Selection.Find.Found = False Then
    MsgBox "Intr-un dosar nu este inregistrat Petent"
    End If
   Next
End With
tabel.Select
With ActiveDocument.Tables(1).Columns(3)
  For Each oCel In .Cells
Selection.Find.Execute FindText:="Creditor"
    If Selection.Find.Found = False Then
    MsgBox "Intr-un dosar nu este inregistrat Creditor"
    End If
Next
End With
tabel.Select
With ActiveDocument.Tables(1).Columns(3)
  For Each oCel In .Cells
Selection.Find.Execute FindText:="Debitor"
    If Selection.Find.Found = False Then
    MsgBox "Intr-un dosar nu este inregistrat Debitor"
    End If
Next
End With

Выполняя пошаговую отладку, я обнаружил, что, когда определенное слово не найдено в ячейке, оно переходит к следующей ячейке, не отображая сообщение.

В чем проблема в этом случае?

Ответы [ 2 ]

0 голосов
/ 15 мая 2019

Другой способ, используя Find:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long, ArrFnd()
ArrFnd = Array("Petent", "Creditor", "Debitor")
With ActiveDocument.Tables(1)
  Set Rng = .Range
  For i = 0 To UBound(ArrFnd)
    With .Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "<" & ArrFnd(i) & ">"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        If .InRange(Rng) = False Then Exit Do
        If .Cells(1).ColumnIndex = 3 Then
          MsgBox "Intr-un dosar nu este inregistrat " & ArrFnd(i)
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
0 голосов
/ 13 мая 2019

Вы уже просматриваете отдельные ячейки, зачем вообще использовать .Find?Просто проверьте значение самой ячейки:

Dim oCel As Cell

With ActiveDocument.Tables(1).Columns(3)

    For Each oCel In .Cells
        If InStr(oCel.Range.Text, "Petent") = 0 Then
            MsgBox "Intr-un dosar nu este inregistrat Petent"
        End If
    Next

    For Each oCel In .Cells
        If InStr(oCel.Range.Text, "Creditor") = 0 Then
            MsgBox "Intr-un dosar nu este inregistrat Creditor"
        End If
    Next

    For Each oCel In .Cells
        If InStr(oCel.Range.Text, "Debitor") = 0 Then
            MsgBox "Intr-un dosar nu este inregistrat Debitor"
        End If
    Next

End With
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...