application.worksheetfunction.sum not evaluating string - vba

I have the following code in my procedure built up to be evaluated within a much larger loop
CalculationHoldArray(Loopcount) = "'[" & Usefile1.Name & "]" & _
Worksheet1 & "'!" & Cells1 & ",'[" & Usefile2.Name & "]" & _
Worksheet2 & "'!" & Cells2
Sheets("ECAP PARAMETER INPUTS").Cells(31, "F").Value =
Application.WorksheetFunction.Sum(CalculationHoldArray(Loopcount))
The string returned for the array is:
'[Control Model v1.35- nonfunctional.xlsm]Input'!E2, _
'[Control Model v1.35- nonfunctional.xlsm]Input'!E3
But the application.worksheetfunction.sum is returning an error 1004. "Unable to get the sum property of the worksheet class". Anyone know what the issue is that it won't let me use the sum property here?

Instead of using the Application.Worksheet.Function command, simply use the Evaluate command, which requires adding in the "sum" and brackets to make it a valid formulae.

But I have found a solution- instead of using the application.worksheet.function command, I can simply use the "EVALUATE" command, just required adding in the "sum" and brackets to make it a full formulae –

Related

Create visible formula through VBA

My challenge is to code a visible formula into cells that end-users can read the reference. Highlighted yellow line of code causes error "Application Defined or Order defined error". The requirement is that in cells are simple formula like below, which takes first number from workbook where actual result will be and other one comes from different workbook. Actual code locates in a third excel.
=5/78
Variables
Private AR As New Dimension
Private UR As New Dimension
UR.KeySheet --> Sheet 1
AR.KeySheet --> DivederNumbers
UR.Wb --> myWorkbook.xlxs
AR.Wb --> myOtherWorkbook.xlxs
.
Dim test As String
If AR.Wb.Sheets(AR.KeySheet).Cells(Cell3.Row, Cell2.Column) > 0 Then
test = AR.Wb.Path & "\" & AR.Wb.Name
'**THIS LINE CAUSES ERROR:
UR.Wb.Sheets("RESULT").Cells(Cell1.Row, Cell1.Column).Formula = _
"='" & UR.KeySheet & "'!" & Cells(Cell1.Row, Cell1.Column).Address & "/" _
& "'" & [test] & AR.KeySheet & "'!" & Cells(Cell1.Row, _
Cell1.Column).Address(External:=True)
Exit For
End If
You can't reference a sheet like that. AR.KeySheet is only valid as part of the Sheets() collection.
Here is a solution for referencing the sheet name: Get a worksheet name using Excel VBA
You would need to use:
"='" & Application.Caller.Worksheet.Name & "'!" & Cells(Cell1.row, Cell1.column).Address & "/" _

VBA Set Cell value to text containing ' not working

I'm trying to get a macro to type out a heap of formulae for me. The formula goes something like this:
=COUNTIF('other_sheet'!A:A,"hello*")
The user can specify A and other_sheet, so I select the cell and then go
ActiveCell.Value = "=COUNTIF('" & othercell & "'!" & column & ":" & _
column & """,hello*"")"
But it keeps giving me errors like:
1004: Object Defined Error
I have tried using ActiveCell.Text, ActiveCell.Formula etc. and they all don't work.
It needs to be ActiveCell.Formula and ", is the wrong way around next to Hello. It should be:
ActiveCell.Formula= "=COUNTIF('" & othercell & "'!" & column _
& ":" & column & ",""hello*"")"
Apparently you can only put proper, completed answers into a cell.
i.e. if the output turns out to be "=sum(A:A" and a bracket isn't closed, then the application throws an error just as if you typed it in manually.

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.

Using string variables within a formula in VBA

I do not understand why this code doesn't work:
Cells(i, formula_col_index).Value = "=IF(" & time_location & "<>" & time_benchmark & ",""ERROR"",""OK"")"
where
time_location=" 17:00:00",
time_benchmark=" 17:30:00"
It keeps throwing application-defined (or object-defined) error.
Thanks in advance.
Since your variables time_location and time_benchmark contains string values, you should include them in double quotes when using formula:
Cells(i, formula_col_index).Value = "=IF(""" & time_location & """<>""" & time_benchmark & """,""ERROR"",""OK"")"