我想讓用戶更容易使用Excel表格而不必隨時修改繁重的表達式需要改變。我將函數的結果動態地放入單元格中。我得到的一切運作如何,除了:我用戶定義的VBA函數正在更新所有使用它的單元格,但我也不想要它
如果我去另一張表,並使用公式,它會返回正確的結果;但是,當返回到已經使用的另一張表單時,該表單將顯示最新的重新發送結果,而不再是其自己的傳遞變量實例。這些表單也與儀表板表格綁定,因此我需要確保如果計算一張表單,則不會篡改其他表單。我不知道該怎麼說這個問題,所以如果有我沒有使用的命名法,或者過去是否回答了這個問題,請告訴我,我會將其解決。
'------------------- 'getScore ' This function is called from a cell and is passed an intager. ' The integer represents the section that it is being called from. ' There is also the sheet title that is passed thrugh to the range. '------------------- Function getScore(section As Integer, sheetTitle As String) Application.Volatile Dim rngSt As Integer Dim rngEnd As Integer rngSt = getRange(section, sheetTitle, 1) 'Gets start range for formula rngEnd = getRange(section, sheetTitle, 2) 'Gets end range for formula Dim Formula As String 'Broken into seperate concatinated lines for readablility '-(COUNTBLANK(H" & rngSt & ":H" & rngEnd & "))," ' This section uses nested if statements to acrue the score through each level. Formula = "=IF(SUM(D" & rngSt & ":D" & rngEnd & ")= nonBlank(D" & rngSt & ":D" & rngEnd & ")," Formula = Formula & "IF(SUM(F" & rngSt & ":F" & rngEnd & ")= nonBlank(F" & rngSt & ":F" & rngEnd & ")," Formula = Formula & "IF(SUM(H" & rngSt & ":H" & rngEnd & ")= nonBlank(H" & rngSt & ":H" & rngEnd & ")," Formula = Formula & "IF(SUM(J" & rngSt & ":J" & rngEnd & ")= nonBlank(J" & rngSt & ":J" & rngEnd & ")," Formula = Formula & "IF(SUM(L" & rngSt & ":L" & rngEnd & ")= nonBlank(L" & rngSt & ":L" & rngEnd & "),5,4),3),2),1), 0)" getScore = Eval(Formula) 'Evaluates formula and returns a score of 0-5. End Function
這裏是getRange溫控功能
Function getRange(section As Integer, sheetName As String, rangePoint As Integer) Application.Volatile Dim FindRow As Range Dim ws As Worksheet Dim wb As Workbook Set wb = ActiveWorkbook If section = 1 Then If rangePoint = 1 Then With wb.Sheets(sheetName) Set FindRow = .Range("C9:C9") End With getRange = FindRow.Row End If If rangePoint = 2 Then With wb.Sheets(sheetName) Set FindRow = .Range("C:C").Find(What:="rngEnd", LookIn:=xlValues) End With getRange = FindRow.Row - 1 End If End IF End Function
這裏是我的評估和演示機能的研究
Function Eval(Ref As String) Application.Volatile Eval = Evaluate(Ref) End Function
非空溫控功能
Function nonBlank(r As Range) As Long 'Counts and returns the number of non blank cells found in given range. Application.Volatile nonBlank = r.Cells.Count - WorksheetFunction.CountBlank(r) End Function
由於這是一個用戶定義函數(UDF),您不必使用'Formula'。您的所有邏輯都可以簡單地放在VBA中,以確定結果傳遞給單元格。 – PeterT
這是因爲我有這個問題的公式方法嗎? –
你可以發佈getRange函數嗎? –