R.Marcus
R.Marcus

Reputation: 1

Issue with nested if

Please can anyone point what wrong I am doing in the code given below, I am getting compilation error that for every EndIF there has to be corresponding IF THanks in advance:

Public Function customavg(rng As Range, nr_weeks As Integer)
Dim total As Integer, count_rng_row As Integer, count_wk As Integer, counter_rng As Integer

total = 0
count_rng_row = rng.Rows.count
count_wk = 0
counter_rng = count_rng_row


   For counter_rng = count_rng_row To 1
     If count_wk < nr_weeks Then
        If rng.Cells.Offset(0, -1) = "b" Then total = total + rng.Cells.Value
           counter_rng = counter_rng - 1
           count_wk = count_wk + 1
        End If
     'Else
      '     counter_rng = counter_rng - 0
       '    count_wk = count_wk + 0
     End If
   Next counter_rng

customavg = total / nr_weeks

End Function

Upvotes: 0

Views: 46

Answers (1)

tif
tif

Reputation: 1484

The culprit is

If rng.Cells.Offset(0, -1) = "b" Then total = total + rng.Cells.Value

The syntax of If is either

If <cond> Then <command>

OR

If <cond> Then
   <commands>
End If

Upvotes: 6

Related Questions