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.
Related
I'm an Excel VBA newbie.
How to change the value of the specified cell via a user-defined function? What's wrong with this code:
Function Test(ByVal ACell As Range) As String
ACell.Value = "This text is set by a function"
Test := "Result"
End Function
My wish is ... when I type =Test(E6) in cell E1, Excel will display the specified text in E6.
YES, of course, it is possible.
Put this code in Module1 of VBA editor:
Function UDF_RectangleArea(A As Integer, B As Integer)
Evaluate "FireYourMacro(" & Application.Caller.Offset(0, 1).Address(False, False) & "," & A & "," & B & ")"
UDF_RectangleArea = "Hello world"
End Function
Private Sub FireYourMacro(ResultCell As Range, A As Integer, B As Integer)
ResultCell = A * B
End Sub
The result of this example UDF is returned in another, adjacent cell. The user defined function UDF_RectangleArea calculates the rectangle area based on its two parameters A and B and returns result in a cell to the right. You can easily modify this example function.
The limitation Microsoft imposed on function is bypassed by the use of VBA Evaluate function. Evaluate simply fires VBA macro from within UDF. The reference to the cell is passed by Application.Caller. Have fun!
UDF limitation documentation: https://support.microsoft.com/en-us/help/170787/description-of-limitations-of-custom-functions-in-excel
A VBA UDF can be used as an array function to return results to multiple adjacent cells.
Enter the formula into E1 and E2 and press Ctrl-Shift-Enter to create a multi-cell array formula. Your UDF would look something like this:
Public Function TestArray(rng As Range)
Dim Ansa(1 To 2, 1 To 1) As Variant
Ansa(1, 1) = "First answer"
Ansa(2, 1) = "Second answer"
TestArray = Ansa
End Function
Excel VBA will not allow a user-defined function to alter the value of another cell.
The only thing a UDF is allowed to do (with a few minor exceptions) is to return values to the cells it is called from.
Why not just typing you formula in E6 then ? That's the Excel logic: put your formula where you want the result to appear.
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.
I'm trying to input a formula in a cell after inserting a row. And VBA does not want that and returns an error.
I first copied the formula from the excel sheet, (with ";" as delimitors), and as I have seen on other threads, I replace those with ",".
I don't understand why I get the error.
Sub Borderx()
Dim nboc As Integer
Dim ipaste As Integer
nboc = Worksheets("BDD").Range("IQ2").Value
For ipaste = 1 To nboc - 1
Worksheets("Bordereaux").Range("B14").EntireRow.Insert
Worksheets("Bordereaux").Range("T14").Formula = "=IF(AND(J14="",E14=""),SUM(F14*F14*H14)/1000,IF(O14="",SUM((H14*F14*G14)+(M14*K14*L14))/1000,SUM((H14*F14*G14)+(M14*K14*L14)+(R14*P14*Q14))/1000))"
Next ipaste
End Sub
In this case, nboc = 2, and this is supposed to insert one row.
Before I added the "if" statement, it worked fine when it was just sum()
Change it like this:
Worksheets("Bordereaux").Range("T14").Formula = " =IF(AND(J14="""",E14=""""),SUM(F14*F14*H14)/1000,IF(O14="""",SUM((H14*F14*G14)+(M14*K14*L14))/1000,SUM((H14*F14*G14)+(M14*K14*L14)+(R14*P14*Q14))/1000))"
I am writing an IF statement that uses the IsEmpty function to determine if True or False.
I tried it both on a cell with a value (e.g., PRB2039) and on a blank cell to test my code, and the result is the same.
I removed formatting, and tried it on a new worksheet. I don't know what I'm doing wrong.
I prefer using
If Len(Trim(Cells(i, 1).Value))=0 Then Msgbox "Empty"
As suggested by #PatricK you may consider using ISBLANK function instead of IsEmpty function. ISBLANK function Returns TRUE if the value is blank
Using VBA
Sub test()
For i = 1 To 4
MsgBox Evaluate("isblank(" & Cells(i, 1).Address & ")")
Next
End Sub
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.