VBA conditional search and marking










0














The aim is to search in column A for the word "XXX". When the word is found in that column, the next cell should match the word "C".If it doesn't, carry on with the search for "XXX" in the next row, etc. The issue in my code is that it marks all "C"s. In other words, there is no conditional search where XXX=C.



enter image description here



For Each cell In ws.Range("A1:A20").SpecialCells(xlCellTypeConstants)
Select Case cell.Value2
Case "XXX"
col = RGB(202, 225, 255)
Case Else
col = 0
End Select


If col > 0 Then
wb.Activate
cell.Interior.Color = col
For Each cell2 In cell.Offset(, 2).Resize(1).SpecialCells (xlCellTypeConstants)
res = Switch(cell2.Value = "C", vbGreen)
If Not IsNull(res) Then Intersect(Range("B:B, J:J, L:L, N:N, Q:Q"), Rows(cell2.Row)).Interior.Color = CLng(res) '
Next
End If
Next









share|improve this question


























    0














    The aim is to search in column A for the word "XXX". When the word is found in that column, the next cell should match the word "C".If it doesn't, carry on with the search for "XXX" in the next row, etc. The issue in my code is that it marks all "C"s. In other words, there is no conditional search where XXX=C.



    enter image description here



    For Each cell In ws.Range("A1:A20").SpecialCells(xlCellTypeConstants)
    Select Case cell.Value2
    Case "XXX"
    col = RGB(202, 225, 255)
    Case Else
    col = 0
    End Select


    If col > 0 Then
    wb.Activate
    cell.Interior.Color = col
    For Each cell2 In cell.Offset(, 2).Resize(1).SpecialCells (xlCellTypeConstants)
    res = Switch(cell2.Value = "C", vbGreen)
    If Not IsNull(res) Then Intersect(Range("B:B, J:J, L:L, N:N, Q:Q"), Rows(cell2.Row)).Interior.Color = CLng(res) '
    Next
    End If
    Next









    share|improve this question
























      0












      0








      0







      The aim is to search in column A for the word "XXX". When the word is found in that column, the next cell should match the word "C".If it doesn't, carry on with the search for "XXX" in the next row, etc. The issue in my code is that it marks all "C"s. In other words, there is no conditional search where XXX=C.



      enter image description here



      For Each cell In ws.Range("A1:A20").SpecialCells(xlCellTypeConstants)
      Select Case cell.Value2
      Case "XXX"
      col = RGB(202, 225, 255)
      Case Else
      col = 0
      End Select


      If col > 0 Then
      wb.Activate
      cell.Interior.Color = col
      For Each cell2 In cell.Offset(, 2).Resize(1).SpecialCells (xlCellTypeConstants)
      res = Switch(cell2.Value = "C", vbGreen)
      If Not IsNull(res) Then Intersect(Range("B:B, J:J, L:L, N:N, Q:Q"), Rows(cell2.Row)).Interior.Color = CLng(res) '
      Next
      End If
      Next









      share|improve this question













      The aim is to search in column A for the word "XXX". When the word is found in that column, the next cell should match the word "C".If it doesn't, carry on with the search for "XXX" in the next row, etc. The issue in my code is that it marks all "C"s. In other words, there is no conditional search where XXX=C.



      enter image description here



      For Each cell In ws.Range("A1:A20").SpecialCells(xlCellTypeConstants)
      Select Case cell.Value2
      Case "XXX"
      col = RGB(202, 225, 255)
      Case Else
      col = 0
      End Select


      If col > 0 Then
      wb.Activate
      cell.Interior.Color = col
      For Each cell2 In cell.Offset(, 2).Resize(1).SpecialCells (xlCellTypeConstants)
      res = Switch(cell2.Value = "C", vbGreen)
      If Not IsNull(res) Then Intersect(Range("B:B, J:J, L:L, N:N, Q:Q"), Rows(cell2.Row)).Interior.Color = CLng(res) '
      Next
      End If
      Next






      search conditional cell background-color






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 14:05









      Yavuz Topal

      7610




      7610






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Edited: See Below



          Slightly unsure what you're asking but I think solution is below.
          I have used much simpler code but it should be adequate.



          For xJ = 1 to 200
          If Range("A" & xJ).Value = "XXX" then
          Range("A" & xJ).Interior.Color = RGB(202, 225, 255)
          If Range("B" & xJ).Value = "C" Then
          Range("B" & xJ).Interior.Color = RGB(57, 225, 20)
          End If
          End if
          Next xJ





          share|improve this answer






















          • Thx. It almost fits the purpose.The aim is to mark all "XXX" in one collor and all "C"s in the next cell of "XXX" in green only when XXX and "C" are next to each other.
            – Yavuz Topal
            Nov 12 at 20:43










          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%2f53263829%2fvba-conditional-search-and-marking%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









          1














          Edited: See Below



          Slightly unsure what you're asking but I think solution is below.
          I have used much simpler code but it should be adequate.



          For xJ = 1 to 200
          If Range("A" & xJ).Value = "XXX" then
          Range("A" & xJ).Interior.Color = RGB(202, 225, 255)
          If Range("B" & xJ).Value = "C" Then
          Range("B" & xJ).Interior.Color = RGB(57, 225, 20)
          End If
          End if
          Next xJ





          share|improve this answer






















          • Thx. It almost fits the purpose.The aim is to mark all "XXX" in one collor and all "C"s in the next cell of "XXX" in green only when XXX and "C" are next to each other.
            – Yavuz Topal
            Nov 12 at 20:43















          1














          Edited: See Below



          Slightly unsure what you're asking but I think solution is below.
          I have used much simpler code but it should be adequate.



          For xJ = 1 to 200
          If Range("A" & xJ).Value = "XXX" then
          Range("A" & xJ).Interior.Color = RGB(202, 225, 255)
          If Range("B" & xJ).Value = "C" Then
          Range("B" & xJ).Interior.Color = RGB(57, 225, 20)
          End If
          End if
          Next xJ





          share|improve this answer






















          • Thx. It almost fits the purpose.The aim is to mark all "XXX" in one collor and all "C"s in the next cell of "XXX" in green only when XXX and "C" are next to each other.
            – Yavuz Topal
            Nov 12 at 20:43













          1












          1








          1






          Edited: See Below



          Slightly unsure what you're asking but I think solution is below.
          I have used much simpler code but it should be adequate.



          For xJ = 1 to 200
          If Range("A" & xJ).Value = "XXX" then
          Range("A" & xJ).Interior.Color = RGB(202, 225, 255)
          If Range("B" & xJ).Value = "C" Then
          Range("B" & xJ).Interior.Color = RGB(57, 225, 20)
          End If
          End if
          Next xJ





          share|improve this answer














          Edited: See Below



          Slightly unsure what you're asking but I think solution is below.
          I have used much simpler code but it should be adequate.



          For xJ = 1 to 200
          If Range("A" & xJ).Value = "XXX" then
          Range("A" & xJ).Interior.Color = RGB(202, 225, 255)
          If Range("B" & xJ).Value = "C" Then
          Range("B" & xJ).Interior.Color = RGB(57, 225, 20)
          End If
          End if
          Next xJ






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 23:07

























          answered Nov 12 at 17:07









          Conor Travers

          262




          262











          • Thx. It almost fits the purpose.The aim is to mark all "XXX" in one collor and all "C"s in the next cell of "XXX" in green only when XXX and "C" are next to each other.
            – Yavuz Topal
            Nov 12 at 20:43
















          • Thx. It almost fits the purpose.The aim is to mark all "XXX" in one collor and all "C"s in the next cell of "XXX" in green only when XXX and "C" are next to each other.
            – Yavuz Topal
            Nov 12 at 20:43















          Thx. It almost fits the purpose.The aim is to mark all "XXX" in one collor and all "C"s in the next cell of "XXX" in green only when XXX and "C" are next to each other.
          – Yavuz Topal
          Nov 12 at 20:43




          Thx. It almost fits the purpose.The aim is to mark all "XXX" in one collor and all "C"s in the next cell of "XXX" in green only when XXX and "C" are next to each other.
          – Yavuz Topal
          Nov 12 at 20:43

















          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%2f53263829%2fvba-conditional-search-and-marking%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