如何使用for循环在“确定”之前合并空单元格

时间:2019-09-07 23:51:48

标签: vba ms-word

我对ms字表有疑问。 如何用for循环合并“ ok”之前的空单元格,然后用文本“ waiting list”替换空单元格, 样本:
#before:










#之后:
等待名单

等待名单

等待名单

Sub try()
Dim i As Integer, x As Integer, k As Integer
x = ActiveDocument.Tables(1).Rows.Count
'k = empty cell count before "ok"

For i = 1 To x - 1
   With ActiveDocument.Tables(1)
     If .Cell(i + 1, 1).Range.Text = Chr(13) & Chr(7) Then
     .Cell(Row:=i, Column:=1).Merge MergeTo:=.Cell(Row:=i + k, Column:=1)
     End If
   End With
Next i

For i = 1 To x - 1
   With ActiveDocument.Tables(1)
     If .Cell(i, 1).Range.Text = Chr(13) & Chr(7) Then
      .Cell(i, 1).Range.Text = “waiting list”         
    End If
   End With
Next i

End Sub

谢谢

1 个答案:

答案 0 :(得分:0)

尝试一下,让我知道它的进展,

Sub try()
Dim i As Integer, x As Integer, k As Integer
x = ActiveDocument.Tables(1).Rows.Count
'k = empty cell count before "ok"
For i = 1 To x - 1
   With ActiveDocument.Tables(1)
     If .Cell(i, 1).Range.Text = Chr(13) & Chr(7) And .Cell(i + 1, 1).Range.Text = Chr(13) & Chr(7) Then
        On Error Resume Next
            .Cell(Row:=i, Column:=1).Merge MergeTo:=.Cell(Row:=i + 1, Column:=1)
        On Error GoTo 0
        i = i - 1
        If i = ActiveDocument.Tables(1).Rows.Count - 1 Then
           GoTo EnterText
        End If
     End If
   End With
Next i

EnterText:
x = ActiveDocument.Tables(1).Rows.Count
For i = 1 To x
    With ActiveDocument.Tables(1)
        If .Cell(i, 1).Range.Text = Chr(13) & Chr(7) Then
          .Cell(i, 1).Range.Text = "waiting list"
        End If
    End With
Next i

End Sub