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.
Related
I need to know an equivalent in VB code for:
'In Excel cell (for ex. xlSheet.Range("A1"))
{=Average(If(A1:A10="Text",B1:X10))} ' <---Equivalence of this written all in VB code, not in Excel
That is, calculate the average of an Excel matrix (for ex. B1 to X10 Excel range), with criteria, from VB.
Here:
Evaluate("=AVERAGE(IF(A1:A10=""test"",B1:U10))")
Don't forget to put double quotes after single quotes for text.
I would like to suggest this code for the issue:
Worksheets("Data1").Range("D2") = Application.WorksheetFunction.AverageIfs(Worksheets("Sheet1").Range("A2:A11, "E2<>0", B2:B11))
Other should be:
Suppose in A1 you have written this formula, so in VBA code you can use something like this:
=IF(WEEKDAY(A1,2)>5,"NO WEEKDAY","")
Range("b2").Formula = "=A1"
Please change cell address as you need.
What formula do you use to check if another cell has formula? For example, I have 2 columns, A has cells which contains either a formula or a value.
(Column A usually contains Formulas but other users try to change their values by directly typing and replacing the formula that was previously there)
In Column B I want to add a formula that will say "HasFormula" if the cell on Column A has formula and say "PlainValue" if it contains a value.
I'm thinking maybe using =ISNUMBER() but that may not be accurate.
I am using Excel 2010.
Excel actually has a builtin ISFORMULA() function.
Say A1 has a formula and you want to check that. In say B1, you can use:
=If(ISFORMULA(A1),"HasFormula","PlainValue")
Edit: Per your comment, you don't have ISFORMULA(). An alternative is to create a quick UDF, and use the custom function in the worksheet.
In a workbook module, put this code:
Function isFormula(ByVal target As Range) As Boolean
isFormula = target.hasFormula
End Function
Then you can call it like this: =isFormula(A1) and it will return TRUE if A1 has a formula.
If you can't use VBA, then you can use this formula:
=IF(ISERROR(FORMULATEXT(A1)),"PlainText","HasFormula")
The MrExcel website (link below) has this method which uses old code from Excel 4 (which is still present for backward compatibility)...
Define a NAME such as "CellToLeftHasFormula" and in the "refers to" box put
=GET.CELL(48,OFFSET(INDIRECT("RC",FALSE),0,-1))
Then in column B use the formula =CellToLeftHasFormula which will return TRUE if it has.
Be aware that this will mean your Excel will now contain a macro and so will need to be saved as such (xlsm). I use this in Excel 2010.
For full explanation (and other .CELL options, besides 48) see MrExcel link: https://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
You can use the Range.HasFormula property.
https://learn.microsoft.com/en-us/office/vba/api/excel.range.hasformula
EDIT:
Text and code from the above link:
"True if all cells in the range contain formulas; False if none of the cells in the range contains a formula; null otherwise. Read-only Variant. ..."
Worksheets("Sheet1").Activate
Set rr = Application.InputBox( _
prompt:="Select a range on this worksheet", _
Type:=8)
If rr.HasFormula = True Then
MsgBox "Every cell in the selection contains a formula"
End If
You can restrict the user by protecting the column A.
You can directly check if a cell contains a formula by using a shortcut Ctrl + `.
You can use vba and write a user defined function :
1. Press alt + F11
2. Insert module in workbook
3. Paste this code
Function IsFormula(cell_ref As Range)
IsFormula = cell_ref.HasFormula
End Function
4. Now, use Isformula in the cell wherever you want.
I am aware that in 2008 the method of exporting formula to excel was downgraded see here.
But I have seen a workaround for a static formula where the same formula is required (see Here and here). However I have the case where I need a relative formula when I export to excel. Is there a newer solution out there does anyone know?
This is my workings so far
The result I am after is cell (T,r3) = cell (R,r3)-cell(O,r3) and then cell (T,r4) = cell (R,r4)-cell(O,r4) and so on in excel I would expect to see
in cell T3 the formula =R3-O3
and cell T4 the formula =R4-O4
O!P!Q!R !S!T!
r3 1 5 N 5 4
r4 3 5 N 10 7
(Sorry I can not get the table to line up)
my ssrs expression is
="=R"+cstr(RowNumber("DataSet1")+2)+"-O"+cstr(RowNumber("DataSet1")+2)
where DataSet1 is what my ssrs tablix is based on and the +2 element is to offset to allow how the titles of the report and the table move the first row of data down the excel sheet.
The output is achieve in the cells when exported to excel is the formula as text in the cell content and if I click into the cell and press backspace and return the formula works. It appears that some white space is being exported or excel just treats every thing as text on the import routine
I can of course use and find and replace on this statement in excel
="=R"+cstr(RowNumber("DataSet1")+2)+"-O"+cstr(RowNumber("DataSet1")+2) as suggested but was trying to be more elegant
I do not suppose you can export some code to run in excel as vba to do the find and replace function at a button press for the user?
Has anyone any suggestions - thanks
Ian
Since this is not supported the workaround is tedious. For each cell that has a formula you can:
Select the cell
Click in the formula bar
Press Enter
This causes Excel to evaluate the string as a formula and replaces the text with the value it represents.
You can't export VBA from SSRS but you could paste in your own VBA after export and run it to accomplish the same steps.
I use SSIS to populate an Excel report template with data and formulas.
I had too many formulas to have to go into the worksheets and updated them manually.
In a script task, I use VB to calculate the formula and populate it in a sheet as well as underline the appropriate line:
ExcelFormula = "=SUM(" & SumColumn & "45:" & SumColumn & Row.ToString & ")"
Worksheet.Cells(Row, Column - 1).Formula = ExcelFormula
Worksheet.Cells(Row, Column - 1).BORDERS(9).Weight = 2
Worksheet.Cells(Row, Column).BORDERS(9).Weight = 2
It's a bit more work initially but once you get it set up to work with one cell, the rest are relatively easy.
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)
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.