I am struggling with creating a VBA Function to calculate recurrences of a value after a specified value as been reached. In my spreadsheet I am looking to iterate through the rows of Column A of a spreadsheet, and when getting to a value, in my instance the identification number of a switch, add up recurrent values below it that match my credential;
Public Function CalculatePS(Ref As String)
Dim i As Long
Dim j As Long
Dim c1 As Long
Dim c2 As Long
Worksheets("Sheet1").Activate
Set Range = ActiveWorksheet.Columns("A")
For i = 1 To Range.End
If Right(Cells(i, "A").Value, 4) < Ref + 1000 Then
j = i + 1
ActiveCell = Cells(j, "A")
Do While ActiveCell.Value <> Empty
If ActiveCell.Value = psType Then
c1 = c1 + 1
End If
If ActiveCell.Value = psType" Then
c2 = c2 + 1
End If
Wend
i = j + 1
End If
Next i
MsgBox (c1)
MsgBox (c2)
Return c1
End Function
I had hoped this function would iterate through the rows until Right(Cells(i,"A).Value,4)
was equal to the reference number I was looking for in identifying switches on our network. I am fairly new to VBA and am sure I could do this in C, but cannot make it function in this environment. Currently, I am getting the error "argument not optional" when trying to compile with CalculatePS("2000").
Just to clarify, I have a list of switch names, and the power supplies assigned to them. with that I am trying to calculate how many 715w and 1100w power supplies are in all switches with the desired identification number.
This is an example of the spreadsheet:
biot-b348-uxxxx
C3KX-PWR-715WAC
C3KX-PWR-1100WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
C3KX-PWR-1100WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
C3KX-PWR-1100WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
BlankCell
biot-b348-uxxxx
C3KX-PWR-715WAC
C3KX-PWR-1100WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
C3KX-PWR-1100WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
C3KX-PWR-1100WAC
C3KX-PWR-715WAC
C3KX-PWR-715WAC
BlankCell
Thank you!
Editted code:
Public Function CalculatePS(uNumber As String, psType As String) As Integer
Dim i As Long
Dim j As Long
Dim c1 As Long
Worksheets("Sheet1").Activate
For i = 1 To rows.End
If Right(Cells(i, "A").Value, 4) < uNumber + 1000 And Right(Cells(i, "A").Value, 4) > uNumber Then
j = i + 1
Do While Cells(j,"A").Value <> Empty
If Cells(j,"A").Value = psType Then
c1 = c1 + 1
End If
Loop
i = j + 1
End If
Next i
CalculatePS = c1
End Function