I am trying to make it easier for users to work with an excel sheet instead of having to modify an arduous expression anytime a change is needed. I am dynamically placing the function's result into a cell. I got everything functioning how it should except:
If I go to another sheet and use the formula, it will return the proper results; however, when returning to another sheet that was already using using it, that sheet will display the most resent results and no longer its own instance of passed variables. These sheets also tie into a dashboard sheet so I need to make sure that if I calculate one sheet, it doesn't tamper with the others. I wasn't sure how to word this issue, so if there is nomenclature in place that I am not using or if this has been answered in the past, let me know and I will close this out.
'------------------- '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
Here is the getRange fucntion
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
Here is my Eval fuction
Function Eval(Ref As String) Application.Volatile Eval = Evaluate(Ref) End Function
nonBlank fucntion
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