I'm trying to build a function that takes at least 1 range, and possibly several ranges as parameters - a little like the SUM worksheet function.
I've got this so far:
Public Function MyFunction(range1 As Range, _
Optional range2 As Range, _
Optional range3 As Range, _
Optional range4 As Range, _
Optional range5 As Range) As Integer
Dim count As Integer
Dim c As Range
count = 0
For Each c In range1.Cells
If c.Value = "S" Then
count = count + 1
End If
Next c
If range2 Is Not Nothing Then
For Each c In range2.Cells
If c.Value = "S" Then
count = count + 1
End If
Next c
End If
.... (same for range3 - range5)
MyFunction = count
End Function
When I run this I get an 'Invalid Use of Object' error on the function declaration line.
Can anyone help me please?
I've got this so far:
Public Function MyFunction(range1 As Range, _
Optional range2 As Range, _
Optional range3 As Range, _
Optional range4 As Range, _
Optional range5 As Range) As Integer
Dim count As Integer
Dim c As Range
count = 0
For Each c In range1.Cells
If c.Value = "S" Then
count = count + 1
End If
Next c
If range2 Is Not Nothing Then
For Each c In range2.Cells
If c.Value = "S" Then
count = count + 1
End If
Next c
End If
.... (same for range3 - range5)
MyFunction = count
End Function
When I run this I get an 'Invalid Use of Object' error on the function declaration line.
Can anyone help me please?