Application.worksheetfunction.sumif with internal arrays as argument












0















I am trying to build a subroutine which checks a certain condition in a client list, separates out the entries which meets the condition, and then average the remaining entries for each name. It is like a doing a pivot table with VBA. I did not want to use the pivot table, since writing the data into a new sheet, refresh it and do something with it adds an unnecessary burden on the speed of the tool. Furthermore, all the arrays are to be kept within the code, rather than written on the sheets. I am almost done with the code, but it is giving me an error in the very end, where I am using the sumif condition.
A point of clarification: The argument 'Number' is a global variable declared in the main tool, which comes from the count of names from the main list, which is in sheet5. I hope that the code is self explanatory beyond that.
What I am getting while running the code is the Error- Run-time error '1004':
Method 'Range' of object '_Global' failed
on the line TaskArray(k, 1) = Application.WorksheetFunction.SumIf(Range(Names), NewList(k), Range(ParameterB))



The Code-



Sub Task()

Dim Names() As Variant 'Declare Names
ReDim Names(0 To Number) As Variant 'Declare Names as a vector
Dim ParameterA() As Variant 'Declare Parameter A
ReDim ParameterA(0 To Number) As Variant 'Declare Parameter A as a vector
Dim ParameterB() As Variant 'Declare Parameter B
ReDim ParameterB(0 To Number) As Variant 'Declare Parameter B as a vector


Dim i As Integer

For i = 1 To Number

Select Case Sheet5.Range("BO" & i + 1) - Sheet5.Range("BN" & i + 1)
Case 0
Names(i) = ""
ParameterA(i) = Sheet5.Range("BN" & i + 1) - Sheet5.Range("BL" & i + 1)
ParameterB(i) = ""
Case Else
Names(i) = Sheet5.Range("F" & i + 1)
ParameterA(i) = Sheet5.Range("BN" & i + 1) - Sheet5.Range("BL" & i + 1)
ParameterB(i) = Sheet5.Range("BO" & i + 1) - Sheet5.Range("BN" & i + 1)
End Select

Next i


Sheet3.Range("T159") = Application.WorksheetFunction.Sum(ParameterA()) 'Write the total of Parameter A
Sheet3.Range("T160") = Application.WorksheetFunction.Sum(ParameterB()) 'Write the total of Parameter B
'________________________ To isolate the list of Names (Unique) with existent Parameter B
Dim NewList() As Variant

Dim j As Long
Dim d As Scripting.Dictionary
Set d = New Scripting.Dictionary
With d
For j = LBound(Names) To UBound(Names)
If IsMissing(Names(j)) = False Then
.item(Names(j)) = 1
End If
Next
NewList = .Keys
End With
'________________________To create an array of sums of Parameter B
For k = 1 To Application.WorksheetFunction.CountA(NewList) - 1
Dim TaskArray() As Variant
ReDim TaskArray(1 To k, 0 To 1) As Variant
ReDim Names(0 To Number) As Variant
ReDim ParameterB(0 To Number) As Variant
TaskArray(k, 0) = NewList(k)
TaskArray(k, 1) = Application.WorksheetFunction.SumIf(Range(Names), NewList(k), Range(ParameterB))
Sheet19.Range("H" & k + 1) = TaskArray(k, 0)
Sheet19.Range("I" & k + 1) = TaskArray(k, 1)
Next k

End Sub









share|improve this question

























  • Your ranges appear to be invalid. Your arrays contain either "" or the value of a cell, and I suspect the cells don't contain the names or addresses of ranges.

    – Rory
    Nov 14 '18 at 15:30











  • Thank you, @Rory, for prompt response. My original arrays- Names() contained either "" or a string value and ParameterB() contained either "" or a double, indeed. However, what I am trying with the TaskArray is to create an auxiliary array with only the unique name list and the sums of their parameterB's. I am getting the first column, that is, the unique names. There are no more blanks in it, and its range is far smaller than the original array's. It is the sumif column which is giving me trouble.

    – Rama
    Nov 14 '18 at 15:38








  • 2





    Those are not ranges, they are arrays. You can't just put Range() round them to turn them into ranges. I think you need to rethink your entire approach since SUMIF will only work with ranges on a worksheet.

    – Rory
    Nov 14 '18 at 15:48











  • I see...Thank you!

    – Rama
    Nov 14 '18 at 15:52
















0















I am trying to build a subroutine which checks a certain condition in a client list, separates out the entries which meets the condition, and then average the remaining entries for each name. It is like a doing a pivot table with VBA. I did not want to use the pivot table, since writing the data into a new sheet, refresh it and do something with it adds an unnecessary burden on the speed of the tool. Furthermore, all the arrays are to be kept within the code, rather than written on the sheets. I am almost done with the code, but it is giving me an error in the very end, where I am using the sumif condition.
A point of clarification: The argument 'Number' is a global variable declared in the main tool, which comes from the count of names from the main list, which is in sheet5. I hope that the code is self explanatory beyond that.
What I am getting while running the code is the Error- Run-time error '1004':
Method 'Range' of object '_Global' failed
on the line TaskArray(k, 1) = Application.WorksheetFunction.SumIf(Range(Names), NewList(k), Range(ParameterB))



