Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
sort dynamic range on active cell | Excel Programming | |||
Return to active cell after sort | Excel Programming | |||
I need to sort an active sheet using the col of the active cell | Excel Programming | |||
sort ascending tab is not active. Why? | Excel Worksheet Functions | |||
sort on not active sheet | Excel Programming |