I have a file that is 3 columns, and traverse the 3rd column, find a keyword, and then calculate the median of the values in column 2 between that keyword and the next keyword.

Here is the code that gives me the output I need:

  For ictr = 1 To lastrow
    If Range("C" & ictr) = "stimulus" Then
      irevrowctr = ictr - 1                                                                'Start looking right above where 'stimulus' was found in col C
      Do Until Range("C" & irevrowctr) = "stimulus" Or irevrowctr = 1        'Check each cell above 'stimulus' for another 'stimulus'
        irevrowctr = irevrowctr - 1                                                      'Count back from where 'stimulus' was found
        irowctr = irowctr - 1                                                              'Counter used for range to plug into MEDIAN
      Loop
      Range("D" & ictr - 1).Activate                                                    'Select cell that is 1 above 'stimulus' and in column D
      ActiveCell.FormulaR1C1 = "=MEDIAN(R[" & irowctr & "]C[-2]:RC[-2])" 'Formula to find the median.
    End If
    irowctr = 0                                                                               'This counter is to count the lines that are ABOVE 'stimulus'
                                                                                                 'it must be negative for MEDIAN to work correctly in this program.
  Next
My problem comes when
ActiveCell.FormulaR1C1 = "=MEDIAN(R[" & irowctr & "]C[-2]:RC[-2])"
references the empty section of column 2 in the file.
The output for these values is "#NUM!". Is there an easy way to delete that so it's more appealing to the eye?

Thanks in advance for any advice!