adapt SMALL(,INDEX(FREQUENCY..) to VBA
up vote
0
down vote
favorite
I'm trying to find the minimum non-zero value. I found the following example on the web and tried to adapt it to VBA with no avail.
=SMALL((A1,C1,E1),INDEX(FREQUENCY((A1,C1,E1),0),1)+1)
Here is my adaption:
myarr = Array(A, B, C)
.Range(Cells(D, 1), Cells(WorksheetFunction.Small(myarr,WorksheetFunction.Index(WorksheetFunction.Frequency(myarr, 0), 1) + 1),1)).EntireRow.Delete
where A, B, C, D are four integers.
In the debug window, the SMALL function returned "out of context" and as soon as this line of code was executed, the compiler exit the sub as if an "Exit Sub" command was executed.
My guess is FREQUENCY function is not working. How to make it work?
For only three values, it's easy to use a non-function solution such as nested IFs but I'm interested in how to make the functions work as I might need it in the future.
excel vba excel-vba excel-formula
add a comment |
up vote
0
down vote
favorite
I'm trying to find the minimum non-zero value. I found the following example on the web and tried to adapt it to VBA with no avail.
=SMALL((A1,C1,E1),INDEX(FREQUENCY((A1,C1,E1),0),1)+1)
Here is my adaption:
myarr = Array(A, B, C)
.Range(Cells(D, 1), Cells(WorksheetFunction.Small(myarr,WorksheetFunction.Index(WorksheetFunction.Frequency(myarr, 0), 1) + 1),1)).EntireRow.Delete
where A, B, C, D are four integers.
In the debug window, the SMALL function returned "out of context" and as soon as this line of code was executed, the compiler exit the sub as if an "Exit Sub" command was executed.
My guess is FREQUENCY function is not working. How to make it work?
For only three values, it's easy to use a non-function solution such as nested IFs but I'm interested in how to make the functions work as I might need it in the future.
excel vba excel-vba excel-formula
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to find the minimum non-zero value. I found the following example on the web and tried to adapt it to VBA with no avail.
=SMALL((A1,C1,E1),INDEX(FREQUENCY((A1,C1,E1),0),1)+1)
Here is my adaption:
myarr = Array(A, B, C)
.Range(Cells(D, 1), Cells(WorksheetFunction.Small(myarr,WorksheetFunction.Index(WorksheetFunction.Frequency(myarr, 0), 1) + 1),1)).EntireRow.Delete
where A, B, C, D are four integers.
In the debug window, the SMALL function returned "out of context" and as soon as this line of code was executed, the compiler exit the sub as if an "Exit Sub" command was executed.
My guess is FREQUENCY function is not working. How to make it work?
For only three values, it's easy to use a non-function solution such as nested IFs but I'm interested in how to make the functions work as I might need it in the future.
excel vba excel-vba excel-formula
I'm trying to find the minimum non-zero value. I found the following example on the web and tried to adapt it to VBA with no avail.
=SMALL((A1,C1,E1),INDEX(FREQUENCY((A1,C1,E1),0),1)+1)
Here is my adaption:
myarr = Array(A, B, C)
.Range(Cells(D, 1), Cells(WorksheetFunction.Small(myarr,WorksheetFunction.Index(WorksheetFunction.Frequency(myarr, 0), 1) + 1),1)).EntireRow.Delete
where A, B, C, D are four integers.
In the debug window, the SMALL function returned "out of context" and as soon as this line of code was executed, the compiler exit the sub as if an "Exit Sub" command was executed.
My guess is FREQUENCY function is not working. How to make it work?
For only three values, it's easy to use a non-function solution such as nested IFs but I'm interested in how to make the functions work as I might need it in the future.
excel vba excel-vba excel-formula
excel vba excel-vba excel-formula
edited Nov 7 at 7:43
Pᴇʜ
18.4k42549
18.4k42549
asked Nov 7 at 7:27
joehua
121111
121111
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Not entirely sure what you are doing but you can do MIN IF inside of Evaluate with an array
Option Explicit
Public Sub GetMinGreaterThanZero()
Dim arr()
arr = Array(0, 4, 2, 1)
Debug.Print Evaluate("=MIN(IF({" & Join(arr, ";") & "}>0,{" & Join(arr, ";") & "}))")
End Sub
1
Works like a charm. Thank you. Would you be kind enough to explain what's going on in the formula?
– joehua
Nov 7 at 9:30
you are most welcome
– QHarr
Nov 7 at 9:30
add a comment |
up vote
1
down vote
There are several options here - the simplest option, if you have Office 365, is to use the MINIFS function, like so: WorksheetFunction.MinIf(myarr, myarr, "<>0")
But, since you haven't tagged the question as office365, I will use a different option:
Private Function MinPositive(Values As Variant) As Variant
MinPositive = CVErr(xlErrValue)
On Error GoTo FuncErr
Dim TempVal As Double, Pointer As Long
If IsArray(Values) Then
TempVal = WorksheetFunction.Max(Values) 'High bound
If TempVal <= 0 Then Exit Function 'No values greater than 0
For Pointer = LBound(Values) To UBound(Values)
If CDbl(Values(Pointer)) > 0 And CDbl(Values(Pointer)) < TempVal Then
TempVal = CDbl(Values(Pointer)) 'Swap for lower number
End If
Next Pointer
End If
MinPositive = TempVal 'Output the Min value greater than 0
FuncErr:
End Function
Thanks but I don't use Office 365. I use Excel 2010.
– joehua
Nov 7 at 9:29
@joehua Hence the second paragraph...
– Chronocidal
Nov 7 at 10:32
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Not entirely sure what you are doing but you can do MIN IF inside of Evaluate with an array
Option Explicit
Public Sub GetMinGreaterThanZero()
Dim arr()
arr = Array(0, 4, 2, 1)
Debug.Print Evaluate("=MIN(IF({" & Join(arr, ";") & "}>0,{" & Join(arr, ";") & "}))")
End Sub
1
Works like a charm. Thank you. Would you be kind enough to explain what's going on in the formula?
– joehua
Nov 7 at 9:30
you are most welcome
– QHarr
Nov 7 at 9:30
add a comment |
up vote
0
down vote
accepted
Not entirely sure what you are doing but you can do MIN IF inside of Evaluate with an array
Option Explicit
Public Sub GetMinGreaterThanZero()
Dim arr()
arr = Array(0, 4, 2, 1)
Debug.Print Evaluate("=MIN(IF({" & Join(arr, ";") & "}>0,{" & Join(arr, ";") & "}))")
End Sub
1
Works like a charm. Thank you. Would you be kind enough to explain what's going on in the formula?
– joehua
Nov 7 at 9:30
you are most welcome
– QHarr
Nov 7 at 9:30
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Not entirely sure what you are doing but you can do MIN IF inside of Evaluate with an array
Option Explicit
Public Sub GetMinGreaterThanZero()
Dim arr()
arr = Array(0, 4, 2, 1)
Debug.Print Evaluate("=MIN(IF({" & Join(arr, ";") & "}>0,{" & Join(arr, ";") & "}))")
End Sub
Not entirely sure what you are doing but you can do MIN IF inside of Evaluate with an array
Option Explicit
Public Sub GetMinGreaterThanZero()
Dim arr()
arr = Array(0, 4, 2, 1)
Debug.Print Evaluate("=MIN(IF({" & Join(arr, ";") & "}>0,{" & Join(arr, ";") & "}))")
End Sub
answered Nov 7 at 9:03
QHarr
25.4k81839
25.4k81839
1
Works like a charm. Thank you. Would you be kind enough to explain what's going on in the formula?
– joehua
Nov 7 at 9:30
you are most welcome
– QHarr
Nov 7 at 9:30
add a comment |
1
Works like a charm. Thank you. Would you be kind enough to explain what's going on in the formula?
– joehua
Nov 7 at 9:30
you are most welcome
– QHarr
Nov 7 at 9:30
1
1
Works like a charm. Thank you. Would you be kind enough to explain what's going on in the formula?
– joehua
Nov 7 at 9:30
Works like a charm. Thank you. Would you be kind enough to explain what's going on in the formula?
– joehua
Nov 7 at 9:30
you are most welcome
– QHarr
Nov 7 at 9:30
you are most welcome
– QHarr
Nov 7 at 9:30
add a comment |
up vote
1
down vote
There are several options here - the simplest option, if you have Office 365, is to use the MINIFS function, like so: WorksheetFunction.MinIf(myarr, myarr, "<>0")
But, since you haven't tagged the question as office365, I will use a different option:
Private Function MinPositive(Values As Variant) As Variant
MinPositive = CVErr(xlErrValue)
On Error GoTo FuncErr
Dim TempVal As Double, Pointer As Long
If IsArray(Values) Then
TempVal = WorksheetFunction.Max(Values) 'High bound
If TempVal <= 0 Then Exit Function 'No values greater than 0
For Pointer = LBound(Values) To UBound(Values)
If CDbl(Values(Pointer)) > 0 And CDbl(Values(Pointer)) < TempVal Then
TempVal = CDbl(Values(Pointer)) 'Swap for lower number
End If
Next Pointer
End If
MinPositive = TempVal 'Output the Min value greater than 0
FuncErr:
End Function
Thanks but I don't use Office 365. I use Excel 2010.
– joehua
Nov 7 at 9:29
@joehua Hence the second paragraph...
– Chronocidal
Nov 7 at 10:32
add a comment |
up vote
1
down vote
There are several options here - the simplest option, if you have Office 365, is to use the MINIFS function, like so: WorksheetFunction.MinIf(myarr, myarr, "<>0")
But, since you haven't tagged the question as office365, I will use a different option:
Private Function MinPositive(Values As Variant) As Variant
MinPositive = CVErr(xlErrValue)
On Error GoTo FuncErr
Dim TempVal As Double, Pointer As Long
If IsArray(Values) Then
TempVal = WorksheetFunction.Max(Values) 'High bound
If TempVal <= 0 Then Exit Function 'No values greater than 0
For Pointer = LBound(Values) To UBound(Values)
If CDbl(Values(Pointer)) > 0 And CDbl(Values(Pointer)) < TempVal Then
TempVal = CDbl(Values(Pointer)) 'Swap for lower number
End If
Next Pointer
End If
MinPositive = TempVal 'Output the Min value greater than 0
FuncErr:
End Function
Thanks but I don't use Office 365. I use Excel 2010.
– joehua
Nov 7 at 9:29
@joehua Hence the second paragraph...
– Chronocidal
Nov 7 at 10:32
add a comment |
up vote
1
down vote
up vote
1
down vote
There are several options here - the simplest option, if you have Office 365, is to use the MINIFS function, like so: WorksheetFunction.MinIf(myarr, myarr, "<>0")
But, since you haven't tagged the question as office365, I will use a different option:
Private Function MinPositive(Values As Variant) As Variant
MinPositive = CVErr(xlErrValue)
On Error GoTo FuncErr
Dim TempVal As Double, Pointer As Long
If IsArray(Values) Then
TempVal = WorksheetFunction.Max(Values) 'High bound
If TempVal <= 0 Then Exit Function 'No values greater than 0
For Pointer = LBound(Values) To UBound(Values)
If CDbl(Values(Pointer)) > 0 And CDbl(Values(Pointer)) < TempVal Then
TempVal = CDbl(Values(Pointer)) 'Swap for lower number
End If
Next Pointer
End If
MinPositive = TempVal 'Output the Min value greater than 0
FuncErr:
End Function
There are several options here - the simplest option, if you have Office 365, is to use the MINIFS function, like so: WorksheetFunction.MinIf(myarr, myarr, "<>0")
But, since you haven't tagged the question as office365, I will use a different option:
Private Function MinPositive(Values As Variant) As Variant
MinPositive = CVErr(xlErrValue)
On Error GoTo FuncErr
Dim TempVal As Double, Pointer As Long
If IsArray(Values) Then
TempVal = WorksheetFunction.Max(Values) 'High bound
If TempVal <= 0 Then Exit Function 'No values greater than 0
For Pointer = LBound(Values) To UBound(Values)
If CDbl(Values(Pointer)) > 0 And CDbl(Values(Pointer)) < TempVal Then
TempVal = CDbl(Values(Pointer)) 'Swap for lower number
End If
Next Pointer
End If
MinPositive = TempVal 'Output the Min value greater than 0
FuncErr:
End Function
answered Nov 7 at 8:47
Chronocidal
2,4801216
2,4801216
Thanks but I don't use Office 365. I use Excel 2010.
– joehua
Nov 7 at 9:29
@joehua Hence the second paragraph...
– Chronocidal
Nov 7 at 10:32
add a comment |
Thanks but I don't use Office 365. I use Excel 2010.
– joehua
Nov 7 at 9:29
@joehua Hence the second paragraph...
– Chronocidal
Nov 7 at 10:32
Thanks but I don't use Office 365. I use Excel 2010.
– joehua
Nov 7 at 9:29
Thanks but I don't use Office 365. I use Excel 2010.
– joehua
Nov 7 at 9:29
@joehua Hence the second paragraph...
– Chronocidal
Nov 7 at 10:32
@joehua Hence the second paragraph...
– Chronocidal
Nov 7 at 10:32
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185093%2fadapt-small-indexfrequency-to-vba%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password