De-selecting cells in a sheet processed indirectly that VBA has selected itself

Bill Hamilton

Board Regular
Joined
Mar 30, 2004
Messages
95
I have a short macro that is invoked from a button on a 'master' sheet which is the active sheet. The macro receives a reference to another worksheet as a parameter, works out an area to sort on that sheet, sorts it and exits. The sorted sheet is never activated or selected or referenced directly.

On exit, the sort area on that sheet is 'selected'. That is not done by the code. How can I de-select it and set the selected or active cell for the sheet somewhere else? I obviously can't actually directly select a cell or anything as it is not the active sheet, but there must be a way of getting rid of the big blue area in the middle of the screen which would annoy my user a lot when he come to look at that sheet if I had to leave it like that.

Hope someone can help.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
What version of Excel do you have. The selection is unchanged if I run this while another worksheet is active in Excel 2003:

Code:
Sub Test()
    With Worksheets("Sheet3")
        .Range("A1:B4").Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    End With
End Sub
 
Upvote 0
Thanks for the speedy reply Andrew.

I'm using 2007 - sorry, should have mentioned that. Here's my code, all stripped down to the bare essentials. The original goes through a few hoops working out the area to be sorted but these specific values do the biz.

Code:
Sub Test()
With Sheets("Class A comp 1")
  .Sort.SortFields.Clear
  .Sort.SortFields.Add Key:=Range("C6"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
  With .Sort
    .SetRange Range("C6:K20")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
End With
End Sub

On running this, the sort area C6:K20 is highlighted on completion.

Bill
 
Upvote 0
Thanks. That worked, and I'll incorporate it into my project.

So, how's a body to know about the Sort Method then? All my sorting code originates from the Macro Recorder because it's just too fiddly to remember all its ins and outs. The 2007 Recorder always utilises what you're calling the Sort Object and I was under the impression that the 'Method' method from older versions of Excel was deprecated. What's Microsoft's agenda here? What's the point of having two different ways of doing the same thing? (or rather, not the same thing in this case.) Sorry, rant over. No reply required.

Anyway, thank you again,

Bill
 
Upvote 0
hi Bill

in doing the same sort of thing, i did away with sort many years ago and use RANK, MATCH, and INDEX to get results sorted, I could then add to the ranking to resolve ties on count back etc etc
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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