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.










share|improve this question




























    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.










    share|improve this question


























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 7:43









      Pᴇʜ

      18.4k42549




      18.4k42549










      asked Nov 7 at 7:27









      joehua

      121111




      121111
























          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





          share|improve this answer

















          • 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


















          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





          share|improve this answer





















          • 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











          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',
          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%2f53185093%2fadapt-small-indexfrequency-to-vba%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          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





          share|improve this answer

















          • 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















          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





          share|improve this answer

















          • 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













          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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














          • 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












          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





          share|improve this answer





















          • 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















          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





          share|improve this answer





















          • 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













          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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




















































































          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          FTSE 250 Index