Macro to show most recently used cell










0















I have a working macro that hides/unhides specific columns based off specific values in Column B. I also want to add another trigger that takes the user to the most recently used row. Below is my attempt.



Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "A"
Columns("B:BP").EntireColumn.Hidden = False
Columns("H:BL").EntireColumn.Hidden = True
Case "B"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:G").EntireColumn.Hidden = True
Columns("P:BP").EntireColumn.Hidden = True
End Select
Next t

End If

safe_exit:
Application.EnableEvents = True

'Go to last cell in column B
With ActiveSheet
Range("B5").Select
Selection.End(xlDown).Select
End With

End Sub









share|improve this question




























    0















    I have a working macro that hides/unhides specific columns based off specific values in Column B. I also want to add another trigger that takes the user to the most recently used row. Below is my attempt.



    Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("B:B")) Is Nothing Then
    On Error GoTo safe_exit
    Application.EnableEvents = False
    Dim t As Range
    For Each t In Intersect(Target, Range("B:B"))
    Select Case (t.Value)
    Case "A"
    Columns("B:BP").EntireColumn.Hidden = False
    Columns("H:BL").EntireColumn.Hidden = True
    Case "B"
    Columns("B:BP").EntireColumn.Hidden = False
    Columns("F:G").EntireColumn.Hidden = True
    Columns("P:BP").EntireColumn.Hidden = True
    End Select
    Next t

    End If

    safe_exit:
    Application.EnableEvents = True

    'Go to last cell in column B
    With ActiveSheet
    Range("B5").Select
    Selection.End(xlDown).Select
    End With

    End Sub









    share|improve this question


























      0












      0








      0








      I have a working macro that hides/unhides specific columns based off specific values in Column B. I also want to add another trigger that takes the user to the most recently used row. Below is my attempt.



      Private Sub Worksheet_Change(ByVal Target As Range)

      If Not Intersect(Target, Range("B:B")) Is Nothing Then
      On Error GoTo safe_exit
      Application.EnableEvents = False
      Dim t As Range
      For Each t In Intersect(Target, Range("B:B"))
      Select Case (t.Value)
      Case "A"
      Columns("B:BP").EntireColumn.Hidden = False
      Columns("H:BL").EntireColumn.Hidden = True
      Case "B"
      Columns("B:BP").EntireColumn.Hidden = False
      Columns("F:G").EntireColumn.Hidden = True
      Columns("P:BP").EntireColumn.Hidden = True
      End Select
      Next t

      End If

      safe_exit:
      Application.EnableEvents = True

      'Go to last cell in column B
      With ActiveSheet
      Range("B5").Select
      Selection.End(xlDown).Select
      End With

      End Sub









      share|improve this question
















      I have a working macro that hides/unhides specific columns based off specific values in Column B. I also want to add another trigger that takes the user to the most recently used row. Below is my attempt.



      Private Sub Worksheet_Change(ByVal Target As Range)

      If Not Intersect(Target, Range("B:B")) Is Nothing Then
      On Error GoTo safe_exit
      Application.EnableEvents = False
      Dim t As Range
      For Each t In Intersect(Target, Range("B:B"))
      Select Case (t.Value)
      Case "A"
      Columns("B:BP").EntireColumn.Hidden = False
      Columns("H:BL").EntireColumn.Hidden = True
      Case "B"
      Columns("B:BP").EntireColumn.Hidden = False
      Columns("F:G").EntireColumn.Hidden = True
      Columns("P:BP").EntireColumn.Hidden = True
      End Select
      Next t

      End If

      safe_exit:
      Application.EnableEvents = True

      'Go to last cell in column B
      With ActiveSheet
      Range("B5").Select
      Selection.End(xlDown).Select
      End With

      End Sub






      excel vba triggers






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 4:42

























      asked Nov 16 '18 at 2:50







      user9394674





























          2 Answers
          2






          active

          oldest

          votes


















          0














          Use Select as little as possible, and replace the last section starting with 'Go to last cell in column B with something like this, except moving it inside the If...End If.



          With Me
          .Cells(.Rows.Count, 2).End(xlUp).Select
          End With





          share|improve this answer

























          • could you add this into the question. I must have my syntax out of whack.

            – user9394674
            Nov 16 '18 at 3:32











          • I'm not sure what you are looking for me to do, go ahead and edit your question as needed.

            – BigBen
            Nov 16 '18 at 3:34











          • I've updated the question. Is this how you want it to look? I'ld like to add this macro to a button that takes the user to the most recently used cell

            – user9394674
            Nov 16 '18 at 3:40











          • What do you mean by avoid using Select?

            – user9394674
            Nov 16 '18 at 4:14











          • Use it sparingly. On a different note, can you elaborate on what you need this button to do (i.e. should it also hide columns)? Sounds like you don't need the Worksheet Change event for this.

            – BigBen
            Nov 16 '18 at 4:15



















          1














          Perhaps I misread your question from the other answer - and if so, I will be more than happy to delete this one.



          You can simply add a variable that will track this for you and persist as long as you keep Excel open.



          Private lastUsedRng As Range

          Private Sub Worksheet_Change(ByVal Target As Range)

          Set lastUsedRng = Target

          . . .



          ...that takes the user to the most recently used row




          You can accomplish this with



          lastUsedRng.Select





          share|improve this answer























          • Thanks @K.Davis. Could you please add this to the full question?

            – user9394674
            Nov 16 '18 at 4:14






          • 1





            @PeterJames123 a bit confused why you're asking to edit the question with an answer? If it's an answer, it probably doesn't belong in the question.

            – BigBen
            Nov 16 '18 at 4:20











          • I'm just trying to add either solution into my working script so I can automatically hide/unhide columns and add a button to take the user to the bottom of the range (rather than scroll down). I can't get either solution working. Hence, it would be appreciated if either answer could be placed into the wider question so I can understand the logic.

            – user9394674
            Nov 16 '18 at 4:30






          • 1





            @PeterJames123 but once you edit your question, the answer loses its original context and other readers won't follow.

            – BigBen
            Nov 16 '18 at 4:33












          • Sorry. I was just trying to convey how I was trying to implement your solution and display how it was incorrectly placed. I obviously don't understand your answer. Thanks for the help. It has been changed back.

            – user9394674
            Nov 16 '18 at 4:44










          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%2f53330735%2fmacro-to-show-most-recently-used-cell%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown
























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Use Select as little as possible, and replace the last section starting with 'Go to last cell in column B with something like this, except moving it inside the If...End If.



          With Me
          .Cells(.Rows.Count, 2).End(xlUp).Select
          End With





          share|improve this answer

























          • could you add this into the question. I must have my syntax out of whack.

            – user9394674
            Nov 16 '18 at 3:32











          • I'm not sure what you are looking for me to do, go ahead and edit your question as needed.

            – BigBen
            Nov 16 '18 at 3:34











          • I've updated the question. Is this how you want it to look? I'ld like to add this macro to a button that takes the user to the most recently used cell

            – user9394674
            Nov 16 '18 at 3:40











          • What do you mean by avoid using Select?

            – user9394674
            Nov 16 '18 at 4:14











          • Use it sparingly. On a different note, can you elaborate on what you need this button to do (i.e. should it also hide columns)? Sounds like you don't need the Worksheet Change event for this.

            – BigBen
            Nov 16 '18 at 4:15
















          0














          Use Select as little as possible, and replace the last section starting with 'Go to last cell in column B with something like this, except moving it inside the If...End If.



          With Me
          .Cells(.Rows.Count, 2).End(xlUp).Select
          End With





          share|improve this answer

























          • could you add this into the question. I must have my syntax out of whack.

            – user9394674
            Nov 16 '18 at 3:32











          • I'm not sure what you are looking for me to do, go ahead and edit your question as needed.

            – BigBen
            Nov 16 '18 at 3:34











          • I've updated the question. Is this how you want it to look? I'ld like to add this macro to a button that takes the user to the most recently used cell

            – user9394674
            Nov 16 '18 at 3:40











          • What do you mean by avoid using Select?

            – user9394674
            Nov 16 '18 at 4:14











          • Use it sparingly. On a different note, can you elaborate on what you need this button to do (i.e. should it also hide columns)? Sounds like you don't need the Worksheet Change event for this.

            – BigBen
            Nov 16 '18 at 4:15














          0












          0








          0







          Use Select as little as possible, and replace the last section starting with 'Go to last cell in column B with something like this, except moving it inside the If...End If.



          With Me
          .Cells(.Rows.Count, 2).End(xlUp).Select
          End With





          share|improve this answer















          Use Select as little as possible, and replace the last section starting with 'Go to last cell in column B with something like this, except moving it inside the If...End If.



          With Me
          .Cells(.Rows.Count, 2).End(xlUp).Select
          End With






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 4:15

























          answered Nov 16 '18 at 2:58









          BigBenBigBen

          6,6672719




          6,6672719












          • could you add this into the question. I must have my syntax out of whack.

            – user9394674
            Nov 16 '18 at 3:32











          • I'm not sure what you are looking for me to do, go ahead and edit your question as needed.

            – BigBen
            Nov 16 '18 at 3:34











          • I've updated the question. Is this how you want it to look? I'ld like to add this macro to a button that takes the user to the most recently used cell

            – user9394674
            Nov 16 '18 at 3:40











          • What do you mean by avoid using Select?

            – user9394674
            Nov 16 '18 at 4:14











          • Use it sparingly. On a different note, can you elaborate on what you need this button to do (i.e. should it also hide columns)? Sounds like you don't need the Worksheet Change event for this.

            – BigBen
            Nov 16 '18 at 4:15


















          • could you add this into the question. I must have my syntax out of whack.

            – user9394674
            Nov 16 '18 at 3:32











          • I'm not sure what you are looking for me to do, go ahead and edit your question as needed.

            – BigBen
            Nov 16 '18 at 3:34











          • I've updated the question. Is this how you want it to look? I'ld like to add this macro to a button that takes the user to the most recently used cell

            – user9394674
            Nov 16 '18 at 3:40











          • What do you mean by avoid using Select?

            – user9394674
            Nov 16 '18 at 4:14











          • Use it sparingly. On a different note, can you elaborate on what you need this button to do (i.e. should it also hide columns)? Sounds like you don't need the Worksheet Change event for this.

            – BigBen
            Nov 16 '18 at 4:15

















          could you add this into the question. I must have my syntax out of whack.

          – user9394674
          Nov 16 '18 at 3:32





          could you add this into the question. I must have my syntax out of whack.

          – user9394674
          Nov 16 '18 at 3:32













          I'm not sure what you are looking for me to do, go ahead and edit your question as needed.

          – BigBen
          Nov 16 '18 at 3:34





          I'm not sure what you are looking for me to do, go ahead and edit your question as needed.

          – BigBen
          Nov 16 '18 at 3:34













          I've updated the question. Is this how you want it to look? I'ld like to add this macro to a button that takes the user to the most recently used cell

          – user9394674
          Nov 16 '18 at 3:40





          I've updated the question. Is this how you want it to look? I'ld like to add this macro to a button that takes the user to the most recently used cell

          – user9394674
          Nov 16 '18 at 3:40













          What do you mean by avoid using Select?

          – user9394674
          Nov 16 '18 at 4:14





          What do you mean by avoid using Select?

          – user9394674
          Nov 16 '18 at 4:14













          Use it sparingly. On a different note, can you elaborate on what you need this button to do (i.e. should it also hide columns)? Sounds like you don't need the Worksheet Change event for this.

          – BigBen
          Nov 16 '18 at 4:15






          Use it sparingly. On a different note, can you elaborate on what you need this button to do (i.e. should it also hide columns)? Sounds like you don't need the Worksheet Change event for this.

          – BigBen
          Nov 16 '18 at 4:15














          1














          Perhaps I misread your question from the other answer - and if so, I will be more than happy to delete this one.



          You can simply add a variable that will track this for you and persist as long as you keep Excel open.



          Private lastUsedRng As Range

          Private Sub Worksheet_Change(ByVal Target As Range)

          Set lastUsedRng = Target

          . . .



          ...that takes the user to the most recently used row




          You can accomplish this with



          lastUsedRng.Select





          share|improve this answer























          • Thanks @K.Davis. Could you please add this to the full question?

            – user9394674
            Nov 16 '18 at 4:14






          • 1





            @PeterJames123 a bit confused why you're asking to edit the question with an answer? If it's an answer, it probably doesn't belong in the question.

            – BigBen
            Nov 16 '18 at 4:20











          • I'm just trying to add either solution into my working script so I can automatically hide/unhide columns and add a button to take the user to the bottom of the range (rather than scroll down). I can't get either solution working. Hence, it would be appreciated if either answer could be placed into the wider question so I can understand the logic.

            – user9394674
            Nov 16 '18 at 4:30






          • 1





            @PeterJames123 but once you edit your question, the answer loses its original context and other readers won't follow.

            – BigBen
            Nov 16 '18 at 4:33












          • Sorry. I was just trying to convey how I was trying to implement your solution and display how it was incorrectly placed. I obviously don't understand your answer. Thanks for the help. It has been changed back.

            – user9394674
            Nov 16 '18 at 4:44















          1














          Perhaps I misread your question from the other answer - and if so, I will be more than happy to delete this one.



          You can simply add a variable that will track this for you and persist as long as you keep Excel open.



          Private lastUsedRng As Range

          Private Sub Worksheet_Change(ByVal Target As Range)

          Set lastUsedRng = Target

          . . .



          ...that takes the user to the most recently used row




          You can accomplish this with



          lastUsedRng.Select





          share|improve this answer























          • Thanks @K.Davis. Could you please add this to the full question?

            – user9394674
            Nov 16 '18 at 4:14






          • 1





            @PeterJames123 a bit confused why you're asking to edit the question with an answer? If it's an answer, it probably doesn't belong in the question.

            – BigBen
            Nov 16 '18 at 4:20











          • I'm just trying to add either solution into my working script so I can automatically hide/unhide columns and add a button to take the user to the bottom of the range (rather than scroll down). I can't get either solution working. Hence, it would be appreciated if either answer could be placed into the wider question so I can understand the logic.

            – user9394674
            Nov 16 '18 at 4:30






          • 1





            @PeterJames123 but once you edit your question, the answer loses its original context and other readers won't follow.

            – BigBen
            Nov 16 '18 at 4:33












          • Sorry. I was just trying to convey how I was trying to implement your solution and display how it was incorrectly placed. I obviously don't understand your answer. Thanks for the help. It has been changed back.

            – user9394674
            Nov 16 '18 at 4:44













          1












          1








          1







          Perhaps I misread your question from the other answer - and if so, I will be more than happy to delete this one.



          You can simply add a variable that will track this for you and persist as long as you keep Excel open.



          Private lastUsedRng As Range

          Private Sub Worksheet_Change(ByVal Target As Range)

          Set lastUsedRng = Target

          . . .



          ...that takes the user to the most recently used row




          You can accomplish this with



          lastUsedRng.Select





          share|improve this answer













          Perhaps I misread your question from the other answer - and if so, I will be more than happy to delete this one.



          You can simply add a variable that will track this for you and persist as long as you keep Excel open.



          Private lastUsedRng As Range

          Private Sub Worksheet_Change(ByVal Target As Range)

          Set lastUsedRng = Target

          . . .



          ...that takes the user to the most recently used row




          You can accomplish this with



          lastUsedRng.Select






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 3:02









          K.DᴀᴠɪsK.Dᴀᴠɪs

          7,320112440




          7,320112440












          • Thanks @K.Davis. Could you please add this to the full question?

            – user9394674
            Nov 16 '18 at 4:14






          • 1





            @PeterJames123 a bit confused why you're asking to edit the question with an answer? If it's an answer, it probably doesn't belong in the question.

            – BigBen
            Nov 16 '18 at 4:20











          • I'm just trying to add either solution into my working script so I can automatically hide/unhide columns and add a button to take the user to the bottom of the range (rather than scroll down). I can't get either solution working. Hence, it would be appreciated if either answer could be placed into the wider question so I can understand the logic.

            – user9394674
            Nov 16 '18 at 4:30






          • 1





            @PeterJames123 but once you edit your question, the answer loses its original context and other readers won't follow.

            – BigBen
            Nov 16 '18 at 4:33












          • Sorry. I was just trying to convey how I was trying to implement your solution and display how it was incorrectly placed. I obviously don't understand your answer. Thanks for the help. It has been changed back.

            – user9394674
            Nov 16 '18 at 4:44

















          • Thanks @K.Davis. Could you please add this to the full question?

            – user9394674
            Nov 16 '18 at 4:14






          • 1





            @PeterJames123 a bit confused why you're asking to edit the question with an answer? If it's an answer, it probably doesn't belong in the question.

            – BigBen
            Nov 16 '18 at 4:20











          • I'm just trying to add either solution into my working script so I can automatically hide/unhide columns and add a button to take the user to the bottom of the range (rather than scroll down). I can't get either solution working. Hence, it would be appreciated if either answer could be placed into the wider question so I can understand the logic.

            – user9394674
            Nov 16 '18 at 4:30






          • 1





            @PeterJames123 but once you edit your question, the answer loses its original context and other readers won't follow.

            – BigBen
            Nov 16 '18 at 4:33












          • Sorry. I was just trying to convey how I was trying to implement your solution and display how it was incorrectly placed. I obviously don't understand your answer. Thanks for the help. It has been changed back.

            – user9394674
            Nov 16 '18 at 4:44
















          Thanks @K.Davis. Could you please add this to the full question?

          – user9394674
          Nov 16 '18 at 4:14





          Thanks @K.Davis. Could you please add this to the full question?

          – user9394674
          Nov 16 '18 at 4:14




          1




          1





          @PeterJames123 a bit confused why you're asking to edit the question with an answer? If it's an answer, it probably doesn't belong in the question.

          – BigBen
          Nov 16 '18 at 4:20





          @PeterJames123 a bit confused why you're asking to edit the question with an answer? If it's an answer, it probably doesn't belong in the question.

          – BigBen
          Nov 16 '18 at 4:20













          I'm just trying to add either solution into my working script so I can automatically hide/unhide columns and add a button to take the user to the bottom of the range (rather than scroll down). I can't get either solution working. Hence, it would be appreciated if either answer could be placed into the wider question so I can understand the logic.

          – user9394674
          Nov 16 '18 at 4:30





          I'm just trying to add either solution into my working script so I can automatically hide/unhide columns and add a button to take the user to the bottom of the range (rather than scroll down). I can't get either solution working. Hence, it would be appreciated if either answer could be placed into the wider question so I can understand the logic.

          – user9394674
          Nov 16 '18 at 4:30




          1




          1





          @PeterJames123 but once you edit your question, the answer loses its original context and other readers won't follow.

          – BigBen
          Nov 16 '18 at 4:33






          @PeterJames123 but once you edit your question, the answer loses its original context and other readers won't follow.

          – BigBen
          Nov 16 '18 at 4:33














          Sorry. I was just trying to convey how I was trying to implement your solution and display how it was incorrectly placed. I obviously don't understand your answer. Thanks for the help. It has been changed back.

          – user9394674
          Nov 16 '18 at 4:44





          Sorry. I was just trying to convey how I was trying to implement your solution and display how it was incorrectly placed. I obviously don't understand your answer. Thanks for the help. It has been changed back.

          – user9394674
          Nov 16 '18 at 4:44

















          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%2f53330735%2fmacro-to-show-most-recently-used-cell%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

          27

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          2013 DreamHack Counter-Strike: Global Offensive Championship