我正在努力创建一个VBA函数来计算达到指定值后的值的重现。在我的电子表格中,我希望迭代电子表格的A列行,当我获取一个值时,在我的实例中,一个开关的标识号,在它下面添加与我的凭证匹配的循环值;
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
我曾希望此函数会遍历行,直到Right(Cells(i,"A).Value,4)
等于我在查找网络中的开关时所寻找的参考号。我对VBA相当新,并且我确信我可以在C中执行此操作,但无法在此环境中使其正常运行。目前,我收到的错误&#34;参数不是可选的&#34;尝试使用CalculatePS进行编译时(&#34; 2000&#34;)。
为了澄清,我有一个开关名称列表,以及分配给它们的电源。我试图用所需的识别码计算所有开关中有多少715w和1100w电源。
这是电子表格的一个示例:
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
谢谢!
编辑代码:
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
答案 0 :(得分:0)
通常我更愿意将任务分成几个其他列。例如,一列可以right(A1, 4)
来提取唯一编号uXXXX,并且在有足够的列来覆盖所有条件之后,您可以使用简单的countif
语句来实现您想要的。我假设您无权更改工作表的格式。
此外,从您的语法中可以明显看出,您可能从未在VBA中编写过,因此我稍微更改了您的代码。再一次,假设您没有自由使用多列来实现您想要的东西,以下函数可能有所帮助(=CalculatePS(A:A,"1234")
)。
'@param vInputs variant type for your to pass range containing your input (in your case, select A:A
'@param strUID string type representing the unique number, XXXX, in uXXXX (note to pass as "XXXX" instead of XXXX)
Public Function CalculatePS(vInputs As Variant, strUID As String) As String
Dim vTemp As Variant: vTemp = vInputs
Dim intCounterTotal As Long
Dim intCounterRelevant As Long
Dim intNumOf715 As Integer: intNumOf715 = 0
Dim intNumOf1100 As Integer: intNumOf1100 = 0
For intCounterTotal = 1 To UBound(vTemp, 1) - 1
If CStr(Right(vTemp(intCounterTotal, 1), 4)) = CStr(strUID) Then
For intCounterRelevant = intCounterTotal + 1 To UBound(vTemp, 1) - 1
If Trim(vTemp(intCounterRelevant, 1)) = vbNullString Then GoTo FinishComputation
If InStr(vTemp(intCounterRelevant, 1), "715WAC") > 0 Then intNumOf715 = intNumOf715 + 1
If InStr(vTemp(intCounterRelevant, 1), "1100WAC") > 0 Then intNumOf1100 = intNumOf1100 + 1
Next intCounterRelevant
End If
Next intCounterTotal
FinishComputation:
CalculatePS = "Number of 715s: " & intNumOf715 & "; Number of 1100s: " & intNumOf1100
End Function
测试数据:
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-u1234
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