'Vba dynamic filter table getting errors when equating divide by zero with nothing

I have a table. For this table, I have a filter that gives another table that only shows items that I choose.

The idea is to show the average amount of the items shown in the filter table.

On VBA I have made the following code so whenever the value being filtered changes, it recalculates the averages.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cItem As Range
Dim iRow As Long
Dim ws As Worksheet

Set ws = Worksheets("Results")

With ws
  For Each cItem In .Range("K:K")
    If cItem.Row > 5 Then
        iRow = cItem.Row

        If cItem.Value <> "" Then
            .cells(iRow, 12).Formula = "=AVERAGEIF(H:H,K" & iRow & ",I:I)"
            'adds formula to cells that have items.

        Else
            Do Until .cells(iRow, 12).Value = ""
            'if cell is empty than clears the values from older filters

                .cells(iRow, 12).Value = ""
                iRow = iRow + 1

            Loop
            Exit For
        End If
    End If
  Next
End With

End Sub

Now, whenever I move from a filter that has more items than another that has less I get Div/0 values.

Div/0 table

When the code tries to run, I get a type mismatch error on the line ".cells(iRow, 12).Value = "" "

Aren't "div/0" different than "" ? How can I overcome this error?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source