SUMIF in Visual Basic Function

  • Thread starter Thread starter magix
  • Start date Start date
M

magix

Can anyone translate the excel function SUMIF into Visual Basic Function
format ?
 
Dim var as Integer
var = 0
Do
If MyCondition = True Then
var = var + 1
Loop

Debug.Print var

'MH
 
The excel always complaint about circular references. how can i prevent that
?
If I use Application.WorksheetFunction.SumIf:
C1 = Application.WorksheetFunction.SumIf...

then I will get the alert of Circular reference. Why ? and How to get rid of
?


I need the value of SUMIF from C1 - C5, in order to compare with R1-R5

My code is as below:


Function CheckSession(S1 As String, S2 As String, S3 As String, S4 As
String, S5 As String, R1 As Integer, R2 As Integer, R3 As Integer, R4 As
Integer, R5 As Integer) As String


Dim Session As String
Dim C1 As Integer
Dim C2 As Integer
Dim C3 As Integer
Dim C4 As Integer
Dim C5 As Integer

C1 = Application.WorksheetFunction.SumIf(Range("AL5:AL3807"), "1",
Range("AP5:AP3807"))
C2 = Application.WorksheetFunction.SumIf(Range("AL5:AL3807"), "2",
Range("AP5:AP3807"))
C3 = Application.WorksheetFunction.SumIf(Range("AL5:AL3807"), "3",
Range("AP5:AP3807"))
C4 = Application.WorksheetFunction.SumIf(Range("AL5:AL3807"), "4",
Range("AP5:AP3807"))
C5 = Application.WorksheetFunction.SumIf(Range("AL5:AL3807"), "5",
Range("AP5:AP3807"))


Session = ""

If ((S1 <> "CLOSED") And (S1 <> "N/A")) Then
If C1 < R1 Then
Session = S1
End If
End If
If ((S2 <> "CLOSED") And (S2 <> "N/A")) Then
If C2 < R2 Then
If Session = "" Then
Session = S2
Else
If Val(Session) > Val(S2) Then
Session = S2
End If
End If
End If
End If

...
...

CheckSession = Session
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top