Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Mona Mehta   on Mar 02 In MS Office Category.

  
Question Answered By: Sherri Parker   on Mar 02

If I understand "Input Box"(like a MsgBox) no. This is too
cumbersome.

I simply put text boxes (could have used buttons also) at the top  of
each collumn to be sorted and coupled each to the respective (fixed
collumn) sort  macro. Just click on the text box and zing! sorted on
that collumn. Been working for several years.

HOWEVER... Though I have my "Selection follows Sort" working, I have
another problem which I can't solve.
NO problems *without* split panes, however...

When I manually split the window into two panes (grab the lower right
mini-bar) and make two panes (left and right sides), I have a cell
selection problem.

If I manually select a cell  in the right pane then run  the sort
macro, the macro  thinks I have a left  pane cell selected! (but
handles it correctly) What I figured out is that the macro "gets"
the last cell selected  in the *LEFT* pane, instead of the right-hand-
cell which _IS_ selected when the macro is entered.

Perhaps I just don't know how to specify the selected cell (in the
macro) .... Help, of course, wasn't any.

Here's my test macro, but the problem has to be obvious above
the '+++++++++++++++++++
====================================================
Sub Sort_N_Select()
'
' Sort_N_Select Macro
' Macro recorded (and edited) 10/21/04 by Steve Noskowicz
'
' This macro has the selected cell follow along with a sort.
'
Dim SelectedCellContents As Variant
InSortCol = 0 'A "selection is in sort col" Flag for fixing things
later...
'
SortColumn = 4 ' Col D sort routine
Application.ScreenUpdating = False ' Suspend screen updating
'
ActiveCell.Activate ' These three don't enlighten the macro.
ActiveCell.Cells.Activate
ActiveCell.Cells.Select

Debug.Print "Enter, Col D Sort ==========="
Debug.Print " ActiveCell="; ActiveCell ' this is the
contents '.Value' optional
Debug.Print " ActiveCell.Row="; ActiveCell.Row ' the row?
Debug.Print " ActiveCell.column "; ActiveCell.Column ' the Column?
' The three variables above this line all show the last selected
_Left_ pane selection
' rather than the _current_ (upon macro entry) right pane
selection. Arrrgg! (:-(
' Therefore, the remaining cell references are in error.

'++++++++++++++++++++++++++++++++++++
If ActiveCell.Column = SortColumn Then
InSortCol = 1 ' In the sort colume
Debug.Print " You're in the sort column=";
ActiveCell.Column ' is a digit
'select one cell to the right first.
ActiveCell.Offset(rowOffset:=0, columnOffset:=1).Activate
Debug.Print " Move to the right, to Col ";
ActiveCell.Column
ActiveCell.Select 'Select the new. Not sure if it is needed,
try W/O
End If
'
SelectedCellContents = ActiveCell.Value ' Hold the selected
cell's contents
Debug.Print " Save contents < "; SelectedCellContents
SelectedColumn = ActiveCell.Column ' Note the selected cell's
column
Debug.Print " Key column  noted = "; SelectedColumn
ActiveCell.Value = "ZUJQRX" ' Insert a search  key in the selected
cell
Debug.Print " Insert Key > "; ActiveCell.Value

'Show the key substitution
Application.ScreenUpdating = True
For A = 0 To 100000
For B = 0 To 500: Next
Next
Application.ScreenUpdating = False

Debug.Print " Do Sort..."
'Do your col D sort (and move the selected cell).
Rows("5:13").Select ' Select the database
Selection.Sort Key1:=Range("D5"), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Debug.Print " ...Sort done."
Columns(SelectedColumn).Select ' Select the "Key" Column
Debug.Print " Select the Key's Column = "; SelectedColumn

' Now find the Keyed cell
Selection.Find(What:="ZUJQRX", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=True).Activate
Debug.Print " Key Found & cell selected"


'Show the key'd cell After the sort
ActiveCell.Select 'Select the new. Not sure if it is needed,
try W/O
Application.ScreenUpdating = True
For A = 0 To 100000
For B = 0 To 300: Next
Next
Application.ScreenUpdating = False

ActiveCell.FormulaR1C1 = SelectedCellContents ' Restore the
contents.
Debug.Print " Replace contents >>"; SelectedCellContents
'
If InSortCol = 1 Then ' If you WERE in the sort column
'' BUT THis doesn't do it IF you start  in column E=5 !!


Debug.Print " You're right of the sort column=";
ActiveCell.Column ' is a digit
'Go back one cell to the left.
ActiveCell.Offset(rowOffset:=0, columnOffset:=-1).Activate
Debug.Print " Offset it back to the left, to Col ";
ActiveCell.Column
End If
ActiveCell.Select 'Select the new. Not sure if it is needed,
try W/O
Debug.Print " It doesn't clear the column selection  if I
don't 'Select'."
Application.ScreenUpdating = True

Debug.Print "Exit, Col D Sort ==========="
End Sub
Sub Restore()
'
' Restore Macro
' Macro recorded 10/21/04 by snoskow1
'
'Resets the database to the original order.
'
Dim SelectedCellContents As Variant
'
Application.ScreenUpdating = False ' Suspend screen updating

SelectedCellContents = ActiveCell.Value ' Hold the selected
cell's contents
SelectedColumn = ActiveCell.Column ' Note the selected cell's
column
ActiveCell.Value = "ZUJQRX" ' Insert a search key in the selected
cell


Rows("5:13").Select
Selection.Sort Key1:=Range("C5"), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Range("E17").Select

Columns(SelectedColumn).Select ' Select the "Key" Column

' Now find the Keyed cell
Selection.Find(What:="ZUJQRX", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=True).Activate

ActiveCell.Select 'Select the found cell

ActiveCell.FormulaR1C1 = SelectedCellContents ' Restore a
contents.

Application.ScreenUpdating = True
End Sub

Share: 

 

This Question has 9 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Retain cell selection through sort Or get search suggestion and latest updates.


Tagged: