Altering input cell value and retrieve output cell value - excel

Sorry if this is a super basic VBA question but I am very new to Excel. But suppose I have an input cell E6, where I can just put any number into it. I also have a cell S12, and its formula is defined in the worksheet and is complex (depends on ~15 other cell values, including E6). How can I write a VBA function that takes in one input X, put it into E6, and returns the value of S12?
I tried doing this
Public Function Neville(X As Double) As Double
Range("E6").Value = X
Neville = Range("S12").Value
End Function
but it is giving me value errors. I think it might be because changing the value of a cell in VBA does not make the value of other cells in the worksheet change?

Related

Sheet doesn't recalculate after picking from list including own function

I have workbook with user VBA function (returning name of the cell):
Function cellName()
cellName = ActiveCell.Offset(0, 0).Name.Name
End Function
I have a list dictList with 3 columns used as dictionary (cellNames; ENG equivalents; CZ equivalents)
I have a cell $P$1 including data validation that can contain EN/CZ value.
Each cell in the sheet that has specified name (=cellName) includes a function
=VLOOKUP(cellName();dictList;IF($P$1="CZ";2;3);FALSE)
Finally each named cell contains text in czech or english language based on its name and vlooked value in dictlist.
The problem occurs, when I switch in $P$1 from CZ to EN or vice versa.
The values with VLOOKUP formulas shows result #VALUE! until I press F2 and Enter on each cell.
I don't want to press F2+enter on each cell after switching the language.
Tank you for your help.
Karel
When I put the result of cellName() directly to vlookup, then it works as expected. When I put there back my function, then it returns the #VALUE! again.
Instead of ActiveCell, use Application.ThisCell:
Returns the cell in which the user-defined function is being called from as a Range object.
Public Function cellName() As String
cellName = Application.ThisCell.Name.Name
End Function

Prevent VBA UDF to get updated if row/col is inserted in excel worksheet

I have a function defined e.g.
Public Function calc_x(ByVal x As Integer)
...do some stuff
calc_x = x+x
End Function
This function gets called within the excel sheet from a cell lets say (A2), with a "pointer" to A1 which contains a value 20:
content cell A1: "20"
content cell A2: "=calc_c(A1)"
However, everytime I insert new rows or columns in excel (even after row A or after col 2) the function gets recalculated. Is there a way to prevent that?
You can use Application.Caller.Text*(1) to reference the cell's original output.
Next, you can put a boolean "On/Off Switch Cell" somewhere.
Your UDF logic will go like: "If the value of boolean cell is TRUE, then do calc, otherwise output CallerCell text"
Below example works and is the roughly the simplest form.
Function Test_Not_Calc(x, y, b)
Application.Volatile
If b.Value = True Then
Test_Not_Calc = x * y
Else
Test_Not_Calc = Application.Caller.Text
End If
End Function
*Reference(1) - S/O - excel vba preserve original value if UDF formula fails
In the Formulas ribbon, you can set calculation options to manual.
You can set this in VBA using Application.Calculation = xlManual.
That will keep the formula from calculating with every new addition to the sheet.

Sum of values when end of range is defined by Cell("address")

I want to sum the total value preceding a lookup value in a range with a defined starting point. can anyone clue me into why this formula wont work?
=SUM(E9:CELL("address",INDEX(E9:BN9,MATCH("130LSR",E9:BN9,0))))
The result from just the cell function works:
=CELL("address",INDEX(E9:BN9,MATCH("130LSR",E9:BN9,0)))
This returns $F$9 in my sheet which is correct. It also has the workbook and sheet references prior, is that my problem? is there a way to remove the book and sheet references? The sum function obviously works if I provide the address, but why can't i define the end of my range with the cell(address) formula?

excel return formula with relative cell

I have 2 worksheets in my excel, the first sheet allows me to select a calculation method from a drop down list and input the variable for the calculation (shown in green cell, the column in blue shows some constant number). The result entry will search for the corresponding calculation formula from my second worksheet (database), then paste the formula to the sheet 1, I need the formula to calculate using the cells in sheet 1 instead of cells in my database.
currently I created an user defined function called Eval as below:
Function Eval(ref As String)
Eval = Application.Evaluate(ref)
End Function
by combining the Eval with vlookup :=Eval(VLOOKUP(A3,Database!A2:E10,5,FALSE)) I will get the result that the calculation equation uses the cells from my database, how can I achieve the result which the formula takes cells in sheet 1 during calculation?
One simple way would be to use all formulas within one CHOOSE like this:
=IFERROR(CHOOSE(SUMPRODUCT(MATCH(A2,"Calculation "&ROW($1:$9),0)),B2*C2*D2,B2*C2-D2,B2+C2-D2,B2^2-C2+D2,B2*D2-C2^2,B2+C2*D2,B2*C2-C2*D2,B2-D2-C2*D2,C2-D2*B2),"")
Another would be to use the Application.Caller like:
Public Function eval(ref As String)
eval = Application.Caller.Parent.Evaluate(ref)
End Function
This ensures the use of of the parent of the caller (the sheet with the eval() formula) to be used as main-ref.
EDIT
Keep in mind that your "rows" are static in the formulas. Going for "Calculation 4" will use row 5 (and not the row of your formula from the first sheet). For this you could use something like:
=Eval(SUBSTITUTE(VLOOKUP(A3,Database!A2:E10,5,FALSE),"##",ROW()))
While all row-numbers should be changed to ## (or whatever unique identifier you like). Then "Calculation 1" would look like: =B##*C##*D##
If you still have any questions, just ask :)

How to find the maximum value in entire Excel workbook

I have an Excel workbook with many sheets (today, about thirty, and growing every day). Each worksheet is named for the date of the data it contains (e.g., 02-10, 02-11, etc.). In cell A2 of each worksheet is a number, and I want to write a formula that looks at cell A2 in every worksheet, and returns the largest one.
I created a list of the current worksheets and named it DayWorksheets, and I attempted to get the value using the formula
{=MAX(INDEX(INDIRECT("'"&DayWorksheets&"'!A2"),0))} (brackets shown to denote array formula).
But the result every time is "10," which is the number in cell A2 of the first worksheet in the named range (and it's not the largest). What am I doing wrong here? I've seen a few other formulas around, the likes of
=VLOOKUP(C1,INDIRECT("Sheet"&MATCH(TRUE,COUNTIF(INDIRECT("Sheet"&ROW(INDIRECT("1:10"))&"!A2:A100"),B1)>0,0)&"!A2:B100"),2,0),
but I don't quite understand the purpose of the countif function in there. Any help would be appreciated.
You can use formula with 3D-reference like this:
=MAX(Sheet1:Sheet30!A2)
Click the cell where you want to enter the function.
Type = (equal sign), enter the name of the function (MAX), and then type
an opening parenthesis.
Click the tab for the first worksheet that you want to reference.
Hold down SHIFT and click the tab for the last worksheet that you
want to reference.
Select the cell or range of cells that you want to reference (A2 in your case).
Complete the formula (add closing parenthesis), and then press ENTER.

Resources