Macro to show most recently used cell
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
add a comment |
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
add a comment |
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
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
excel vba triggers
edited Nov 16 '18 at 4:42
asked Nov 16 '18 at 2:50
user9394674
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
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
|
show 3 more comments
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
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
|
show 3 more comments
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
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
|
show 3 more comments
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
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
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
|
show 3 more comments
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
|
show 3 more comments
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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