Simple VB ststment deconstruction - excel

Could somebody please explain this simple vba statment to me? I just want to know what each part is referring to, and basically what this statement is accomplishing within my workbook. Thank you
ActiveCell.FormulaR1C1 = "='Bren Template'!R[-3]C[-6]"

This is a cell reference. In the ActiveCell (the one chosen), it will put the formula ='Bren Template'!R[-3]C[-6]. The formula breakdown is "Bren Template" is a reference to a sheet with that name. The R[-3] refers to three rows ABOVE the active cell. The c[-6] refers to three columns to the LEFT of the active cell.
So, if the active cell is H5 that formula will read ='Bren Template'!B2
If your active cell is I6, then the formula will read ='Bren Template'!C3
Note: The r[-3] and c[-6] will be "translated" from R1C1 style (i.e. Row 1 Column 1) when the formula is actually set in the cell.

Related

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

Excel VBA Add formula based on row

So I have that table above, I use Excel VBA to add new prices then add the formula to Decision column.
As you can see, cell B2 formula should be =IF($A2>50000,"Ignore","Buy") and cell B3 formula should be =IF($A3>50000,"Ignore","Buy") so the formula in B2 refers to the value in A2, this is the same for B3 to A3 and so on. I use the VBA below to add the same formula to blank cells. Yes, there will be blank decision cells and they need formula. I must NOT use autofill from top to bottom. I tried using below (LastRow is the usedrange.row):
Sheet1.Range("B2:B" & LastRow).SpecialCells(xlCellTypeBlanks). _
Formula = "=IF($A2>50000,""Ignore"",""Buy"")"
The problem with that VBA is even in cell B5 the formula is =IF($A2>50000,""Ignore"",""Buy"") when it should be =IF($A5>50000,""Ignore"",""Buy"") (should be $A5 instead of $A2). What am I doing wrong?
With SpecialCells(xlCellTypeBlanks) you will probably get a non continuous range. With this the auto fill process will not work with A1 formulas. But with R1C1formulas it will.
Use:
.Range("B2:B" & lastrow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=IF(RC1>50000,""Ignore"",""Buy"")"
RC1 means the Row you are currently in but always fix Column 1.
For R1C1 references see https://support.office.com/en-us/article/Overview-of-formulas-7abfda78-eff3-4cc6-b4a7-6350d512d2dc?CorrelationId=2bedf5ef-a3b7-4a82-9b12-6ee86b494ae9&ui=en-US&rs=en-US&ad=US#bmusing_references_in_formulas. Scroll down to The R1C1 reference style.
You can paste the formula in all cells, considering you have the formula in cell "B2":
Range("B2").Copy
Range("B2:B" & LastRow).PasteSpecial xlPasteFormulas
edit for more detail: You can use the R1C1 reference style, more importantly, R[1]C[1] notation. There is a caveat for different languages though, see the very end of the post. Examples:
R2C4 'row 2, column 4 so it's the cell D2 in A1-notation
R[2]C[4] 'the cell 2 to the right and 4 down from the current cell (where this reference is located)
R[2]C4 'the cell 2 to the right from the current cell in column 4 (D)
R[-2]C[-4] 'you can also give negative arguments, this is the cell 2 to the left and 4 up
R[2]C 'the same as R[2]C[0]
RC[4] 'the same as R[0]C[4]
R2C 'the same as R2C[0]
RC4 'the same as R[0]C4
R2 'row 2
C4 'column 4 (the same as D:D)
As you can see from the last three examples, the notations can't be mixed.
Now for your case:
If you want to have the following in cell Bx (replace x by any number)
"=IF($Ax>50000,""Ignore"",""Buy"")"
This would be the R1C1 formula
"=IF(RC1>50000,""Ignore"",""Buy"")"
or if it is more important that it is the column to the left:
"=IF(RC[-1]>50000,""Ignore"",""Buy"")"
The latter would be the like dropping the $ from the original formula.
Your second formula was
"=IFERROR(VLOOKUP(RC3,Database!$A:$F,3,FALSE),""Missing"")"
and Axel's answer
"=IFERROR(VLOOKUP(RC3,Database!C1:C6,3,FALSE),""Missing"")"
should be clear now.
If you don't want or can't use the formulaR1C1 property but still use the R1C1 style reference for a single cell, you can use the INDIRECT worksheet function. INDIRECT("R1C1",FALSE) is a reference to R1C1. The FALSE tells it to use R1C1 instead of A1 notation. It might behave slightly different than a simple reference if there is something other than numbers in the referenced cell.
I personally like the R1C1 notation better than the A1 notation mostly because it is easier to reference cells relative to the current position but also because it is easier to read for high column numbers and it's closer to the Cells(rowIndex,columnIndex) syntax.
One last thing: In other language versions of excel, R1C1 might be named differently. That doesn't affect the formula when you enter it via VBA (I think) but if you want to enter it from the worksheet, you need to keep that in mind. In German it's Z1S1 for example. This can also cause problems when opening the file with a different language version. If you used INDIRECT("R1C1",FALSE) in a formula, the INDIRECT and FALSE will be translated but the string will not so it will not work :( (The last part is from memory)

Excel: Adding a row disrupts formula

In a spreadsheet I use for cash management tracking, I have the following formula:
=IF(D176="Cash", F175+C176, IF(D176="Transfer", F175+C176, F175))
When I add a row, I use control+D to fill in the formula from the cell above (I'm using Excel for Mac 2011). This results in the correct formula as follows:
=IF(D177="Cash", F176+C177, IF(D177="Transfer", F176+C177, F176))
However, this has the effect of changing the formula in the cell in the row below:
=IF(D178="Cash", F176+C178, IF(D178="Transfer", F176+C178, F176))
Here you can see the rows for column F are not correct: F176 should be F177.
Can anyone offer any advice to ensure that when I insert a row the formula remains intact?
Thanks.
Replace all of the references to F175 in the original formula (the one if row 176) with INDEX(F:F, ROW()-1).
=IF(D176="Cash", INDEX(F:F, ROW()-1)+C176, IF(D176="Transfer", INDEX(F:F, ROW()-1)+C176, INDEX(F:F, ROW()-1)))
'or better as
=INDEX(F:F, ROW()-1)+(OR(D176={"Cash", "Transfer"}*C176)

referencing sheets by number instead of name in cells

Lets say
sheet3.name = "d"
Is there a way I could put in a cell on sheet2 the formula =sum(sheet3!b:b) where sheet3 is being substituted with the actual sheet3 name?
I can only get =sum('d'!b:b) to work so far.
I could use VBA for this probably but I'm curious how to do this in a cell so I don't have to run a macro each time.
If you can use a UDF User Defined Function that will return the sheet name
Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name
End Function
then a formula like
=SUM(INDIRECT(SHEETNAME(3) &"!B:B"))
will return the sum from column B on sheet 3.
SHEETNAME(number) returns the sheet name of the number which is index.
So Sheet(1) returns Sheet1, etc
Use below formula anywhere in the sheet to get the sheet name - the sheet must have a filename for this to work:
=REPLACE(CELL("filename"),1,FIND("]",CELL("filename")),"")
You can either reference that cell using Indirect:
=SUM(Indirect("'"&A1&"'!B:B"))
or, if you don't want to have a second cell, you can combine the two formulas into one:
=SUM(INDIRECT("'"&REPLACE(CELL("filename"),1,FIND("]",CELL("filename")),"")&"'!B:B"))
For anyone not concerned with the order of the sheets, the post by Biff here on mrexcel.com works well.
In Excel 2013, go to the Formulas tab in the ribbon and make a defined name:
Name: SheetNames
Refers to: =GET.WORKBOOK(1)&T(NOW())
Then use a formula like this example:
=INDIRECT("'"&INDEX(MID(SheetNames,FIND("]",SheetNames)+1,255),A3)&"'!A1")
where A3 refers to the index number in a cell in the current sheet, and A1 refers to the location of the value to be retrieved from the other sheet. I.e., in the current sheet, if A3 = 2, then the formula will point to cell A1 in the second sheet of the workbook. I just use a column of index numbers in my current sheet, then drag this formula down and it fills in values from all of my other sheets.
You will need to save as a macro-enabled file (.xlsm).
I'm not sure if this is a good idea but it's the first one I could think of.
I would add additional function to your VBA project which will return actual name of your Sheet3:
Function Sheet3Name()
Sheet3Name = Sheet3.Name
End Function
Next, when you create sum formula of column B:B in Excel cell you need to do it in this way:
=SUM(INDIRECT(Sheet3Name()&"!A:A"))

Write IF statement using FormulaR1C1 in VBA Excel

I have the following formula
=IF((GLOBAL_DATE-30)<G2,"1 Month",IF((GLOBAL_DATE-60)<G2,"2 Month",IF((GLOBAL_DATE-90)<G2,"3 Month","Older Than 3 Months")))
and I would like to write this into specific cells using the FormualR1C1 in VBA.
(the GLOBAL_DATE is a named cell on another sheet)
Thanks
Select the cell that has that formula. In the VBE, go to the Immediate Window and type
?Activecell.FormulaR1C1
and press enter. That will give you the R1C1 translation of your formula.
This is what I get using the Macro Recorder:
ActiveCell.FormulaR1C1 = _
"=IF((GLOBAL_DATE-30)<R[1]C[6],""1 Month"",IF((GLOBAL_DATE-60)<R[1]C[6],""2 Month"",IF((GLOBAL_DATE-90)<R[1]C[6],""3 Month"",""Older Than 3 Months"")))"
That is using relative cell addresses (R[1]C[6] is the cell one row below and 6 columns to the rigth from the ActiveCell. Alternatively, you can use absolute adresses by replacing R[1]C[6] by R2C7 (for row 2, column 7 = G2).
You can easily use VBA to translate those formulae that you entered in a sheet into a sytax that's suitable for VBA. I once wrote a sub for that purpose.

Resources