Error using averageifs in VBA - vba

My VBA code is like this:
Sheets("Result").Range("E" & i) = Application.WorksheetFunction.AverageIfs( _
Sheets("Data").Range("B:B"), _
Sheets("Data").Range("B:B"), _
">=" & Sheets("Result").Range("A" & i), _
Sheets("Data").Range("B:B"), _
"<" & Sheets("Result").Range("B" & i), _
Sheets("Data").Range("B:B"))
However, an error message showed "Unable to get the averageif property of the worksheetfunction class".
When I change the critiria value to be a constant, however, it can run correctly:
Sheets("Result").Range("E" & i) = Application.WorksheetFunction.AverageIfs( _
Sheets("data").Range("b:b"), _
Sheets("data").Range("b:b"), _
">=" & 0.06, _
Sheets("data").Range("b:b"), _
"<" & 0.07)
What's the problem here? And how can I correct it?
-I found the problem was that there is no value within the specified range. But when I tried to debug using the iferror() function, it still shows the same error message.

The error comes up when
there is no data in any one of the referenced ranges
none of the data in column B matches the criteria

Related

Compiling error when trying to access directory

The following string of code keeps returning the error expected name parameter and i can not figure out where the trouble is:
With ActiveSheet.Shapes.AddPicture(Filename:="C:\Users\" & user & _
"\Dropbox (Junior-Consult)\JC - Eksterne projekter\1 JC - Virksomheder\2017-18\Dansk Supermarked - Mapping af kvalitative kommentarer\4. Analyser\" & _
country(j) & "\Billeder\" & pic_pre(i) & ws_name(j) & ".jpg",
This code however works just fine:
"C:\Users\" & user & _
"\Dropbox (Junior-Consult)\JC - Eksterne projekter\1 JC - Virksomheder\2017-18\Dansk Supermarked - Mapping af kvalitative kommentarer\4. Analyser\" & _
country(i) & "\Sentiment\" & country(i) & "_sent_o_" & ws_name(j))
i cant figure out what part of my directory string is wrong?
Your directory string is probably alright. You didn't end the first statement the right way. Maybe because your original string was so long you didn't see it.
You can use multiple lines using _ like this:
With ActiveSheet.Shapes.AddPicture(Filename:="C:\Users\" & user & _
"\Dropbox (Junior-Consult)\JC - Eksterne projekter\1 JC - Virksomheder" & _
"\2017-18\Dansk Supermarked - Mapping af kvalitative kommentarer" & _
"\4. Analyser\" & _
country(j) & "\Billeder\" & pic_pre(i) & ws_name(j) & ".jpg")
Are we answering in the comments now? I missed that in the tutorial.

Runtime error 1004 Using Vlookup and Named ranges

I have tried both sets of code below and either get runtime error 1004 or no answer if if put in error handling.
res.Cells(rlr, 8) = Application.WorksheetFunction.VLookup( _
res.Cells(rlr, 6), Repetition, 2, False)
And
res.Cells(rlr, 8).FormulaR1C1 = Evaluate("VLOOKUP(" & _
rec.Cells(2, 16) & "," & Repetition & ",2,FALSE)")
Repetition is a dynamic named range, I have tried manually doing this and it works fine.
What am I doing wrong?
Try:
res.Cells(rlr, 8).Value = res.Evaluate("VLOOKUP(" & _
res.Cells(2, 16).Address & "," & Repetition.Address & ",2,FALSE)")
Assuming rec.Cells(2,16) was a typo and all ranges are on sheet res

Run-time error '1004' when applying formula to Range.FormulaR1C1 in VBA

I'm trying to apply a big nested formula to a range using the code below. Basically, if the value in cell A of the active row exists in the column A of another workbook and if the cell in column E of the active row is not empty, I want the active cell to display the cells to display the value of the equivalent cell in a separate workbook.
This needs to be applied to several worksheets so I'm using the variables lrow (which is an int with the last row of the active worksheet in workbook#1) and tlrow (which is an int equal to the last row of the active worksheet in workbook#2). When I step through the sub, these variables both return the numbers I would expect them to.
Likewise, this is inside of a for loop so I also use Worksheets(i).Name where I is an int.
When I run the code, I get the run-time error "'1004': Application-defined or object-defined error".
I'm assuming it's a syntax issue.
Code:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IF(ISERROR(VLOOKUP(RC1,'[temp.xlsx]" & _
Worksheets(i).Name & _
"'!A15:D" & tlrow & ",3,FALSE)),""0"",VLOOKUP(RC1,'[temp.xlsx]" & _
Worksheets(i).Name & "'!A15:D" & tlrow & ",3,FALSE))))"
Try using this:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IF(ISERROR(VLOOKUP(RC1," & _
Worksheets(i).Range("A1:D" & lrow).Address(ReferenceStyle:=xlR1C1, External:=True) & _
",3,FALSE)),""0"",VLOOKUP(RC1," & _
Worksheets(i)..Range("A1:D" & tlrow).Address(ReferenceStyle:=xlR1C1, External:=True) & _
",3,FALSE)))"
What version of Excel are you running? In more recent versions you can use the Iferror function in this formula to really chop down the size.
It would be something like this:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IFERROR(VLOOKUP(RC1," & " & Worksheets(i).Range("A1:D" & _
tlrow).Address(ReferenceStyle:=xlR1C1, External:=True) & ",3,0),""0"")"
Thanks for your help. I was able to resolve the problem by defining my vlookup range in a Range variable and then inputting the variable name in L42's equation in place of
worksheets(i).Range("A1:D" & lrow)
Really apprecaite the responses! Thanks again.

VBA SumProduct Runtime Error 13

As a part of a much longer code, I am trying to include a SumProduct
Dim SumPr as Variant
SumPr=Application.WorksheetFunction.SumProduct(((Workbooks(Source2).Sheets("Prices_EUR_Adj").Range("A:A")) = Range("A" & i)) * ((Workbooks(Source2).Sheets("Prices_EUR_Adj").Range("D:D")) = "PH") * (Workbooks(Source2).Sheets("Prices_EUR_Adj").Range(ColLtr & ":" & ColLtr)))
MsgBox SumPr
However, I keep getting Runtime Error 13 for some reason. Any idea what is wrong? Source2 is properly defined, and ColLtr is letter conversion of column number I got from match; it also works ok as checked by message box.
Try using the Evaluate function instead:
Dim SumPr As Variant
SumPr = Application.Evaluate("SUMPRODUCT(--(" & Workbooks(Source2).Sheets("Prices_EUR_Adj").Range("A:A").Address & "=" & Range("A" & i).Address & ")," & _
"--(" & Workbooks(Source2).Sheets("Prices_EUR_Adj").Range("D:D").Address & "=""PH"")," & _
Workbooks(Source2).Sheets("Prices_EUR_Adj").Range(ColLtr & ":" & ColLtr).Address & ")")
MsgBox SumPr
I'm sure the WorksheetFunction.SumProduct will also work, but I was able to get the Evaluate function working easier.

Insert formula from VBA

I'm attempting to have the following code insert the formula into the changed cell. I'm not receiving any errors but the code is not populating on the worksheet. Any insight into what I'm not doing?
formula_p1 = _
"=GETPIVOTDATA(""Max of " & [value_column_header] & ",Database!R1C14, & _
""Key"",& Key &)"
Debug.Print Cell.Address
Sheets("Cost Sheet").Cell.Formula = formula_p1
Thank you in advance.
Update
My current code is below I'm stuck on the concatenation in the final part. I'm not sure how to accomplish this in VBA. Also I need this to be a formula that will re evaluate on changes to the "Family" part that can be done on the worksheet. The change in "Family" is accomplished from a combo box selection.
formula_1 = _
"=GETPIVOTDATA(""Max of " & value_column_header & """,Database!$N$1,""Key""," & Concatenate(CurrentHFMFamily, G5, Left(B12, 4)) & ")"
This is the final result of what i was trying accomplish. It's not pretty but it's quick and stable.
=IF(INDIRECT(CONCATENATE("B",ROW()))=""," ",IFERROR(GETPIVOTDATA("Max of & _
"&INDIRECT((ADDRESS(11,COLUMN())))&"_ADJ",Database!R1C14,"Key",& _
LEFT(INDIRECT(CONCATENATE("B",SUM(ROW()-1))),4)&CurrentHFMFamily& & _
(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(5))),- & _
(MOD(COLUMN()+1,4))))),0))
Thanks to everyone for your ideas they helped in pointing me in the right direction.