Write formula in VBA Excel - excel

I have troubles with writing formula in Excel VBA.
Sub Macro()
valueA1 = Range("A1").Value
Range("C1").Formula = "=RC[-1]*" & valueA1
End Sub
At the end I want formula in cell C1 to be written as =B1*0,5, if value in B1 is 0,5.
Thaks for the help!

Excel doesn't like foreign languages. You will need to use FormulaR1C1Local:
Range("C1").FormulaR1C1Local = "=RC[-1]+" & valueA1
or maybe
Range("C1").FormulaR1C1Local = "=RC[-1]*" & valueA1
if you are trying to multiply B1*A1. (Your question says multiply, your code says add.)
That should cause it to accept "0,5" as a valid number.

Related

Extract current cell address

I need to extract the current cell in VBA without passing it in parameter of the function.
I mean, for example, if the formula is in cell B35, I want to extract the address (line = 35, column = 2), in the VBA code of the formula.
I thought the active cell would do it, but no, it extracts the address of the cell where the cursor is.
Do you know how I could do it?
Thanks in advance for help.
I think you mean Application.Caller which is the object that calls VBA. It can be used inside a function to return the cell calling the UDF, not the ActiveCell
Application.Caller property (Excel)
An easy example would be this:
Public Function ThisCell() As String
ThisCell = "ROW:" & Application.Caller.Row & " - COLUMN:" & Application.Caller.Column
End Function
If you type now in any cell =ThisCell() it will return its row and column:
Notice the output is different in all of them, even if they use the same UDF with no arguments.
A similar one to Foxfire And Burns And Burns' answer, but no vba needed.
Apply this formula, then copy it and paste as values.
="line = "&ROW()& " column= "&COLUMN()
Try below codes
Sub ExAddress()
MsgBox "Row: " & ActiveCell.Row & vbNewLine & _
"Column: " & ActiveCell.Column
End Sub

Evaluate formula not working in VBA

What is the wrong with the following formula ?
matchformula = "{=MATCH(1, (G12= G:G) , 0)}"
x = MySheet.Evaluate(matchformula)
Whereas the code below yields a correct result.
matchformula = "=MATCH(G12, G:G , 0)"
x = MySheet.Evaluate(matchformula)
VBA should handle this automatically. Consider this array formula case:
In VBA the equivalent code gives the same results:
Sub qwerty()
MsgBox Evaluate("MIN(IF(A1:A3>2,A1:A3))")
End Sub
There is no need to tell VBA that the formula should be treated as an array formula.

Reference another workbook with dynamic worksheet name

I want to reference another workbook and a specific worksheet which is dependent on some cell values.
This equation works
='S:\Down Time[11-Nov_2013_Downtime Tracker.xls]30'!$F$12
but the values 'Nov' and '30' are dynamic-- they depend on the cell values of D13 and E13, respectively.
How can I modify the equation?
Thank you
If you want to do this only by excel formulas, you could modify you formula like this:
=INDIRECT("'S:\Down Time[11-" & A1 & "_2013_Downtime Tracker.xls]" & A2 & "'!$F$12)
In this example it is assumed that the nov-value is in cell A1 and the 30-value is in cell A2. This formula, however, will only work if the referenced workbooks are open. Otherwise the formula would return a #REF! error. Only alternative would be to use VBA.
A little bit late, but here it goes in case someone else is looking for it.
Change the ranges to accommodate your needs.
With VBA:
Sub CallOtherWB()
Dim ThisMonth As String
Dim TheOtherVar As String
ThisMonth = Range("D13").Value
TheOtherVar = Range("E13").Value
With Range("A1")
.Formula = "'S:\Down Time[11-" & ThisMonth & "_2013_Downtime Tracker.xls]" & TheOtherVar & "'!$F$12"
End With
End Sub

Vba Excel Formula for following condition(Circular Referencing)

Hi, i tried to put a formula like ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",RC,"""")" If the country is not India then Check1, Check2 and Check3 should be empty otherwise they should display their own value. when i tried to put that formula the excel has given me circular referencing warning. I just want that formula. Any help would be appreciated greatly.
When a formula refers back to its own cell, either directly or indirectly, it creates a circular reference.
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",RC,"""")"
You are writing the formula in activecell and the formula says if RC[-1]="India"
and if its true RC which is same as activecell. Hence you are getting the circular reference error.
But if you put this formula in Column E as below it would work.
Range("E2:E" & lastRow).FormulaR1C1 = "=IF(RC[-4]=""India"",RC[-3],"""")"
Alternatively below is simple VBA code.
Sub sample()
Dim lastRow As Long
lastRow = Range("A65000").End(xlUp).Row
For i = 2 To lastRow
If (InStr(1, Cells(i, 1), "India") <= 0) Then
Range("B" & i & ":D" & i).Clear
End If
Next
End Sub
You can't use RC as a result only as this is referencing the current formula.
Option 1: You need to have another cell to be the validated cell, e.g.the following will create a cell to the left one of the current cell:
ActiveCell.Offset(0,1).FormulaR1C1 = "=IF(RC[-2]=""India"",RC[-1],"""")"
Option 2 after comment: If your line of code is only going to clear the current cells value if the left cell is not India then use this:
If ActiveCell.Offset(0,-1).Value <> "India" Then ActiveCell.Value = ""
Option 3: If your default value in RC has to stay but a formula is needed to override it if the value of RC[-1] becomes a not equal to India you must hard code your value in the formula like so:
ActiveCell.FormulaR1C1 = "=IF(RC[-1]=""India"",""" & ActiveCell.Value & ""","""")"

Inserting value into Excel cell instead of formula

I have a VBA script that inserts long strings into Excel cells. In some cases, the string begins with a =. It seems like Excel interprets this as a formula and I get an Out of Memory error due to the memory limitations of formulas.
How do I tell Excel that I am writing a value, not a formula? Currently, I am doing this:
ws.Range("A" & row) = Mid(xml, first, CHUNK_SIZE)
I have tried the following code, but it doesn't work...
ws.Range(...).Value = ....
Append a ' before the = sign:
Sub Test()
'This returns 30 in cell Al
Range("A1").Value = "=SUM(10,10,10)"
'This shows formula as text in cell A2
Range("A2").Value = "'" & "=SUM(10,10,10)"
End Sub
Add an apostrophe ' to the beginning of the string (that excel thinks is a formula) and excel should interpret it as a string instead of a formula.

Resources