-1

enter code herei try to sum all positive value in my array but every time object not found or parameter misssing error seen.

Option Explicit Sub trail2() With ThisWorkbook Dim arr1 As New ArrayList arr1.Add 20 arr1.Add -15 arr1.Add 20.77 msgbox Application.WorksheetFunction.CountIf(arr1, ">0") end with end sub 

error = object not found or parameter misssing error seen

i also try on range but thats also not working

Option Explicit Sub trail2() dim rr as variant dim i as variant With ThisWorkbook rr = Range(.Sheets("data").Cells(2, 5), .Sheets("data").Cells(4, 5)) msgbox Application.WorksheetFunction.CountIf(rr, ">0") end with end sub 

same data as per array but error seen. error = object required i don't want that sheet data so i need only array for sumif. how to slove it ?

4
  • 1
    The first parameter of CountIf must be a Range, so that approach won't work. Use a loop. Commented Jul 21, 2023 at 13:01
  • msgbox = .... ----> MsgBox .... No =. Commented Jul 21, 2023 at 13:02
  • "I also try on range": you actually didn't. rr is a Variant array, not a Range. Commented Jul 21, 2023 at 13:35
  • You need Set rr = Range(.Sheets("data").Cells(2, 5), .Sheets("data").Cells(4, 5)) Commented Jul 21, 2023 at 13:42

1 Answer 1

1

When assigning a range to a variable, you need to use Set

You should also use the worksheet your range is on to avoid it pulling from an active sheet.

Option Explicit Sub trial2() Dim rr As Range Dim i As Variant With ThisWorkbook.Sheets("Data") Set rr = .Range(.Cells(2, 5), .Cells(4, 5)) MsgBox Application.WorksheetFunction.CountIf(rr, ">0") End With End Sub 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.