我想要一个excel vba application.worksheetfunction的简写。到目前为止它在下面的代码中运行良好,但现在我想评估一个函数,它类似于
=IFERROR(MAX(M$2:M$64)*BINOM.DIST($W2,COUNTIF(M$2:M$64,"<"&MAX(M$2:M$64))-COUNTIF(M$2:M$64,"<"&1),0.5,FALSE),"")
在我调用的每个函数前面使用application.worksheet会非常麻烦。请告诉我如何使用速记。
Sub valuesCalc()
Dim sumBinCenter As Double, lengthRows As Long
sumBinCenter = 0
lengthRows = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lengthRows
Cells(i, "G").Value = 0.25 + sumBinCenter 'List BinCenters
sumBinCenter = sumBinCenter + 0.5
Range("H" & i) = Application.WorksheetFunction.CountIf(Range("A2:A" & lengthRows), "<" & (Cells(i, "G").Value + 0.25)) - Application.WorksheetFunction.CountIf(Range("A2:A" & lengthRows), "<" & (Cells(i, "G").Value - 0.25))
Range("I" & i) = Application.WorksheetFunction.CountIf(Range("B2:B" & lengthRows), "<" & (Cells(i, "G").Value + 0.25)) - Application.WorksheetFunction.CountIf(Range("B2:B" & lengthRows), "<" & (Cells(i, "G").Value - 0.25))
Range("J" & i) = Application.WorksheetFunction.CountIf(Range("C2:C" & lengthRows), "<" & (Cells(i, "G").Value + 0.25)) - Application.WorksheetFunction.CountIf(Range("C2:C" & lengthRows), "<" & (Cells(i, "G").Value - 0.25))
Range("K" & i) = Application.WorksheetFunction.CountIf(Range("D2:D" & lengthRows), "<" & (Cells(i, "G").Value + 0.25)) - Application.WorksheetFunction.CountIf(Range("D2:D" & lengthRows), "<" & (Cells(i, "G").Value - 0.25))
Range("M" & i) = Application.WorksheetFunction.CountIf(Range("A2:A" & lengthRows), "<" & (Cells(i, "G").Value + 0.25))
Range("N" & i) = Application.WorksheetFunction.CountIf(Range("B2:B" & lengthRows), "<" & (Cells(i, "G").Value + 0.25))
Range("O" & i) = Application.WorksheetFunction.CountIf(Range("C2:C" & lengthRows), "<" & (Cells(i, "G").Value + 0.25))
Range("P" & i) = Application.WorksheetFunction.CountIf(Range("D2:D" & lengthRows), "<" & (Cells(i, "G").Value + 0.25))
Next i
Dim intCount As Double
intCount = -1
For i = 2 To lengthRows
Range("R" & i) = Cells(i, "M").Value * 100 / Application.WorksheetFunction.Max(Range("M2:M" & lengthRows))
Range("S" & i) = Cells(i, "N").Value * 100 / Application.WorksheetFunction.Max(Range("N2:N" & lengthRows))
Range("T" & i) = Cells(i, "O").Value * 100 / Application.WorksheetFunction.Max(Range("O2:O" & lengthRows))
Range("U" & i) = Cells(i, "P").Value * 100 / Application.WorksheetFunction.Max(Range("P2:P" & lengthRows))
Cells(i, "W").Value = 1 + intCount 'List intCounter
intCount = intCount + 1
Next i
End Sub
答案 0 :(得分:4)
带有声明的声明在这里是理想的 e.g。
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
所以在你的情况下
Range("M" & i) = Application.WorksheetFunction.CountIf(Range("A2:A" & lengthRows), "<" & (Cells(i, "G").Value + 0.25))
Range("N" & i) = Application.WorksheetFunction.CountIf(Range("B2:B" & lengthRows), "<" & (Cells(i, "G").Value + 0.25))
Range("O" & i) = Application.WorksheetFunction.CountIf(Range("C2:C" & lengthRows), "<" & (Cells(i, "G").Value + 0.25))
Range("P" & i) = Application.WorksheetFunction.CountIf(Range("D2:D" & lengthRows), "<" & (Cells(i, "G").Value + 0.25))
变为:
With Application.WorksheetFunction
Range("M" & i) = .CountIf(Range("A2:A" & lengthRows), "<" & (Cells(i, "G").value + 0.25))
Range("N" & i) = .CountIf(Range("B2:B" & lengthRows), "<" & (Cells(i, "G").value + 0.25))
Range("O" & i) = .CountIf(Range("C2:C" & lengthRows), "<" & (Cells(i, "G").value + 0.25))
Range("P" & i) = .CountIf(Range("D2:D" & lengthRows), "<" & (Cells(i, "G").value + 0.25))
End With
编辑(感谢@Wolfie):
您还可以定义WorksheetFunction
对象,并在With
块之外使用它。例如:
Dim WF As WorksheetFunction
Set WF = Application.WorksheetFunction
Range("M" & i) = WF.CountIf(Range("A2:A" & lengthRows), "<" & (Cells(i, "G").value + 0.25))
' < etc. >
附加请求,iferror用例:
Sub test()
With Application.WorksheetFunction
MsgBox (.IfError(ActiveCell.Value, "Error"))
End With
End Sub
其他请求,错误处理:
With Application.WorksheetFunction
On Error Resume Next
For i = 2 To lengthRows
Err.Clear
Range("M" & i) = maxfew * .BinomDist(Cells(i, "M").Value, numtrialsfew, 0.5, False)
If Err > 0 Then Range("M" & i) = ""
Next i
On Error GoTo 0
End With