The Code-



Sub Task()

Dim Names() As Variant 'Declare Names
ReDim Names(0 To Number) As Variant 'Declare Names as a vector
Dim ParameterA() As Variant 'Declare Parameter A
ReDim ParameterA(0 To Number) As Variant 'Declare Parameter A as a vector
Dim ParameterB() As Variant 'Declare Parameter B
ReDim ParameterB(0 To Number) As Variant 'Declare Parameter B as a vector


Dim i As Integer

For i = 1 To Number

Select Case Sheet5.Range("BO" & i + 1) - Sheet5.Range("BN" & i + 1)
Case 0
Names(i) = ""
ParameterA(i) = Sheet5.Range("BN" & i + 1) - Sheet5.Range("BL" & i + 1)
ParameterB(i) = ""
Case Else
Names(i) = Sheet5.Range("F" & i + 1)
ParameterA(i) = Sheet5.Range("BN" & i + 1) - Sheet5.Range("BL" & i + 1)
ParameterB(i) = Sheet5.Range("BO" & i + 1) - Sheet5.Range("BN" & i + 1)
End Select

Next i


Sheet3.Range("T159") = Application.WorksheetFunction.Sum(ParameterA()) 'Write the total of Parameter A
Sheet3.Range("T160") = Application.WorksheetFunction.Sum(ParameterB()) 'Write the total of Parameter B
'________________________ To isolate the list of Names (Unique) with existent Parameter B
Dim NewList() As Variant

Dim j As Long
Dim d As Scripting.Dictionary
Set d = New Scripting.Dictionary
With d
For j = LBound(Names) To UBound(Names)
If IsMissing(Names(j)) = False Then
.item(Names(j)) = 1
End If
Next
NewList = .Keys
End With
'________________________To create an array of sums of Parameter B
For k = 1 To Application.WorksheetFunction.CountA(NewList) - 1
Dim TaskArray() As Variant
ReDim TaskArray(1 To k, 0 To 1) As Variant
ReDim Names(0 To Number) As Variant
ReDim ParameterB(0 To Number) As Variant
TaskArray(k, 0) = NewList(k)
TaskArray(k, 1) = Application.WorksheetFunction.SumIf(Range(Names), NewList(k), Range(ParameterB))
Sheet19.Range("H" & k + 1) = TaskArray(k, 0)
Sheet19.Range("I" & k + 1) = TaskArray(k, 1)
Next k

End Sub









share|improve this question

























  • Your ranges appear to be invalid. Your arrays contain either "" or the value of a cell, and I suspect the cells don't contain the names or addresses of ranges.

    – Rory
    Nov 14 '18 at 15:30











  • Thank you, @Rory, for prompt response. My original arrays- Names() contained either "" or a string value and ParameterB() contained either "" or a double, indeed. However, what I am trying with the TaskArray is to create an auxiliary array with only the unique name list and the sums of their parameterB's. I am getting the first column, that is, the unique names. There are no more blanks in it, and its range is far smaller than the original array's. It is the sumif column which is giving me trouble.

    – Rama
    Nov 14 '18 at 15:38








  • 2





    Those are not ranges, they are arrays. You can't just put Range() round them to turn them into ranges. I think you need to rethink your entire approach since SUMIF will only work with ranges on a worksheet.

    – Rory
    Nov 14 '18 at 15:48











  • I see...Thank you!

    – Rama
    Nov 14 '18 at 15:52














0












0








0








I am trying to build a subroutine which checks a certain condition in a client list, separates out the entries which meets the condition, and then average the remaining entries for each name. It is like a doing a pivot table with VBA. I did not want to use the pivot table, since writing the data into a new sheet, refresh it and do something with it adds an unnecessary burden on the speed of the tool. Furthermore, all the arrays are to be kept within the code, rather than written on the sheets. I am almost done with the code, but it is giving me an error in the very end, where I am using the sumif condition.
A point of clarification: The argument 'Number' is a global variable declared in the main tool, which comes from the count of names from the main list, which is in sheet5. I hope that the code is self explanatory beyond that.
What I am getting while running the code is the Error- Run-time error '1004':
Method 'Range' of object '_Global' failed
on the line TaskArray(k, 1) = Application.WorksheetFunction.SumIf(Range(Names), NewList(k), Range(ParameterB))



The Code-



Sub Task()

