Anh Le
Anh Le

Reputation: 1

mismatch error while trying to split cells in a range

I'm trying to run a mailmerge and I want to delete all rows beside the header in each table if the last cell of that row is blank.

Here is the line I'm getting mismatch error:

If Split(.Cell(r, c).Range.Text, vbCr) = 0

Below is the sub:

Sub Delete_row_if_last_cell_is_blank()
Application.ScreenUpdating = False
Dim Tbl As Table, r As Long, c As Long
ActiveDocument.MailMerge.Execute
With ActiveDocument
  For Each Tbl In .Tables
    With Tbl
      c = .Columns.Count
      For r = .Rows.Count To 2 Step -1
        If Split(.Cell(r, c).Range.Text, vbCr) = 0 Then .Rows(r).Delete
      Next
    End With
  Next
End With
Application.ScreenUpdating = False
End Sub

I tried to put the result as empty or "" but it still doesn't work.

Upvotes: 0

Views: 34

Answers (1)

Black cat
Black cat

Reputation: 6097

Split returns an array of the splitted items. Therefore instead of

Split(.Cell(r, c).Range.Text, vbCr) = 0

use

Ubound(Split(.Cell(r, c).Range.Text, vbCr)) = 0

which gives the array's top index value.

Upvotes: 0

Related Questions