Paste text as formula - vba

I am creating a string that is a formula. Like in here (this is a simpler example)
If:
A1 is "Sum"
A2 is "D3"
Then B1 is =Concatenate("=",A1,"(",A2,")")
I want a VBA macro that takes the result of the formula in B1 and paste is as a formula in C1.
I need C1 to be the formula =SUM(D3)
I think it involves the PasteSpecial and evaluate, but I can't figure out how.
I don't want to use the INDIRECT function because I want to be able to fill more cells using than formula and the relative references inside.

with Activesheet
.Range("C1").Formula = .Range("B1").Value
End With

I don't know if you explicitly want a command macro, but this thing seems like a great use of a VBA UDF. If you create the UDF:
Function EvalFormula(f As String) As Variant
EvalFormula = Application.Evaluate(f)
End Function
Then in C1 you can call:
=EvalFormula(B1)
Writing this as a UDF is going to eliminate those unpleasant situations where you forgot to run your macro and now your sheet is all out of whack.

I used ActiveSheet.Cells(5, 3).Formula = ActiveSheet.Cells(5, 3).Value to use the text value as a formula

ActiveSheet.Cells(2, 2).Value = ActiveSheet.Cells(1, 2).Value
You can work the rest from that I'd assume :-)

Related

Copy Formula to a range

Would like to copy a formula from a cell to a range by using variables. It works fine with xlPasteFormulas but want to avoid it. This formula used wrong references so endresult is wrong:
With ActiveSheet
.Range(.Cells(rowStarWs1, colAct), .Cells(rowLast, colAct)).Formula = .Cells(rowSuch2Ws1, colAct).Formula
End With
Example:
Formula is saved in cell C3 (e.g. A3*B3)
Would like to paste the formula to range C10:C13. Range is given by my variables.
Result should look like below (Yellow cells)
I find using R1C1 notation very helpful when moving formulas around, especially in simple cases like the one you illustrated. More consideration is needed for more complex cases.
Sub f()
Dim f As String
f = Range("C3").FormulaR1C1
Range("C10:C13").FormulaR1C1 = f
End Sub
or
Range("C10:C13").FormulaR1C1 = Range("C3").FormulaR1C1
You might use FillDown for this purpose.
Cells(10, 3).Formula = Cells(3, 3).FormulaR1C1
Range("C10:C14").FillDown
FillDown copies the formula from the first cell of the defined range to all its cells. Therefore the first line of code fills that first cell. Assigning the R1C1 format to the Formula property changes the cells referenced in the formula without using the R1C1 format in the output.

Remove certain string from word Excel VBA

So I have a certain word within a word in a cell in excel that I'm trying to remove. For instance in one of the cells I have P/N279-82345 and I'm trying to get rid of the P/N in the front but keep the rest. I know there is the like function but is there a way to use the like function to remove just the P/N?
Thank You
Answer is
x = Replace(ws2.Cells(i, 1), "P/N", "")
Use the replace function.
Syntax can be found here: https://support.office.com/en-us/article/REPLACE-REPLACEB-functions-8d799074-2425-4a8a-84bc-82472868878a
if you are looking for a VBA macro function you can use something like:
Cells(1, 1).Value = Replace(Cells(1, 1).Value, "P/N", "")
where the Cells value is the range you want to search.
If you are looking for an excel worksheet function, you can use substitute:
=substitute(A1,"P/N","")
where A1 is the cell that contains the value you want to substitute.

VBA formula inside cell

I need to put this formula inside a column subset range
=VLOOKUP(SUBSTITUTE(M3;"#";"");$AG$413:$AK$821;5;FALSE)
I wrote this code:
XML.Range("V3:V411").Formula = "=VLookup(Substitute(M3, ""#"", """"), $AG$413:$AK$821, 5, False)"".Value = .Value"
but doesn't work and I get "Select method of range class failed" error
I recommend using the R1C1 format, more stable in macros.
Also, just use the macro recorder.
Don't pass .Value to Excel, it has no idea what that means. What was the plan with that?
.FormulaR1C1 = "=VLOOKUP(SUBSTITUTE(R[2]C[12],""#"",""""),R413C33:R821C37,5,FALSE)"
Or with .Formula:
.Formula = "=VLOOKUP(SUBSTITUTE(M3,""#"",""""),$AG$413:$AK$821,5,FALSE)"

Inserting VLOOKUP formula in a cell

I am trying to insert a VLOOKUP formula in a cell. I am not even trying to substitute parameters yet. I use this code:
myCell.Offset(0, 4).Value = "=VLOOKUP(A2;AnotherWorksheet!$A$1:$B$231;2;FALSE)"
And I am receiving an error: 1004
If I paste that exact formula in a cell, then the formula is recognized and accepted...
Any hint? thanks!
First, use , and not ; when separating the arguments. Second, you're looking for the .Formula property, not .Value. .FormulaR1C1 may work, but for exactness, just use A1-style formula writing for your VLOOKUP. See below.
Code:
Sub Voom()
Range("A1").FormulaR1C1 = "=VLOOKUP(A2,""AnotherWorksheet!$A$1:$B$231"",2,FALSE)"
End Sub
Result:
It shows #N/A because, of course, I have no data available. :)
For your exact code, just use:
myCell.Offset(0, 4).Formula = "=VLOOKUP(A2,""AnotherWorksheet!$A$1:$B$231"",2,FALSE)"
Hope this helps.
You need to use the Formula property.
For example:
myCell.Offset(0, 4).Formula = "=VLOOKUP(A2,""AnotherWorksheet!$A$1:$B$231"",2,FALSE)"
From MSDN, the Formula property and the FormulaR1C1 property documentation.

Add a cell formula in Excel via vba

I’m not an Excel or VBA expert but I want to insert this current excel formula into cell’s using VBA.
Current Excel formula:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
VBA formula :
ActiveSheet.Range("a" & ActiveSheet.Rows.Count).End(xlUp).Offset(2, 12).Value = "=IF(OR(ISNUM(R[-3]C[-9])=FALSE;ISNUM(R[0]C[-9])=FALSE);'';IF(R[0]C[-10]='Total';R[0]C[-9]-R[-3]C[-9];''))"
It doesn’t work… Someone can help me please?
Try using .formula = instead of .value = in your VBA code.
Setting the .value of a cell simply copies in whatever value you specify. In this case, your formula is simply converted to a string value.
Using the .formula property, you are actually specifying the formula that gets used to compute the value, which is what you are looking for.
Can I first suggest a simplification of your formula, from:
=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))
...to...
=IF(AND(C573="Total"; ISNUM(D570); ISNUM(D573)); D573-D570; "")
Then, I'd set a cell (the active cell in the example below) to use that formula using the VBA code:
ActiveCell.Formula = "=IF(...)"