return to selected cell after closing filter

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
I have a column with thousands of serial numbers. I apply button 1 to filter the range, based on (part of) the serial number. Works fine.

Then I apply button 2 to undo the filter. However, I then want to return to the previously selected cell, in this example to A15.

Who can help me ?

Thanks.
 

Attachments

  • filter.png
    filter.png
    48.2 KB · Views: 5

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
after having a short smoke break, I found the solution myself. I guess I was too tired after a weekend of VBA programming...

This is my solution:

on top I put:
VBA Code:
Public i, j As Integer

then the code for button 2 (which I named cmdShowAll

VBA Code:
Private Sub cmdShowAll_Click()

ActiveSheet.ShowAllData

cmdFind.Visible = True
cmdShowAll.Visible = False

Application.Goto Reference:=Cells(i, j)

End Sub
 
Upvote 0
Solution
Thanks for posting your solution. It is a good example declaring a variable in the module scope to be able to assign their values in some procedures and accessing them in other procedures. So, it would be great if you could also post the procedure that actually assigns variables i and j. Then it would be more helpful example to the future readers.

Also, the following declaration:
VBA Code:
Public i, j As Integer
declares j as an Integer variable, however, i as a Variant. Same line declaration is correct and acceptable (although I personally don't prefer that for readability purposes). However, each variable should be declared with their type as shown below:
VBA Code:
Public i Integer, j As Integer
It won't affect the code since the Variant type is OK in this particular case, but you might be thinking that i is also declared as an Integer when it is used in this form. It is not.

While we are talking about variable types, just to make sure not causing an overflow error, declaring i as Long type would be better choice since row index can be more than 32767. Again, this will probably not affect most applications including yours, but I remember the days right after Excel 2007 (1M rows) that I found myself making this mistake a lot in large worksheets.
 
Upvote 0

Forum statistics

Threads
1,215,078
Messages
6,122,996
Members
449,093
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top