Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
jmon
 
Posts: n/a
Default Sorting function button

Hi All,

I have three columns, (two text colums, and one numbers column). I can
click on the top cell of any of the three columns and then click on the Sort
Ascending button to sort the list and the entire worksheet will be sorted
with respect to the column I selected.

However, I want to be able to use a clickable color button as the first cell
of each of these columns. When the user clicks on this button it should sort
the entire worksheet by the column below automatically. Is there a way to do
this?

  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default Sorting function button

I saved from a previous post:

How about this. You put some rectangles over each of the header cells in row
1. Make them invisible. When the user clicks on one of those rectangles, your
data gets sorted by that column (click on the same rectangle, it sorts in the
opposite direction).

If that sounds promising...

Saved from a previous post...

I put rectangles over the headers (made the borders invisible) so that when you
clicked on the rectangle, it looked like you were clicking on the header.

Option Explicit
Sub setupOneTime()

Dim myRng As Range
Dim myCell As Range
Dim curWks As Worksheet
Dim myRect As Shape

Set curWks = ActiveSheet

With curWks
'10 columns
Set myRng = .Range("a1").Resize(1, 10)
For Each myCell In myRng.Cells
With myCell
Set myRect = .Parent.Shapes.AddShape _
(Type:=msoShapeRectangle, _
Top:=.Top, Height:=.Height, _
Width:=.Width, Left:=.Left)
End With
With myRect
.OnAction = ThisWorkbook.Name & "!SortTable"
.Fill.Visible = False
.Line.Visible = False
End With
Next myCell
End With
End Sub
Sub sortTable()

Dim myTable As Range
Dim myColToSort As Long
Dim curWks As Worksheet
Dim mySortOrder As Long
Dim LastRow As Long

Set curWks = ActiveSheet
With curWks
myColToSort = .Shapes(Application.Caller).TopLeftCell.Column
LastRow = .Cells(.Rows.Count, "a").End(xlUp).Row
Set myTable = .Range("a1:a" & LastRow).Resize(, 10)
If .Cells(myTable.Row + 1, myColToSort).Value _
< .Cells(LastRow, myColToSort).Value Then
mySortOrder = xlDescending
Else
mySortOrder = xlAscending
End If
myTable.Sort key1:=.Cells(myTable.Row, myColToSort), _
order1:=mySortOrder, _
header:=xlYes
End With

End Sub

===
The setuponetime routine puts the rectangles in 10 columns A1:J1 with this:
'10 columns
Set myRng = .Range("a1").Resize(1, 10)

The sortTable routine sorts that same table based on the number of cells used in
column A (and 10 columns wide) with this line:

Set myTable = .Range("a1:a" & LastRow).Resize(, 10)

If you have more or less columns, adjust both .resize(,10) portions.

If you have to use a different column to get the last row, adjust this line:
LastRow = .Cells(.Rows.Count, "a").End(xlUp).Row



If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

jmon wrote:

Hi All,

I have three columns, (two text colums, and one numbers column). I can
click on the top cell of any of the three columns and then click on the Sort
Ascending button to sort the list and the entire worksheet will be sorted
with respect to the column I selected.

However, I want to be able to use a clickable color button as the first cell
of each of these columns. When the user clicks on this button it should sort
the entire worksheet by the column below automatically. Is there a way to do
this?


--

Dave Peterson
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
Auto average function Diederik Excel Worksheet Functions 3 June 14th 05 03:56 PM
IF function to blank without getting #value in sum function Brad Stevenson Excel Worksheet Functions 5 May 26th 05 10:26 AM
Function Won't Calculate -- Sometimes Bill Martin -- (Remove NOSPAM from address) Excel Discussion (Misc queries) 4 April 13th 05 04:38 PM
Need a ISWorkday Function -- Any Ideas Mark Excel Worksheet Functions 5 March 29th 05 01:58 AM
Can I change the Insert Function button to an equals sign? Jokeyjojo Excel Discussion (Misc queries) 1 January 17th 05 05:45 PM


All times are GMT +1. The time now is 09:21 AM.

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

About Us

"It's about Microsoft Excel"