Excel: left of a reference - excel

The cell C4 points to cell E4 (but in my real workbook E4 is another sheet). I want cell C5 to point to the right of whatever cell C4 points to. So I only have to change C4 pointer to make C5 change as well - always to the cell to the right of whatever C4 points to. How do I do this?

Offset() and Indirect() work perfectly together like this, if you add an option to read the formula from a VBA custom function:
=OFFSET(INDIRECT(CellFormula(C4));0;1)
Option Explicit
Public Function CellFormula(Rng As Range) As String
CellFormula = Right(Rng.Formula, Len(Rng.Formula) - 1)
End Function
You do not need the custom function, if you are using Excel 2013. Then FormulaText does exactly the same. Just make sure to eliminate the equal (=) sign on the left from FormulaText, if you are using it.

if VBA is an Option
Range("c5").Value = Range("c4").Precedents.Offset(0, 1)

Related

Using Excel Formula in VBA code with variable ranges

I'm currently having a problem finding a way to vba an excel formula for all my columns in a worksheet. The number of rows in this worksheet is set to change from use to use hence it has to have a variable range.
The table currently looks like this:
I've got the value for noOfRowsOfData using:
Worksheets("Posttrans").Range("A1").Select
noOfRowsInPosttrans = Selection.CurrentRegion.Rows.Count - 2
The formula I am trying to put at the bottom of the table is:
=IF(noOfRowsInPosttrans=COUNTIF(A3:A688,A2),TRUE,FALSE)
Where 688 is set to change to noOfRowsInPosttrans
This will keep it dynamic. I'm just wondering how I would go about inserting the formulas displayed.
Any help would be great!
Thanks,
PineappleBeans
A6 = "=IF(COUNTIF(A$3:A5,A$2)=(CELL("row",A6)-3),TRUE,FALSE)"
Copy A6 to B6
EDIT: getting kind of crazy, but to show the formula:
Function function1(zcell As Range)
function1 = zcell.Formula
End Function
Then A8 = "=function1(A6)" and B9 = "=function1(B6)" gives

Excel - sum cell values divided by "plus"

