Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 504
Default help with sort on active cell

hi is there anyway to sort a range but the column to be sorted is based on
which cell is active ie if i have 3 cols - name, age, height i need to click
a button that sorts the col of whatever cell is currently selected. ideally i
would like to be able for the button to sort the other way ie descending if
clicked again. To complicate things the range is dynamic

Hope you guys can help

Thanks
kevin
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default help with sort on active cell

Kevin.

The code below will sort the first three columns based on the active cell, with the region starting
in A1, with headers in row 1.

Insert a commandbutton off the control toolbox commandbar to sude this code.

HTH,
Bernie
MS Excel MVP


Public SortAsc As Boolean
Public SortDesc As Boolean
Public myCol As Integer

Private Sub CommandButton1_Click()
Dim myC As Range
Dim myR As Range

Set myR = Range("A1").CurrentRegion
Set myC = Selection

If myC.Cells.Count 1 Then
MsgBox "Select a single cell only."
Exit Sub
End If

If Intersect(myR, myC) Is Nothing Then
MsgBox "Select a cell within " & myR.Address(False, False)
Exit Sub
End If

If myCol = 0 Or myCol < myC.Column Then
myCol = myC.Column
SortAsc = False
SortDesc = False
End If

If Not SortAsc And Not SortDesc Then
SortAsc = True
myR.Sort myC, xlAscending, header:=xlYes
ElseIf SortAsc Then
SortAsc = False
SortDesc = True
myR.Sort myC, xlDescending, header:=xlYes
Else
SortAsc = True
SortDesc = False
myR.Sort myC, xlAscending, header:=xlYes
End If

End Sub




"kevin" wrote in message
...
hi is there anyway to sort a range but the column to be sorted is based on
which cell is active ie if i have 3 cols - name, age, height i need to click
a button that sorts the col of whatever cell is currently selected. ideally i
would like to be able for the button to sort the other way ie descending if
clicked again. To complicate things the range is dynamic

Hope you guys can help

Thanks
kevin



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default help with sort on active cell

There is no need for multiple postings. See your otiginal post.



"kevin" wrote in message
...
hi is there anyway to sort a range but the column to be sorted is based on
which cell is active ie if i have 3 cols - name, age, height i need to
click
a button that sorts the col of whatever cell is currently selected.
ideally i
would like to be able for the button to sort the other way ie descending
if
clicked again. To complicate things the range is dynamic

Hope you guys can help

Thanks
kevin



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 504
Default help with sort on active cell

Agreed. I came back to the discussion board and didn't see my original post
so thought it may have not gone through and submitted another. apologies

Thanks for the help Bernie really appreciate it

"JLGWhiz" wrote:

There is no need for multiple postings. See your otiginal post.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sort dynamic range on active cell kevin Excel Programming 3 November 20th 09 04:34 PM
Return to active cell after sort Patrick C. Simonds Excel Programming 2 August 2nd 08 03:58 AM
I need to sort an active sheet using the col of the active cell HamFlyer Excel Programming 3 June 6th 06 07:25 PM
sort ascending tab is not active. Why? jeremy corke Excel Worksheet Functions 2 July 10th 05 12:00 AM
sort on not active sheet peter_bhp Excel Programming 4 December 25th 04 09:00 AM


All times are GMT +1. The time now is 08:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"