0

I keep getting this error, but can't figure out why. It looks like all rules are followed.

The structure as I read is the following - If -> ElseIf -> End If. But here I get an error though it's all the same.

Sub hide()
        Application.ScreenUpdating = False
        Dim wRange As Range
        Set wRange = Range("A5:B10")

        Dim mergedRows As Integer
        Dim mergedColumns As Integer

        Dim cellFirst As Range

        For Each cell In wRange
            If IsEmpty(cell) Then
                cell.EntireRow.Hidden = True
            ElseIf cell.MergeCells Then
                mergeRows = cell.MergeArea.Rows.Count
                mergeColumns = cell.MergeArea.Columns.Count
                With cell.MergeArea
                    Set cellFirst = cell.MergeArea(Cells(1, 1))
                    If IsEmpty(cellFirst) Then
                        cellFirst.EntireRow.Hidden = True
                    End If
            End If
        Next
End Sub
3
  • 3
    type End With right before the last End If. In this case the error message is misleading, because the With block is the issue, not the If, but over time (and with good indenting practice, as you have done) you'll spot these much faster and know what to look for :) Commented May 23, 2017 at 13:22
  • 1
    @Scott Holtzman, Thanks! Didn't know With out to be finished too. You might answer, I'll accept. Commented May 23, 2017 at 13:24
  • you can actually remove the With cell.MergeArea as you never implement a proper With block. (nor do you need to). Commented May 23, 2017 at 13:26

1 Answer 1

2

You need to also close your With statement.

With cell.MergeArea
   Set cellFirst = cell.MergeArea(Cells(1, 1))
   If IsEmpty(cellFirst) Then
      cellFirst.EntireRow.Hidden = True
   End If
End With
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.