I have a cell (A1 which have the text "3+2". On the next cell I need to put a formula which is "=A1+1".
How can I tell excel that it has to do the sum on A1, so he can sum another values?
Try to use EVALUATE function. It is best way to do it without VBA, however needs simple trick:
While being in cell B1 add to name manager range "Result" with formula
=EVALUATE($A1)
Than just place in B1 formula
=Result
For more comprehensive description regarding EVALUATE function please refer to this link. Additionaly please notice that EVALUATE function can have different name in your local language.
Btw. similar topic was discussed here
If A1 contains something like
number+number
then in B1 enter:
=LEFT(A1,FIND("+",A1)-1)+MID(A1,FIND("+",A1)+1,999)+1
If A1 contains an arbitrary numeric expression as text, you would use a VBA UDF.
EDIT#1:
If you have a list of values separated by the + sign, then you can use this array formula:
=SUM((TRIM(MID(SUBSTITUTE(A1,"+",REPT(" ",255)),1+(ROW(A1:A999)-1)*255,255)) & "0")/10)+1
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar:
Assuming you have your x+xxx+xxxxxxxxxxxx value in cell A1, consider the following formula below:
=SUM(0+TRIM(MID(SUBSTITUTE("+"&A1,"+",REPT(" ",99)),ROW(INDEX(A:A,1):INDEX(A:A,(1+LEN(A1)-LEN(SUBSTITUTE(A1,"+","")))))*99,99)))
When you return the formula, be sure to press CONTROL+SHIFT+ENTER. You can then just drag it down for your other additions.
You need to create a VB Macro:
Open the Tools>Macro>Visual Basic Editor
Right click to Create a new Module (important!)
Double click on the just created new module and paste this code
Public Function EvaluateString(strTextString As String)
Application.Volatile
EvaluateString = Evaluate(strTextString)
End Function
Close the VB editor window
Now you can use =EvaluateString(A1) to get the corresponding value of A1.

Calling the same cell of different worksheets in EXCEL

I have a drop down list (with name of sheets) and based on that value, let's say that I select the Sheet4 as in the image, I want to bring to another sheet the value of that selection, let's say on the cell B8.
I know that this works:
=IF(B1="Sheet1", Sheet1!B8, IF(B1="Sheet2", Sheet2!B8, Sheet3!B8))
That's for just 3 sheets but is there a nicer or more efficient way to do this?
This is in general how all the sheets look like:
Use INDIRECT to construct a valid worksheet and cell reference from text-that-looks-like-a-worksheet-and-cell-reference 1
The indirect takes a string and turns it into a valid reference.
=INDIRECT("'" & B1 & "'!B8")
So in the case above it would create a string "'Sheet4!B8". Then the Indirect will turn it into a valid cell reference.
As Jeeped also pointed out in the comments The B8 reference since it is literal text it will not change if the formula is copied or dragged to another cell.
The B1 which is a cell reference and is relative will change as it is copied or dragged to different cells.
1 as per #Jeeped comment

Dynamic Excel Subtraction

Say I've got cells A1-A10 populated with numbers. My initial formula in cell B1 is =A1-A6. However, I'd like to strike-out cell A3 (keeping the contents visible underneath the strike-out if possible), and I'd like the formula in B1 to recognise that change, and then automatically adjust itself to =A1-A7 (the idea being that I'd like A1 subtracted by the number in the cell 5 "non-struck out" cells below it). And then if I strike out cell A5 I'd like the formula to adjust itself to =A1-A8 and so on. Does anyone know how to do this?
(EDIT#1: misread the input, sorry)
A bit straightforward, but will do the job: type =A1-INDIRECT("A"&SMALL(IF(A:A<>"",ROW(A:A),""),6)) and press CTRL+SHIFT+ENTER instead of usual ENTER - this will define an ARRAY formula and will result in {} brackets around it (but do NOT type them manually!).
To speed up calculation you may replace A:A to any limited range.
Sample file (resulting formula is yellow-marked): https://www.dropbox.com/s/sy7zkg71xtfgib9/Subtract5th.xlsx
(EDIT#2: misread the "strike-out", sorry)
Font styles (as well as similar cell properties) may NOT be read by default Excel functions, that's why you need to add UDF called StrikeOut:
Press ALT-F11 - thiss will open VBA editor.
Insert new module: Insert > Module.
Paste the code to added module:
Function StrikeOut(R As Range) As Long
Dim c As Range
StrikeOut = 0
For Each c In R.Cells
If c.Font.Strikethrough = True Then StrikeOut = StrikeOut + 1
Next
End Function
Add the formula to B1: =A1-INDIRECT("A"&(6+StrikeOut(A2:A10)))
Set strikethrough font to any cells in A1:A10.
Unfortunately, cell format change does NOT trigger any change event, so you need either press F9 or change any cell value on the sheet to recalculate and therefore update result in B1.
Sample file is shared: https://www.dropbox.com/s/n9o7tn3ks3x8nza/StrikeOut.xlsm
P.S. at least for me that was extremely useful)))

Excel formula to show linked cell ID

In an excel cell, I've placed a simple formula
=C4
The cell typically displays the value of cell C4, but instead I want to see the linked cell ID instead, which in this case is "C4".
Is there a formula to show me this? like:
=SHOWCELL(C4)
The reason I need this instead of simply typing the value of "C4" into the cell, is so Excel will maintain the link to the correct cell even if rows are inserted/deleted, AND show me which cell is linked.
You should be able to use the Cell function.
In Excel, the Cell function can be used to retrieve information about a cell. This can include contents, formatting, size, etc.
=Cell("address", C4)
This displays $C$4.
When inserting a row before C4, it is changed to $C$5.
In case you do not want the $ signs, one way would be the Substitute function:
=Substitute( Cell("address", C4), "$", "" )
You can create your own User Defined Function to achieve this. I call it "CellReference".
Usage:
=CellReference(B6)
displays "B6"
To use it, launch VBA, insert a module, and then copy the below into the module:
Function CellReference(cell As range) As String
CellReference = cell.Address(0, 0, xlA1)
End Function

Resources