Worksheet_Change trouble working on multiple columns












-1















i have trouble getting this code to work on the last to columns (T and U)
what can i do to change tis?



code:



   Private Sub Worksheet_ChangeS(ByVal Target As Range) 'column s, structure
If Intersect(Target, Range("S:S")) Is Nothing Then Exit Sub
Dim foundVal As Range
Set foundVal = Sheets("Dropdown").Range("A:A").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
If Not foundVal Is Nothing Then
Target = foundVal.Offset(0, 1)
End If
End Sub

Private Sub Worksheet_ChangeT(ByVal Target As Range) 'column t, component
If Intersect(Target, Range("T:T")) Is Nothing Then Exit Sub
Dim foundVal As Range
Set foundVal = Sheets("Dropdown").Range("D:D").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
If Not foundVal Is Nothing Then
Target = foundVal.Offset(0, 1)
End If
End Sub

Private Sub Worksheet_ChangeU(ByVal Target As Range) 'column U, parameter
If Intersect(Target, Range("U:U")) Is Nothing Then Exit Sub
Dim foundVal As Range
Set foundVal = Sheets("Dropdown").Range("I:I").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
If Not foundVal Is Nothing Then
Target = foundVal.Offset(0, 1)
End If
End Sub


Thanks in advance










share|improve this question



























    -1















    i have trouble getting this code to work on the last to columns (T and U)
    what can i do to change tis?



    code:



       Private Sub Worksheet_ChangeS(ByVal Target As Range) 'column s, structure
    If Intersect(Target, Range("S:S")) Is Nothing Then Exit Sub
    Dim foundVal As Range
    Set foundVal = Sheets("Dropdown").Range("A:A").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
    If Not foundVal Is Nothing Then
    Target = foundVal.Offset(0, 1)
    End If
    End Sub

    Private Sub Worksheet_ChangeT(ByVal Target As Range) 'column t, component
    If Intersect(Target, Range("T:T")) Is Nothing Then Exit Sub
    Dim foundVal As Range
    Set foundVal = Sheets("Dropdown").Range("D:D").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
    If Not foundVal Is Nothing Then
    Target = foundVal.Offset(0, 1)
    End If
    End Sub

    Private Sub Worksheet_ChangeU(ByVal Target As Range) 'column U, parameter
    If Intersect(Target, Range("U:U")) Is Nothing Then Exit Sub
    Dim foundVal As Range
    Set foundVal = Sheets("Dropdown").Range("I:I").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
    If Not foundVal Is Nothing Then
    Target = foundVal.Offset(0, 1)
    End If
    End Sub


    Thanks in advance










    share|improve this question

























      -1












      -1








      -1








      i have trouble getting this code to work on the last to columns (T and U)
      what can i do to change tis?



      code:



         Private Sub Worksheet_ChangeS(ByVal Target As Range) 'column s, structure
      If Intersect(Target, Range("S:S")) Is Nothing Then Exit Sub
      Dim foundVal As Range
      Set foundVal = Sheets("Dropdown").Range("A:A").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
      If Not foundVal Is Nothing Then
      Target = foundVal.Offset(0, 1)
      End If
      End Sub

      Private Sub Worksheet_ChangeT(ByVal Target As Range) 'column t, component
      If Intersect(Target, Range("T:T")) Is Nothing Then Exit Sub
      Dim foundVal As Range
      Set foundVal = Sheets("Dropdown").Range("D:D").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
      If Not foundVal Is Nothing Then
      Target = foundVal.Offset(0, 1)
      End If
      End Sub

      Private Sub Worksheet_ChangeU(ByVal Target As Range) 'column U, parameter
      If Intersect(Target, Range("U:U")) Is Nothing Then Exit Sub
      Dim foundVal As Range
      Set foundVal = Sheets("Dropdown").Range("I:I").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
      If Not foundVal Is Nothing Then
      Target = foundVal.Offset(0, 1)
      End If
      End Sub


      Thanks in advance










      share|improve this question














      i have trouble getting this code to work on the last to columns (T and U)
      what can i do to change tis?



      code:



         Private Sub Worksheet_ChangeS(ByVal Target As Range) 'column s, structure
      If Intersect(Target, Range("S:S")) Is Nothing Then Exit Sub
      Dim foundVal As Range
      Set foundVal = Sheets("Dropdown").Range("A:A").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
      If Not foundVal Is Nothing Then
      Target = foundVal.Offset(0, 1)
      End If
      End Sub

      Private Sub Worksheet_ChangeT(ByVal Target As Range) 'column t, component
      If Intersect(Target, Range("T:T")) Is Nothing Then Exit Sub
      Dim foundVal As Range
      Set foundVal = Sheets("Dropdown").Range("D:D").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
      If Not foundVal Is Nothing Then
      Target = foundVal.Offset(0, 1)
      End If
      End Sub

      Private Sub Worksheet_ChangeU(ByVal Target As Range) 'column U, parameter
      If Intersect(Target, Range("U:U")) Is Nothing Then Exit Sub
      Dim foundVal As Range
      Set foundVal = Sheets("Dropdown").Range("I:I").Find(Target, LookIn:=xlValues, lookat:=xlWhole)
      If Not foundVal Is Nothing Then
      Target = foundVal.Offset(0, 1)
      End If
      End Sub


      Thanks in advance







      excel vba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 10:04









      Susanne LysgaardSusanne Lysgaard

      102




      102
























          1 Answer
          1






          active

          oldest

          votes


















          2














          There's only one Worksheet_Change event that fires whenever a cell is changed.



          Private Sub Worksheet_ChangeS(ByVal Target As Range) would have to be "manually" called from the Worksheet_Change event to work as it's a user defined procedure.



          You should delete those three procedures and using something along the lines of:



          Private Sub Worksheet_Change(ByVal Target As Range)

          Dim lCol As Long
          Dim foundVal As Range

          Application.EnableEvents = False

          If Not Intersect(Target, Range("S:U")) Is Nothing Then

          If Target.Cells.Count = 1 Then 'Check a single cell is changing.
          Select Case Target.Column
          Case 19 'col S
          lCol = 1
          Case 20 'col T
          lCol = 4
          Case 21 'col U
          lCol = 9
          End Select

          Set foundVal = Worksheets("DropDown").Columns(lCol).Find( _
          Target.Value, LookIn:=xlValues, LookAt:=xlWhole)

          If Not foundVal Is Nothing Then
          Target = foundVal.Offset(, 1)
          End If
          End If
          End If

          Application.EnableEvents = True

          End Sub


          Although looking at the range you then try and use FIND on you should be able to offset from the Target column and do the find in a single statement rather than three.



          Edit:

          I've updated the code to include EnableEvents. At the end of your code you change the value of Target which would cause the Worksheet_Change event to fire again. EnableEvents stops that from happening.



          Edit 2:

          I've updated the code to use a single FIND. The Select Case gives the column to search in the FIND command.



          Note:

          If your code crashes for any reason you may need to open the immediate window (Ctrl+G) and type in Application.EnableEvents = True as this does not reset when the code stops - you'll just notice that all your event code stops firing.






          share|improve this answer





















          • 1





            Thanks, works perfectly :)

            – Susanne Lysgaard
            Nov 14 '18 at 11:19











          • When deleting a row in the sheet, it gives an 1004 error pointing at the [set foundval]area.. Can you explain why?

            – Susanne Lysgaard
            Nov 26 '18 at 11:11













          • The only error that jumps out at me would be that by deleting an entire row, or column would mean you're working on more than one cell - the FIND command excepts a single value. I've updated the code to make sure the Change event only fires when a single cell is changed.

            – Darren Bartrup-Cook
            Nov 26 '18 at 11:34






          • 1





            Thanks! :D it works! :D

            – Susanne Lysgaard
            Nov 26 '18 at 11:41











          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%2f53297559%2fworksheet-change-trouble-working-on-multiple-columns%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









          2














          There's only one Worksheet_Change event that fires whenever a cell is changed.



          Private Sub Worksheet_ChangeS(ByVal Target As Range) would have to be "manually" called from the Worksheet_Change event to work as it's a user defined procedure.



          You should delete those three procedures and using something along the lines of:



          Private Sub Worksheet_Change(ByVal Target As Range)

          Dim lCol As Long
          Dim foundVal As Range

          Application.EnableEvents = False

          If Not Intersect(Target, Range("S:U")) Is Nothing Then

          If Target.Cells.Count = 1 Then 'Check a single cell is changing.
          Select Case Target.Column
          Case 19 'col S
          lCol = 1
          Case 20 'col T
          lCol = 4
          Case 21 'col U
          lCol = 9
          End Select

          Set foundVal = Worksheets("DropDown").Columns(lCol).Find( _
          Target.Value, LookIn:=xlValues, LookAt:=xlWhole)

          If Not foundVal Is Nothing Then
          Target = foundVal.Offset(, 1)
          End If
          End If
          End If

          Application.EnableEvents = True

          End Sub


          Although looking at the range you then try and use FIND on you should be able to offset from the Target column and do the find in a single statement rather than three.



          Edit:

          I've updated the code to include EnableEvents. At the end of your code you change the value of Target which would cause the Worksheet_Change event to fire again. EnableEvents stops that from happening.



          Edit 2:

          I've updated the code to use a single FIND. The Select Case gives the column to search in the FIND command.



          Note:

          If your code crashes for any reason you may need to open the immediate window (Ctrl+G) and type in Application.EnableEvents = True as this does not reset when the code stops - you'll just notice that all your event code stops firing.






          share|improve this answer





















          • 1





            Thanks, works perfectly :)

            – Susanne Lysgaard
            Nov 14 '18 at 11:19











          • When deleting a row in the sheet, it gives an 1004 error pointing at the [set foundval]area.. Can you explain why?

            – Susanne Lysgaard
            Nov 26 '18 at 11:11













          • The only error that jumps out at me would be that by deleting an entire row, or column would mean you're working on more than one cell - the FIND command excepts a single value. I've updated the code to make sure the Change event only fires when a single cell is changed.

            – Darren Bartrup-Cook
            Nov 26 '18 at 11:34






          • 1





            Thanks! :D it works! :D

            – Susanne Lysgaard
            Nov 26 '18 at 11:41
















          2














          There's only one Worksheet_Change event that fires whenever a cell is changed.



          Private Sub Worksheet_ChangeS(ByVal Target As Range) would have to be "manually" called from the Worksheet_Change event to work as it's a user defined procedure.



          You should delete those three procedures and using something along the lines of:



          Private Sub Worksheet_Change(ByVal Target As Range)

          Dim lCol As Long
          Dim foundVal As Range

          Application.EnableEvents = False

          If Not Intersect(Target, Range("S:U")) Is Nothing Then

          If Target.Cells.Count = 1 Then 'Check a single cell is changing.
          Select Case Target.Column
          Case 19 'col S
          lCol = 1
          Case 20 'col T
          lCol = 4
          Case 21 'col U
          lCol = 9
          End Select

          Set foundVal = Worksheets("DropDown").Columns(lCol).Find( _
          Target.Value, LookIn:=xlValues, LookAt:=xlWhole)

          If Not foundVal Is Nothing Then
          Target = foundVal.Offset(, 1)
          End If
          End If
          End If

          Application.EnableEvents = True

          End Sub


          Although looking at the range you then try and use FIND on you should be able to offset from the Target column and do the find in a single statement rather than three.



          Edit:

          I've updated the code to include EnableEvents. At the end of your code you change the value of Target which would cause the Worksheet_Change event to fire again. EnableEvents stops that from happening.



          Edit 2:

          I've updated the code to use a single FIND. The Select Case gives the column to search in the FIND command.



          Note:

          If your code crashes for any reason you may need to open the immediate window (Ctrl+G) and type in Application.EnableEvents = True as this does not reset when the code stops - you'll just notice that all your event code stops firing.






          share|improve this answer





















          • 1





            Thanks, works perfectly :)

            – Susanne Lysgaard
            Nov 14 '18 at 11:19











          • When deleting a row in the sheet, it gives an 1004 error pointing at the [set foundval]area.. Can you explain why?

            – Susanne Lysgaard
            Nov 26 '18 at 11:11













          • The only error that jumps out at me would be that by deleting an entire row, or column would mean you're working on more than one cell - the FIND command excepts a single value. I've updated the code to make sure the Change event only fires when a single cell is changed.

            – Darren Bartrup-Cook
            Nov 26 '18 at 11:34






          • 1





            Thanks! :D it works! :D

            – Susanne Lysgaard
            Nov 26 '18 at 11:41














          2












          2








          2







          There's only one Worksheet_Change event that fires whenever a cell is changed.



          Private Sub Worksheet_ChangeS(ByVal Target As Range) would have to be "manually" called from the Worksheet_Change event to work as it's a user defined procedure.



          You should delete those three procedures and using something along the lines of:



          Private Sub Worksheet_Change(ByVal Target As Range)

          Dim lCol As Long
          Dim foundVal As Range

          Application.EnableEvents = False

          If Not Intersect(Target, Range("S:U")) Is Nothing Then

          If Target.Cells.Count = 1 Then 'Check a single cell is changing.
          Select Case Target.Column
          Case 19 'col S
          lCol = 1
          Case 20 'col T
          lCol = 4
          Case 21 'col U
          lCol = 9
          End Select

          Set foundVal = Worksheets("DropDown").Columns(lCol).Find( _
          Target.Value, LookIn:=xlValues, LookAt:=xlWhole)

          If Not foundVal Is Nothing Then
          Target = foundVal.Offset(, 1)
          End If
          End If
          End If

          Application.EnableEvents = True

          End Sub


          Although looking at the range you then try and use FIND on you should be able to offset from the Target column and do the find in a single statement rather than three.



          Edit:

          I've updated the code to include EnableEvents. At the end of your code you change the value of Target which would cause the Worksheet_Change event to fire again. EnableEvents stops that from happening.



          Edit 2:

          I've updated the code to use a single FIND. The Select Case gives the column to search in the FIND command.



          Note:

          If your code crashes for any reason you may need to open the immediate window (Ctrl+G) and type in Application.EnableEvents = True as this does not reset when the code stops - you'll just notice that all your event code stops firing.






          share|improve this answer















          There's only one Worksheet_Change event that fires whenever a cell is changed.



          Private Sub Worksheet_ChangeS(ByVal Target As Range) would have to be "manually" called from the Worksheet_Change event to work as it's a user defined procedure.



          You should delete those three procedures and using something along the lines of:



          Private Sub Worksheet_Change(ByVal Target As Range)

          Dim lCol As Long
          Dim foundVal As Range

          Application.EnableEvents = False

          If Not Intersect(Target, Range("S:U")) Is Nothing Then

          If Target.Cells.Count = 1 Then 'Check a single cell is changing.
          Select Case Target.Column
          Case 19 'col S
          lCol = 1
          Case 20 'col T
          lCol = 4
          Case 21 'col U
          lCol = 9
          End Select

          Set foundVal = Worksheets("DropDown").Columns(lCol).Find( _
          Target.Value, LookIn:=xlValues, LookAt:=xlWhole)

          If Not foundVal Is Nothing Then
          Target = foundVal.Offset(, 1)
          End If
          End If
          End If

          Application.EnableEvents = True

          End Sub


          Although looking at the range you then try and use FIND on you should be able to offset from the Target column and do the find in a single statement rather than three.



          Edit:

          I've updated the code to include EnableEvents. At the end of your code you change the value of Target which would cause the Worksheet_Change event to fire again. EnableEvents stops that from happening.



          Edit 2:

          I've updated the code to use a single FIND. The Select Case gives the column to search in the FIND command.



          Note:

          If your code crashes for any reason you may need to open the immediate window (Ctrl+G) and type in Application.EnableEvents = True as this does not reset when the code stops - you'll just notice that all your event code stops firing.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 11:35

























          answered Nov 14 '18 at 10:22









          Darren Bartrup-CookDarren Bartrup-Cook

          13.9k11432




          13.9k11432








          • 1





            Thanks, works perfectly :)

            – Susanne Lysgaard
            Nov 14 '18 at 11:19











          • When deleting a row in the sheet, it gives an 1004 error pointing at the [set foundval]area.. Can you explain why?

            – Susanne Lysgaard
            Nov 26 '18 at 11:11













          • The only error that jumps out at me would be that by deleting an entire row, or column would mean you're working on more than one cell - the FIND command excepts a single value. I've updated the code to make sure the Change event only fires when a single cell is changed.

            – Darren Bartrup-Cook
            Nov 26 '18 at 11:34






          • 1





            Thanks! :D it works! :D

            – Susanne Lysgaard
            Nov 26 '18 at 11:41














          • 1





            Thanks, works perfectly :)

            – Susanne Lysgaard
            Nov 14 '18 at 11:19











          • When deleting a row in the sheet, it gives an 1004 error pointing at the [set foundval]area.. Can you explain why?

            – Susanne Lysgaard
            Nov 26 '18 at 11:11













          • The only error that jumps out at me would be that by deleting an entire row, or column would mean you're working on more than one cell - the FIND command excepts a single value. I've updated the code to make sure the Change event only fires when a single cell is changed.

            – Darren Bartrup-Cook
            Nov 26 '18 at 11:34






          • 1





            Thanks! :D it works! :D

            – Susanne Lysgaard
            Nov 26 '18 at 11:41








          1




          1





          Thanks, works perfectly :)

          – Susanne Lysgaard
          Nov 14 '18 at 11:19





          Thanks, works perfectly :)

          – Susanne Lysgaard
          Nov 14 '18 at 11:19













          When deleting a row in the sheet, it gives an 1004 error pointing at the [set foundval]area.. Can you explain why?

          – Susanne Lysgaard
          Nov 26 '18 at 11:11







          When deleting a row in the sheet, it gives an 1004 error pointing at the [set foundval]area.. Can you explain why?

          – Susanne Lysgaard
          Nov 26 '18 at 11:11















          The only error that jumps out at me would be that by deleting an entire row, or column would mean you're working on more than one cell - the FIND command excepts a single value. I've updated the code to make sure the Change event only fires when a single cell is changed.

          – Darren Bartrup-Cook
          Nov 26 '18 at 11:34





          The only error that jumps out at me would be that by deleting an entire row, or column would mean you're working on more than one cell - the FIND command excepts a single value. I've updated the code to make sure the Change event only fires when a single cell is changed.

          – Darren Bartrup-Cook
          Nov 26 '18 at 11:34




          1




          1





          Thanks! :D it works! :D

          – Susanne Lysgaard
          Nov 26 '18 at 11:41





          Thanks! :D it works! :D

          – Susanne Lysgaard
          Nov 26 '18 at 11:41


















          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%2f53297559%2fworksheet-change-trouble-working-on-multiple-columns%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python