I'm using the below code to do a fast vlookup, however I'm struggling when the code returns "#N/A" errors.
I've tried to copy and paste special in the hope that the "N/A" will be treated like regular text (then I can replace with any value I want), but that doesn't seem to work.
My code as follows:
lr1 = Worksheets("Risk Explorer greeks").Cells(Rows.Count, "I").End(xlUp).Row
Range("J2:J" & lr1).FormulaR1C1 = "=VLOOKUP(RC[-1], R1C1:R50000C1, 1, False)"
Would appreciate any help! Any ideas on how I can get rid of these "#N/A" errors using VBA?
Related
I'm still getting to grips with VBA and have an issue with something I'm trying to do and was hoping i can get some help. (sorry if it doesn't make much sense, I don't have the lingo).
This part of the code is where I need it to put a value in the last cell on col A from col I (row dependant of my variable).
Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Value = Range("I" & RowCount)
If i replace the variable "RowCount" with "1" it gives me what I want. I have used the same wording for "Range("I" & RowCount)" multiple times with in the macro but for some reason doesn't like it in this...
Any help?
I have this line of code that is supposed to find the same value in a range:
Set RowN = oldDashboard.Sheets("Dom").Range("F1:F1000").Find(this.Sheets("Domestic").Range("A" & i).Value)
It works fine when my range is composed of values but when the range is composed of formulas it does not work. I tried putting .Value after Range("F1: F1000") but it brings an error. I think the answer is fairly simple but I cannot figure it out.
Thanks
You are not using all arguments that .Find method offers.
It is possible to search by formula (xlFormulas) or values(xlValues)
Example:
Set RowN = oldDashboard.Sheets("Dom").Range("F1:F1000").Find(What:=Sheets("Domestic").Range("A" & i).Value, LookIn:=xlFormulas)
I am trying to use VBA to put a formula into a cell. This is the formula.
Range(Cells(2, 15)).FormulaR1C1 = "=SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7clrow,Classes!R[]C[-13]))"
The error is inside the quotes on the right. I've tried taking off range, using A1 notation, and everything else I can think of. I have several extremely similar lines before and after that all work fine, so I'm not quite sure what I'm missing. Thanks for any help.
EDIT: Here is the line several people have suggested:
Cells(2, 15).FormulaR1C1 = " =SUMPRODUCT(COUNTIF('All Failing Classes'!$G$2:$G$" & lrow & ",Classes!B2))"
or
Cells(2, 15).FormulaR1C1 = "=SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7C" & lrow & ",Classes!R[]C[-13]))"
Which is the same as far as I can tell.
While Mat was helping me I accidentally added a space in front of the = to create the line:
Cells(2, 15).FormulaR1C1 = " =SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7C" & lrow & ",Classes!R[]C[-13]))"
and that line goes in just fine? When I move to XL and take the space out, the formula works as intended. Does this help narrow down what I'm doing wrong?
Edit 2: Here is what I was doing wrong - I had redefined a variable before that line that referenced another sheet. When I did that the program apparently kept referencing that sheet, and gave me my issues. When I changed and added the sheet before the cells (sheets(blah).cellc(blah) then it worked. Thanks to everyone (especially Mat) who helped out.
Does =SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7clrow,Classes!R[]C[-13])) work as a formula?
If it doesn't work (and no, it doesn't), then Excel itself can't assign the broken formula to the cell you're typing it in - for the exact same reason, VBA won't be able to do it either.
So you mean lrow to be evaluated by VBA before it's inserted into the formula, right? For this to happen, VBA needs to be able to see that lrow variable. Being in a string literal, it doesn't, and it can't guess at your intentions either: as far as VBA is concerned, this is what's happening:
SomeObject.SomeProperty = "some string literal"
That's all.
You need to concatenate the variable's value into the string literal, the way you concatenate strings in VBA:
SomeObject.SomeProperty = "some " & someVariable & " string literal"
In other words make the right-hand side of the assignment a string-valued expression, not just a literal:
"=SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7c" & lrow & ",Classes!R[]C[-13]))"
Try this
Cells(2, 15).FormulaR1C1 = "=SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7c" & lrow & ",Classes!R[]C[-13]))"
I'm guessing that lrow is a variable...
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.
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(...)"