VBA - Find value from range of formulas - vba

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:=Shee‌​ts("Domestic").Range‌​("A" & i).Value, LookIn:=xlFormulas)

Related

Dynamic Array Formula using VBA

the problem I am trying to figure out is a bit complicated and detailed but I will try to simplify it with an example.
I am looking to use vba to change the array formula with a conditional statement. An example of what the code would look like is below
If (InStr(1, Range("E" & i).Value, "X")) > 0 Then
With outputSheet
Range("I" & i).FormulaArray = Sumproduct(('Sheet1'!$A$57:$A$104=$B2)*('Sheet1'!$E$56:$AZ$56=$H2),'Sheet1'!$E$57:$AZ$104)
End With
Else [Same code as above for Array formula]
End If
Initially I was using the following code to copy the Array formula down however using that I cannot use a conditional statement where the formula is dynamic according to cells.
Range("G2").AutoFill .Range("G2:G" & OutputLastRow)
If however, I can somehow make the $B2 into B&i then that would do the trick for me.
On just a highlevel, the idea was that the output cell would look at some conditions in the same row and different columns and change array formula according to the conditions. It would look at it for each cell and move down until it hits the last row
Any suggestions on how this can be tackled. I hope the problem is clear. Thanks so much!!

VBA Error method of '_default' if object 'range' failed when inserting formula into cell

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...

Syntax error for INDEX formula using Macro

I'm getting a syntax error when I try to insert a formula in a cell using Macro. I can't seem to figure out what am I doing wrong? The formula works fine when I manually enter it. Here's my code:
ws3.Range("F2:F" & lastRow3).Formula = "=INDEX($L$1:INDEX(L:L,MATCH("ZZZ",D:D)),AGGREGATE(15,6,ROW($K$1:INDEX(K:K,MATCH("ZZZ",D:D)))/($K$1:INDEX(K:K,MATCH("ZZZ",D:D))=D2),COUNTIF($D$1:$D2,D2)))"
ws3 is the worksheet, and lastRow3 is just to autofill the contents by comparing to a column to the left. Any ideas why this is giving an error?
FYI: This formula is finding values in one column from another column and giving the adjacent entries.
When filling a full range it is best to use R1C1 format.
Also with using vba to set the last row there is no reason for all the INDEX(L:L,MATCH("ZZZ",D:D)) to find the last row, since the code itself can set it directly.
Use this:
ws3.Range("F2:F" & lastRow3).FormulaR1C1 = _
"=INDEX(R1C12:R" & lastrow & "C12,AGGREGATE(15,6,ROW(R1C11:R" & lastrow & "C11)/(R1C11:R" & lastrow & "C11=RC[-2]),COUNTIF(R1C4:RC4,RC[-2])))"

Writing formula into an Excel Range with Option Strict On

Is it possible to write formulas across a range in Excel from VB.Net? I'm using a String array to hold a list of formulas that I would like to apply to an Excel range, instead of looping through and writing them one at a time.
This line is what I am attempting to use to write to a range:
xlWorkSheet.Range("AF" & intCurrentRow.ToString & ":AG" & intCurrentRow.ToString).Formula = formulas
The formula is being written to Excel, but Excel is actually displaying the formula, instead of the calculated value. If I write each formula to each cell like this:
xlWorkSheet.Range("AF" & intCurrentRow.ToString).Formula = formulas(0)
xlWorkSheet.Range("AG" & intCurrentRow.ToString).Formula = formulas(1)
It works perfectly fine and Excel displays the calculated values as it should. Almost seems like I'm missing a step but I haven't been able to find anything in my research.
As soon as I hit post I figured out the problem. Instead of using the .Formula property of an Excel.Range, you have to use the .FormulaArray property instead.
xlWorkSheet.Range("AF" & intCurrentRow.ToString & ":AG" & intCurrentRow.ToString).FormulaArray = formulas

VBA VLOOKUP Convert to Values Gives #N/A

I'm having some trouble with VLOOKUP in my VBA. Here's an example of the code I'm using:
Sub Macro15()
'
' Macro15 Macro
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range("B1:B" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-1],'https://internal_sharepoint_address
/[Vendor_Information.xlsx]Sheet1'!R3C3:R150C18,4,FALSE)"
Range("C1:C" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-2],'https://internal_sharepoint_address
/[Vendor_Information.xlsx]Sheet1'!R3C3:R150C18,5,FALSE)"
With Range("B1:C" & LR)
.Value = .Value
End With
End Sub
The problem is that the values in Columns B & C (the VLOOKUP formulas) return a value of #N/A.
However, if I stop the code before converting the formula to values (the "With Range("B1:C" & LR)" line), the VLOOKUP formula returns the correct values.
Also strange - if I clear the contents of Columns B & C and re-run the above code, the values return fine. If I try to add a second cycle to the VBA, however, it does NOT work.
Any wisdom that anyone can provide would be a huge help. I've been stuck on this for a long time, and I'm just at my wit's end.
Thanks all,
David
You'll probably need to add in a step that runs a calculation cycle before you try to replace with the value:
Application.Calculate
Edit from comment: I would imagine that retrieving lookup data from a linked workbook on a Sharepoint site would take awhile. Maybe add some delay loops? Can you make two separate macros (one ending with the formulas, and a second one starting at the Paste Values), and run them separately with a pause in between?