Converting from Excel formula to VBA code












0















What's the equivalent of the following Excel code in VBA?



=MATCH(A1,Ranged_Name,0)


where Ranged_Name is a ranged name on the workbook level.










share|improve this question

























  • You may want to read about worksheet functions

    – cybernetic.nomad
    Nov 20 '18 at 18:33


















0















What's the equivalent of the following Excel code in VBA?



=MATCH(A1,Ranged_Name,0)


where Ranged_Name is a ranged name on the workbook level.










share|improve this question

























  • You may want to read about worksheet functions

    – cybernetic.nomad
    Nov 20 '18 at 18:33
















0












0








0








What's the equivalent of the following Excel code in VBA?



=MATCH(A1,Ranged_Name,0)


where Ranged_Name is a ranged name on the workbook level.










share|improve this question
















What's the equivalent of the following Excel code in VBA?



=MATCH(A1,Ranged_Name,0)


where Ranged_Name is a ranged name on the workbook level.







excel vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:31









Scott Craner

90.3k82550




90.3k82550










asked Nov 20 '18 at 18:30









Garry WGarry W

767




767













  • You may want to read about worksheet functions

    – cybernetic.nomad
    Nov 20 '18 at 18:33





















  • You may want to read about worksheet functions

    – cybernetic.nomad
    Nov 20 '18 at 18:33



















You may want to read about worksheet functions

– cybernetic.nomad
Nov 20 '18 at 18:33







You may want to read about worksheet functions

– cybernetic.nomad
Nov 20 '18 at 18:33














3 Answers
3






active

oldest

votes


















1














In both a worksheet cell and VBA:



Sub dural()
MsgBox Evaluate("Match(A1,Range_Name,0)")
End Sub


enter image description here



EDIT#1:



Based on the Comments, a better alternative:



Sub dural()
Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction

Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")
Set wf = Application.WorksheetFunction

v = wf.Match(r1, r2, 0)
MsgBox v
End Sub


and to use Find():



Sub larud()
Dim r1 As Range, r2 As Range, v As Variant

Set r1 = Sheets("Sheet1").Range("A1")
Set r2 = Range("Range_Name")

v = r2.Find(What:=r1.Value, After:=r2(1)).Row

MsgBox v
End Sub





share|improve this answer





















  • 2





    Might be an improvment to use the Worksheet.Evaluate method, rather than the Application version? Otherwise, the result will vary according to which sheet is active.

    – Tim Williams
    Nov 20 '18 at 18:51











  • @TimWilliams You are correct!

    – Gary's Student
    Nov 20 '18 at 19:12











  • I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)

    – Garry W
    Nov 21 '18 at 14:03











  • @GarryWang See my EDIT#1

    – Gary's Student
    Nov 21 '18 at 14:14











  • @Gary'sStudent I surely feel like the student here. Thank you for the help!

    – Garry W
    Nov 21 '18 at 18:23



















4














To add one more:



Dim r
With Activesheet
r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
End with

If Not IsError(r) Then
'got a match
End If