Dim Names() As Variant 'Declare Names
ReDim Names(0 To Number) As Variant 'Declare Names as a vector
Dim ParameterA() As Variant 'Declare Parameter A
ReDim ParameterA(0 To Number) As Variant 'Declare Parameter A as a vector
Dim ParameterB() As Variant 'Declare Parameter B
ReDim ParameterB(0 To Number) As Variant 'Declare Parameter B as a vector


Dim i As Integer

For i = 1 To Number

Select Case Sheet5.Range("BO" & i + 1) - Sheet5.Range("BN" & i + 1)
Case 0
Names(i) = ""
ParameterA(i) = Sheet5.Range("BN" & i + 1) - Sheet5.Range("BL" & i + 1)
ParameterB(i) = ""
Case Else
Names(i) = Sheet5.Range("F" & i + 1)
ParameterA(i) = Sheet5.Range("BN" & i + 1) - Sheet5.Range("BL" & i + 1)
ParameterB(i) = Sheet5.Range("BO" & i + 1) - Sheet5.Range("BN" & i + 1)
End Select

Next i


Sheet3.Range("T159") = Application.WorksheetFunction.Sum(ParameterA()) 'Write the total of Parameter A
Sheet3.Range("T160") = Application.WorksheetFunction.Sum(ParameterB()) 'Write the total of Parameter B
'________________________ To isolate the list of Names (Unique) with existent Parameter B
Dim NewList() As Variant

Dim j As Long
Dim d As Scripting.Dictionary
Set d = New Scripting.Dictionary
With d
For j = LBound(Names) To UBound(Names)
If IsMissing(Names(j)) = False Then
.item(Names(j)) = 1
End If
Next
NewList = .Keys
End With
'________________________To create an array of sums of Parameter B
For k = 1 To Application.WorksheetFunction.CountA(NewList) - 1
Dim TaskArray() As Variant
ReDim TaskArray(1 To k, 0 To 1) As Variant
ReDim Names(0 To Number) As Variant
ReDim ParameterB(0 To Number) As Variant
TaskArray(k, 0) = NewList(k)
TaskArray(k, 1) = Application.WorksheetFunction.SumIf(Range(Names), NewList(k), Range(ParameterB))
Sheet19.Range("H" & k + 1) = TaskArray(k, 0)
Sheet19.Range("I" & k + 1) = TaskArray(k, 1)
Next k

End Sub









share|improve this question
















I am trying to build a subroutine which checks a certain condition in a client list, separates out the entries which meets the condition, and then average the remaining entries for each name. It is like a doing a pivot table with VBA. I did not want to use the pivot table, since writing the data into a new sheet, refresh it and do something with it adds an unnecessary burden on the speed of the tool. Furthermore, all the arrays are to be kept within the code, rather than written on the sheets. I am almost done with the code, but it is giving me an error in the very end, where I am using the sumif condition.
A point of clarification: The argument 'Number' is a global variable declared in the main tool, which comes from the count of names from the main list, which is in sheet5. I hope that the code is self explanatory beyond that.
What I am getting while running the code is the Error- Run-time error '1004':
Method 'Range' of object '_Global' failed
on the line TaskArray(k, 1) = Application.WorksheetFunction.SumIf(Range(Names), NewList(k), Range(ParameterB))



The Code-



Sub Task()

Dim Names() As Variant 'Declare Names
ReDim Names(0 To Number) As Variant 'Declare Names as a vector
Dim ParameterA() As Variant 'Declare Parameter A
ReDim ParameterA(0 To Number) As Variant 'Declare Parameter A as a vector
Dim ParameterB() As Variant 'Declare Parameter B
ReDim ParameterB(0 To Number) As Variant 'Declare Parameter B as a vector


Dim i As Integer

For i = 1 To Number

Select Case Sheet5.Range("BO" & i + 1) - Sheet5.Range("BN" & i + 1)
Case 0
Names(i) = ""
ParameterA(i) = Sheet5.Range("BN" & i + 1) - Sheet5.Range("BL" & i + 1)
ParameterB(i) = ""
Case Else
Names(i) = Sheet5.Range("F" & i + 1)
ParameterA(i) = Sheet5.Range("BN" & i + 1) - Sheet5.Range("BL" & i + 1)
ParameterB(i) = Sheet5.Range("BO" & i + 1) - Sheet5.Range("BN" & i + 1)
End Select

Next i


Sheet3.Range("T159") = Application.WorksheetFunction.Sum(ParameterA()) 'Write the total of Parameter A
Sheet3.Range("T160") = Application.WorksheetFunction.Sum(ParameterB()) 'Write the total of Parameter B
'________________________ To isolate the list of Names (Unique) with existent Parameter B
Dim NewList() As Variant

