簡體   English   中英

excel vba應用程序的簡寫。

[英]shorthand for excel vba application.worksheetfunction

我想要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

with語句在這里很理想,例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM