0
Sub test()

    'Dim total(1 To 9) As Variant
    ReDim total(1 To 9) As Variant


    For i = 1 To 9

    total(i) = Application.WorksheetFunction.SumIf(Sheets("Sheet2").Range(Cells(3, 3), Cells(i, 3)), ">" & _
    Sheets("Sheet2").Range(Cells(3, 6), Cells(i, 6))-60, _
    Sheets("Sheet2").Range(Cells(3, 4), Cells(i, 4)))
    Next i


End Sub

Can anyone tell why i keep getting this error although i am using variant for my array? everything seems fine to me. What i am trying to do is to deduct 60 days from the date in the cells, then sum up the amount between the initial date and deducted date. so it would be like: enter image description here

  • what is your sub supposed to do? – DisplayName Jun 11 '18 at 06:01
  • Sheets("Sheet2").Range(Cells(3, 6), Cells(i, 6)) is expected to be a single cell or value, not a range. –  Jun 11 '18 at 06:03
  • The `Cells(3, 3), Cells(i, 3)` that define `Sheets("Sheet2").Range(...)` have no defined parent worksheet. See [Is the . in .Range necessary when defined by .Cells](https://stackoverflow.com/questions/36368220/is-the-in-range-necessary-when-defined-by-cells) –  Jun 11 '18 at 06:05
  • btw, you didn't `dim i as long`. –  Jun 11 '18 at 06:32

1 Answers1

0
  1. Sheets("Sheet2").Range(Cells(3, 6), Cells(i, 6)) (the criteria parameter) is expected to be a single cell or value, not a range.
  2. The Cells(3, 3), Cells(i, 3) that define Sheets("Sheet2").Range(...) have no defined parent worksheet. See Is the . in .Range necessary when defined by .Cells?

    with workSheets("Sheet2")
        For i = lbound(total) To ubound(total)
            total(i) = Application.SumIf(.Range(.Cells(3, 3), .Cells(i, 3)), _
                                         ">" & .Cells(i, 6), _
                                         .Range(.Cells(3, 4), .Cells(i, 4)))
        Next i
    end with
    
  • 1
    Are we sure they didn't mean an array formula. My assumption was this was what they were going for (perhaps incorrect) ?i.e. Did I miss something in the question ? – QHarr Jun 11 '18 at 06:15
  • It's possible but an array formula cannot be used like the original; awaiting confirmation. –  Jun 11 '18 at 06:17