Dim j As Long
Dim d As Scripting.Dictionary
Set d = New Scripting.Dictionary
With d
For j = LBound(Names) To UBound(Names)
If IsMissing(Names(j)) = False Then
.item(Names(j)) = 1
End If
Next
NewList = .Keys
End With
'________________________To create an array of sums of Parameter B
For k = 1 To Application.WorksheetFunction.CountA(NewList) - 1
Dim TaskArray() As Variant
ReDim TaskArray(1 To k, 0 To 1) As Variant
ReDim Names(0 To Number) As Variant
ReDim ParameterB(0 To Number) As Variant
TaskArray(k, 0) = NewList(k)
TaskArray(k, 1) = Application.WorksheetFunction.SumIf(Range(Names), NewList(k), Range(ParameterB))
Sheet19.Range("H" & k + 1) = TaskArray(k, 0)
Sheet19.Range("I" & k + 1) = TaskArray(k, 1)
Next k

End Sub






arrays excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 15:47









WhatsThePoint

2,18662136




2,18662136










asked Nov 14 '18 at 15:24









RamaRama

11




11













  • Your ranges appear to be invalid. Your arrays contain either "" or the value of a cell, and I suspect the cells don't contain the names or addresses of ranges.

    – Rory
    Nov 14 '18 at 15:30











  • Thank you, @Rory, for prompt response. My original arrays- Names() contained either "" or a string value and ParameterB() contained either "" or a double, indeed. However, what I am trying with the TaskArray is to create an auxiliary array with only the unique name list and the sums of their parameterB's. I am getting the first column, that is, the unique names. There are no more blanks in it, and its range is far smaller than the original array's. It is the sumif column which is giving me trouble.

    – Rama
    Nov 14 '18 at 15:38








  • 2





    Those are not ranges, they are arrays. You can't just put Range() round them to turn them into ranges. I think you need to rethink your entire approach since SUMIF will only work with ranges on a worksheet.

    – Rory
    Nov 14 '18 at 15:48











  • I see...Thank you!

    – Rama
    Nov 14 '18 at 15:52



















  • Your ranges appear to be invalid. Your arrays contain either "" or the value of a cell, and I suspect the cells don't contain the names or addresses of ranges.

    – Rory
    Nov 14 '18 at 15:30











  • Thank you, @Rory, for prompt response. My original arrays- Names() contained either "" or a string value and ParameterB() contained either "" or a double, indeed. However, what I am trying with the TaskArray is to create an auxiliary array with only the unique name list and the sums of their parameterB's. I am getting the first column, that is, the unique names. There are no more blanks in it, and its range is far smaller than the original array's. It is the sumif column which is giving me trouble.

    – Rama
    Nov 14 '18 at 15:38








  • 2





    Those are not ranges, they are arrays. You can't just put Range() round them to turn them into ranges. I think you need to rethink your entire approach since SUMIF will only work with ranges on a worksheet.

    – Rory
    Nov 14 '18 at 15:48











  • I see...Thank you!

    – Rama
    Nov 14 '18 at 15:52

















Your ranges appear to be invalid. Your arrays contain either "" or the value of a cell, and I suspect the cells don't contain the names or addresses of ranges.

– Rory
Nov 14 '18 at 15:30





Your ranges appear to be invalid. Your arrays contain either "" or the value of a cell, and I suspect the cells don't contain the names or addresses of ranges.

– Rory
Nov 14 '18 at 15:30













Thank you, @Rory, for prompt response. My original arrays- Names() contained either "" or a string value and ParameterB() contained either "" or a double, indeed. However, what I am trying with the TaskArray is to create an auxiliary array with only the unique name list and the sums of their parameterB's. I am getting the first column, that is, the unique names. There are no more blanks in it, and its range is far smaller than the original array's. It is the sumif column which is giving me trouble.

– Rama
Nov 14 '18 at 15:38







Thank you, @Rory, for prompt response. My original arrays- Names() contained either "" or a string value and ParameterB() contained either "" or a double, indeed. However, what I am trying with the TaskArray is to create an auxiliary array with only the unique name list and the sums of their parameterB's. I am getting the first column, that is, the unique names. There are no more blanks in it, and its range is far smaller than the original array's. It is the sumif column which is giving me trouble.

– Rama
Nov 14 '18 at 15:38






2




2





Those are not ranges, they are arrays. You can't just put Range() round them to turn them into ranges. I think you need to rethink your entire approach since SUMIF will only work with ranges on a worksheet.

– Rory
Nov 14 '18 at 15:48





Those are not ranges, they are arrays. You can't just put Range() round them to turn them into ranges. I think you need to rethink your entire approach since SUMIF will only work with ranges on a worksheet.

– Rory
Nov 14 '18 at 15:48













I see...Thank you!

– Rama
Nov 14 '18 at 15:52





I see...Thank you!

– Rama
Nov 14 '18 at 15:52












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303527%2fapplication-worksheetfunction-sumif-with-internal-arrays-as-argument%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303527%2fapplication-worksheetfunction-sumif-with-internal-arrays-as-argument%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python