share|improve this answer































    1














    You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.






    share|improve this answer























      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%2f53399325%2fconverting-from-excel-formula-to-vba-code%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      In both a worksheet cell and VBA:



      Sub dural()
      MsgBox Evaluate("Match(A1,Range_Name,0)")
      End Sub


      enter image description here



      EDIT#1:



      Based on the Comments, a better alternative:



      Sub dural()
      Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction

      Set r1 = Sheets("Sheet1").Range("A1")
      Set r2 = Range("Range_Name")
      Set wf = Application.WorksheetFunction

      v = wf.Match(r1, r2, 0)
      MsgBox v
      End Sub


      and to use Find():



      Sub larud()
      Dim r1 As Range, r2 As Range, v As Variant

      Set r1 = Sheets("Sheet1").Range("A1")
      Set r2 = Range("Range_Name")

      v = r2.Find(What:=r1.Value, After:=r2(1)).Row

      MsgBox v
      End Sub





      share|improve this answer





















      • 2





        Might be an improvment to use the Worksheet.Evaluate method, rather than the Application version? Otherwise, the result will vary according to which sheet is active.

        – Tim Williams
        Nov 20 '18 at 18:51











      • @TimWilliams You are correct!

        – Gary's Student
        Nov 20 '18 at 19:12











      • I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)

        – Garry W
        Nov 21 '18 at 14:03











      • @GarryWang See my EDIT#1

        – Gary's Student
        Nov 21 '18 at 14:14











      • @Gary'sStudent I surely feel like the student here. Thank you for the help!

        – Garry W
        Nov 21 '18 at 18:23
















      1














      In both a worksheet cell and VBA:



      Sub dural()
      MsgBox Evaluate("Match(A1,Range_Name,0)")
      End Sub


      enter image description here



      EDIT#1:



      Based on the Comments, a better alternative:



      Sub dural()
      Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction

      Set r1 = Sheets("Sheet1").Range("A1")
      Set r2 = Range("Range_Name")
      Set wf = Application.WorksheetFunction

      v = wf.Match(r1, r2, 0)
      MsgBox v
      End Sub


      and to use Find():



      Sub larud()
      Dim r1 As Range, r2 As Range, v As Variant

      Set r1 = Sheets("Sheet1").Range("A1")
      Set r2 = Range("Range_Name")

      v = r2.Find(What:=r1.Value, After:=r2(1)).Row

      MsgBox v
      End Sub





      share|improve this answer





















      • 2





        Might be an improvment to use the Worksheet.Evaluate method, rather than the Application version? Otherwise, the result will vary according to which sheet is active.

        – Tim Williams
        Nov 20 '18 at 18:51











      • @TimWilliams You are correct!

        – Gary's Student
        Nov 20 '18 at 19:12











      • I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)

        – Garry W
        Nov 21 '18 at 14:03











      • @GarryWang See my EDIT#1

        – Gary's Student
        Nov 21 '18 at 14:14











      • @Gary'sStudent I surely feel like the student here. Thank you for the help!

        – Garry W
        Nov 21 '18 at 18:23














      1












      1








      1







      In both a worksheet cell and VBA:



      Sub dural()
      MsgBox Evaluate("Match(A1,Range_Name,0)")
      End Sub


      enter image description here



      EDIT#1:



      Based on the Comments, a better alternative:



      Sub dural()
      Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction

      Set r1 = Sheets("Sheet1").Range("A1")
      Set r2 = Range("Range_Name")
      Set wf = Application.WorksheetFunction

      v = wf.Match(r1, r2, 0)
      MsgBox v
      End Sub


      and to use Find():



      Sub larud()
      Dim r1 As Range, r2 As Range, v As Variant

      Set r1 = Sheets("Sheet1").Range("A1")
      Set r2 = Range("Range_Name")

      v = r2.Find(What:=r1.Value, After:=r2(1)).Row

      MsgBox v
      End Sub





      share|improve this answer















      In both a worksheet cell and VBA:



      Sub dural()
      MsgBox Evaluate("Match(A1,Range_Name,0)")
      End Sub


      enter image description here



      EDIT#1:



      Based on the Comments, a better alternative:



      Sub dural()
      Dim r1 As Range, r2 As Range, v As Variant, wf As WorksheetFunction

      Set r1 = Sheets("Sheet1").Range("A1")
      Set r2 = Range("Range_Name")
      Set wf = Application.WorksheetFunction

      v = wf.Match(r1, r2, 0)
      MsgBox v
      End Sub


      and to use Find():



      Sub larud()
      Dim r1 As Range, r2 As Range, v As Variant

      Set r1 = Sheets("Sheet1").Range("A1")
      Set r2 = Range("Range_Name")

      v = r2.Find(What:=r1.Value, After:=r2(1)).Row

      MsgBox v
      End Sub






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 21 '18 at 14:13

























      answered Nov 20 '18 at 18:39









      Gary's StudentGary's Student

      72.8k93962




      72.8k93962








      • 2





        Might be an improvment to use the Worksheet.Evaluate method, rather than the Application version? Otherwise, the result will vary according to which sheet is active.

        – Tim Williams
        Nov 20 '18 at 18:51











      • @TimWilliams You are correct!

        – Gary's Student
        Nov 20 '18 at 19:12











      • I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)

        – Garry W
        Nov 21 '18 at 14:03











      • @GarryWang See my EDIT#1

        – Gary's Student
        Nov 21 '18 at 14:14











      • @Gary'sStudent I surely feel like the student here. Thank you for the help!

        – Garry W
        Nov 21 '18 at 18:23














      • 2





        Might be an improvment to use the Worksheet.Evaluate method, rather than the Application version? Otherwise, the result will vary according to which sheet is active.

        – Tim Williams
        Nov 20 '18 at 18:51











      • @TimWilliams You are correct!

        – Gary's Student
        Nov 20 '18 at 19:12











      • I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)

        – Garry W
        Nov 21 '18 at 14:03











      • @GarryWang See my EDIT#1

        – Gary's Student
        Nov 21 '18 at 14:14











      • @Gary'sStudent I surely feel like the student here. Thank you for the help!

        – Garry W
        Nov 21 '18 at 18:23








      2




      2





      Might be an improvment to use the Worksheet.Evaluate method, rather than the Application version? Otherwise, the result will vary according to which sheet is active.

      – Tim Williams
      Nov 20 '18 at 18:51





      Might be an improvment to use the Worksheet.Evaluate method, rather than the Application version? Otherwise, the result will vary according to which sheet is active.

      – Tim Williams
      Nov 20 '18 at 18:51













      @TimWilliams You are correct!

      – Gary's Student
      Nov 20 '18 at 19:12





      @TimWilliams You are correct!

      – Gary's Student
      Nov 20 '18 at 19:12













      I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)

      – Garry W
      Nov 21 '18 at 14:03





      I used Evaluate("Match('Sheet1'!A1,Range_Name,0)") and that also worked. :)

      – Garry W
      Nov 21 '18 at 14:03













      @GarryWang See my EDIT#1

      – Gary's Student
      Nov 21 '18 at 14:14





      @GarryWang See my EDIT#1

      – Gary's Student
      Nov 21 '18 at 14:14













      @Gary'sStudent I surely feel like the student here. Thank you for the help!

      – Garry W
      Nov 21 '18 at 18:23





      @Gary'sStudent I surely feel like the student here. Thank you for the help!

      – Garry W
      Nov 21 '18 at 18:23













      4














      To add one more:



      Dim r
      With Activesheet
      r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
      End with

      If Not IsError(r) Then
      'got a match
      End If





      share|improve this answer




























        4














        To add one more:



        Dim r
        With Activesheet
        r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
        End with

        If Not IsError(r) Then
        'got a match
        End If





        share|improve this answer


























          4












          4








          4







          To add one more:



          Dim r
          With Activesheet
          r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
          End with

          If Not IsError(r) Then
          'got a match
          End If





          share|improve this answer













          To add one more:



          Dim r
          With Activesheet
          r = Application.Match(.Range("A1").Value, Range("Ranged_Name"), 0)
          End with

          If Not IsError(r) Then
          'got a match
          End If






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 18:53









          Tim WilliamsTim Williams

          86.3k96986




          86.3k96986























              1














              You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.






              share|improve this answer




























                1














                You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.






                share|improve this answer


























                  1












                  1








                  1







                  You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.






                  share|improve this answer













                  You can either use Range().Find or Evaluate() your Match, if the sheet is the ActiveSheet.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 18:37









                  Patrick HonorezPatrick Honorez

                  18.8k563118




                  18.8k563118






























                      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%2f53399325%2fconverting-from-excel-formula-to-vba-code%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

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith