How to compare value with other values in range easily









up vote
0
down vote

favorite












could someone help, how to rewrite folowing condition somehow smarter (and definitely shorter)?



It should pretty much say: If value in one specific cell is not empty and at the same time is different from any value in cells from X32 to X47 → do something



Thanks a lot!



For i = 4 To 69
If Range(SO_COLUMN & i) <> "" _
And Range(SO_COLUMN & i) <> Range("X32") And Range(SO_COLUMN & i) <> Range("X33") And Range(SO_COLUMN & i) <> Range("X34") And Range(SO_COLUMN & i) <> Range("X35") _
And Range(SO_COLUMN & i) <> Range("X36") And Range(SO_COLUMN & i) <> Range("X37") And Range(SO_COLUMN & i) <> Range("X38") And Range(SO_COLUMN & i) <> Range("X39") _
And Range(SO_COLUMN & i) <> Range("X40") And Range(SO_COLUMN & i) <> Range("X41") And Range(SO_COLUMN & i) <> Range("X42") And Range(SO_COLUMN & i) <> Range("X43") _
And Range(SO_COLUMN & i) <> Range("X44") And Range(SO_COLUMN & i) <> Range("X45") And Range(SO_COLUMN & i) <> Range("X46") And Range(SO_COLUMN & i) <> Range("X47") Then

"DO SOMETHING"
End If
Next i









share|improve this question

























    up vote
    0
    down vote

    favorite












    could someone help, how to rewrite folowing condition somehow smarter (and definitely shorter)?



    It should pretty much say: If value in one specific cell is not empty and at the same time is different from any value in cells from X32 to X47 → do something



    Thanks a lot!



    For i = 4 To 69
    If Range(SO_COLUMN & i) <> "" _
    And Range(SO_COLUMN & i) <> Range("X32") And Range(SO_COLUMN & i) <> Range("X33") And Range(SO_COLUMN & i) <> Range("X34") And Range(SO_COLUMN & i) <> Range("X35") _
    And Range(SO_COLUMN & i) <> Range("X36") And Range(SO_COLUMN & i) <> Range("X37") And Range(SO_COLUMN & i) <> Range("X38") And Range(SO_COLUMN & i) <> Range("X39") _
    And Range(SO_COLUMN & i) <> Range("X40") And Range(SO_COLUMN & i) <> Range("X41") And Range(SO_COLUMN & i) <> Range("X42") And Range(SO_COLUMN & i) <> Range("X43") _
    And Range(SO_COLUMN & i) <> Range("X44") And Range(SO_COLUMN & i) <> Range("X45") And Range(SO_COLUMN & i) <> Range("X46") And Range(SO_COLUMN & i) <> Range("X47") Then

    "DO SOMETHING"
    End If
    Next i









    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      could someone help, how to rewrite folowing condition somehow smarter (and definitely shorter)?



      It should pretty much say: If value in one specific cell is not empty and at the same time is different from any value in cells from X32 to X47 → do something



      Thanks a lot!



      For i = 4 To 69
      If Range(SO_COLUMN & i) <> "" _
      And Range(SO_COLUMN & i) <> Range("X32") And Range(SO_COLUMN & i) <> Range("X33") And Range(SO_COLUMN & i) <> Range("X34") And Range(SO_COLUMN & i) <> Range("X35") _
      And Range(SO_COLUMN & i) <> Range("X36") And Range(SO_COLUMN & i) <> Range("X37") And Range(SO_COLUMN & i) <> Range("X38") And Range(SO_COLUMN & i) <> Range("X39") _
      And Range(SO_COLUMN & i) <> Range("X40") And Range(SO_COLUMN & i) <> Range("X41") And Range(SO_COLUMN & i) <> Range("X42") And Range(SO_COLUMN & i) <> Range("X43") _
      And Range(SO_COLUMN & i) <> Range("X44") And Range(SO_COLUMN & i) <> Range("X45") And Range(SO_COLUMN & i) <> Range("X46") And Range(SO_COLUMN & i) <> Range("X47") Then

      "DO SOMETHING"
      End If
      Next i









      share|improve this question













      could someone help, how to rewrite folowing condition somehow smarter (and definitely shorter)?



      It should pretty much say: If value in one specific cell is not empty and at the same time is different from any value in cells from X32 to X47 → do something



      Thanks a lot!



      For i = 4 To 69
      If Range(SO_COLUMN & i) <> "" _
      And Range(SO_COLUMN & i) <> Range("X32") And Range(SO_COLUMN & i) <> Range("X33") And Range(SO_COLUMN & i) <> Range("X34") And Range(SO_COLUMN & i) <> Range("X35") _
      And Range(SO_COLUMN & i) <> Range("X36") And Range(SO_COLUMN & i) <> Range("X37") And Range(SO_COLUMN & i) <> Range("X38") And Range(SO_COLUMN & i) <> Range("X39") _
      And Range(SO_COLUMN & i) <> Range("X40") And Range(SO_COLUMN & i) <> Range("X41") And Range(SO_COLUMN & i) <> Range("X42") And Range(SO_COLUMN & i) <> Range("X43") _
      And Range(SO_COLUMN & i) <> Range("X44") And Range(SO_COLUMN & i) <> Range("X45") And Range(SO_COLUMN & i) <> Range("X46") And Range(SO_COLUMN & i) <> Range("X47") Then

      "DO SOMETHING"
      End If
      Next i






      excel vba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 5:51









      Vratislav Vašina

      124




      124






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Maybe something like:



          With ThisWorkbook.Worksheets("yourSheetName")
          For i = 4 To 69
          If Not IsEmpty(.Range(SO_COLUMN & i)) And IsError(Application.Match(.Range(SO_COLUMN & i), .Range("X32:X47"), 0)) Then
          'do something
          End If
          Next
          End With





          share|improve this answer
















          • 2




            Well said. And good job for almost hitting 25k!!! :D
            – K.Dᴀᴠɪs
            Nov 11 at 6:18










          • @K.Dᴀᴠɪs That is kind. Thank you.
            – QHarr
            Nov 11 at 6:19










          • The "IsEmpty" part does not work, so i replaced it with original Range(SO_COLUMN & i) <> "" and the rest works perfectly. Thank you very much :)
            – Vratislav Vašina
            Nov 11 at 7:45










          • you are welcome. Do you perhaps have formulas in the cells? You can use <> vbNullString for faster comparisons than "".
            – QHarr
            Nov 11 at 7:46







          • 1




            You are perfectly right, I do have formulas in there. I adjust as you recomend and it works smoothly :) Thank you again.
            – Vratislav Vašina
            Nov 11 at 7:56










          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%2f53246205%2fhow-to-compare-value-with-other-values-in-range-easily%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Maybe something like:



          With ThisWorkbook.Worksheets("yourSheetName")
          For i = 4 To 69
          If Not IsEmpty(.Range(SO_COLUMN & i)) And IsError(Application.Match(.Range(SO_COLUMN & i), .Range("X32:X47"), 0)) Then
          'do something
          End If
          Next
          End With





          share|improve this answer
















          • 2




            Well said. And good job for almost hitting 25k!!! :D
            – K.Dᴀᴠɪs
            Nov 11 at 6:18










          • @K.Dᴀᴠɪs That is kind. Thank you.
            – QHarr
            Nov 11 at 6:19










          • The "IsEmpty" part does not work, so i replaced it with original Range(SO_COLUMN & i) <> "" and the rest works perfectly. Thank you very much :)
            – Vratislav Vašina
            Nov 11 at 7:45










          • you are welcome. Do you perhaps have formulas in the cells? You can use <> vbNullString for faster comparisons than "".
            – QHarr
            Nov 11 at 7:46







          • 1




            You are perfectly right, I do have formulas in there. I adjust as you recomend and it works smoothly :) Thank you again.
            – Vratislav Vašina
            Nov 11 at 7:56














          up vote
          2
          down vote



          accepted










          Maybe something like:



          With ThisWorkbook.Worksheets("yourSheetName")
          For i = 4 To 69
          If Not IsEmpty(.Range(SO_COLUMN & i)) And IsError(Application.Match(.Range(SO_COLUMN & i), .Range("X32:X47"), 0)) Then
          'do something
          End If
          Next
          End With





          share|improve this answer
















          • 2




            Well said. And good job for almost hitting 25k!!! :D
            – K.Dᴀᴠɪs
            Nov 11 at 6:18










          • @K.Dᴀᴠɪs That is kind. Thank you.
            – QHarr
            Nov 11 at 6:19










          • The "IsEmpty" part does not work, so i replaced it with original Range(SO_COLUMN & i) <> "" and the rest works perfectly. Thank you very much :)
            – Vratislav Vašina
            Nov 11 at 7:45










          • you are welcome. Do you perhaps have formulas in the cells? You can use <> vbNullString for faster comparisons than "".
            – QHarr
            Nov 11 at 7:46







          • 1




            You are perfectly right, I do have formulas in there. I adjust as you recomend and it works smoothly :) Thank you again.
            – Vratislav Vašina
            Nov 11 at 7:56












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Maybe something like:



          With ThisWorkbook.Worksheets("yourSheetName")
          For i = 4 To 69
          If Not IsEmpty(.Range(SO_COLUMN & i)) And IsError(Application.Match(.Range(SO_COLUMN & i), .Range("X32:X47"), 0)) Then
          'do something
          End If
          Next
          End With





          share|improve this answer












          Maybe something like:



          With ThisWorkbook.Worksheets("yourSheetName")
          For i = 4 To 69
          If Not IsEmpty(.Range(SO_COLUMN & i)) And IsError(Application.Match(.Range(SO_COLUMN & i), .Range("X32:X47"), 0)) Then
          'do something
          End If
          Next
          End With






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 6:16









          QHarr

          26.9k81839




          26.9k81839







          • 2




            Well said. And good job for almost hitting 25k!!! :D
            – K.Dᴀᴠɪs
            Nov 11 at 6:18










          • @K.Dᴀᴠɪs That is kind. Thank you.
            – QHarr
            Nov 11 at 6:19










          • The "IsEmpty" part does not work, so i replaced it with original Range(SO_COLUMN & i) <> "" and the rest works perfectly. Thank you very much :)
            – Vratislav Vašina
            Nov 11 at 7:45










          • you are welcome. Do you perhaps have formulas in the cells? You can use <> vbNullString for faster comparisons than "".
            – QHarr
            Nov 11 at 7:46







          • 1




            You are perfectly right, I do have formulas in there. I adjust as you recomend and it works smoothly :) Thank you again.
            – Vratislav Vašina
            Nov 11 at 7:56












          • 2




            Well said. And good job for almost hitting 25k!!! :D
            – K.Dᴀᴠɪs
            Nov 11 at 6:18










          • @K.Dᴀᴠɪs That is kind. Thank you.
            – QHarr
            Nov 11 at 6:19










          • The "IsEmpty" part does not work, so i replaced it with original Range(SO_COLUMN & i) <> "" and the rest works perfectly. Thank you very much :)
            – Vratislav Vašina
            Nov 11 at 7:45










          • you are welcome. Do you perhaps have formulas in the cells? You can use <> vbNullString for faster comparisons than "".
            – QHarr
            Nov 11 at 7:46







          • 1




            You are perfectly right, I do have formulas in there. I adjust as you recomend and it works smoothly :) Thank you again.
            – Vratislav Vašina
            Nov 11 at 7:56







          2




          2




          Well said. And good job for almost hitting 25k!!! :D
          – K.Dᴀᴠɪs
          Nov 11 at 6:18




          Well said. And good job for almost hitting 25k!!! :D
          – K.Dᴀᴠɪs
          Nov 11 at 6:18












          @K.Dᴀᴠɪs That is kind. Thank you.
          – QHarr
          Nov 11 at 6:19




          @K.Dᴀᴠɪs That is kind. Thank you.
          – QHarr
          Nov 11 at 6:19












          The "IsEmpty" part does not work, so i replaced it with original Range(SO_COLUMN & i) <> "" and the rest works perfectly. Thank you very much :)
          – Vratislav Vašina
          Nov 11 at 7:45




          The "IsEmpty" part does not work, so i replaced it with original Range(SO_COLUMN & i) <> "" and the rest works perfectly. Thank you very much :)
          – Vratislav Vašina
          Nov 11 at 7:45












          you are welcome. Do you perhaps have formulas in the cells? You can use <> vbNullString for faster comparisons than "".
          – QHarr
          Nov 11 at 7:46





          you are welcome. Do you perhaps have formulas in the cells? You can use <> vbNullString for faster comparisons than "".
          – QHarr
          Nov 11 at 7:46





          1




          1




          You are perfectly right, I do have formulas in there. I adjust as you recomend and it works smoothly :) Thank you again.
          – Vratislav Vašina
          Nov 11 at 7:56




          You are perfectly right, I do have formulas in there. I adjust as you recomend and it works smoothly :) Thank you again.
          – Vratislav Vašina
          Nov 11 at 7:56

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53246205%2fhow-to-compare-value-with-other-values-in-range-easily%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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3