LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 139
Default Button-Instigated Sort: One More Thing...

Per (PeteCresswell):
Sample spreadsheet: http://tinyurl.com/2zhq9x


Here's the VBA code I wound up with in the MS Access app that creates the
spreadsheets. If anybody wants to play with it, I can flip them
and eMail with a .txt file so the word wrapping will go away.
-----------------------------------------------------------------------
Public Sub SortButtons_Create(ByVal theRowNum_Buttons As Long, ByVal
theRowNum_DataFirst As Long, ByVal theRowNum_DataLast As Long, ByVal
theColNum_ButtonFirst As Long, ByVal theColNum_ButtonLast As Long, ByVal
theColNum_DataFirst As Long, ByVal theColNum_DataLast As Long, ByVal
theArrowColor As Long, ByRef theWS As Excel.Worksheet)
13000 debugStackPush mModuleName & ": SortButtons_Create"
13001 On Error GoTo SortButtons_Create_err

' PURPOSE: - To put a series of invisible rectangles on a worksheet which,
when clicked,
' call a routine that sorts the entire sheet's data on that
column's values.
' - To create up/down arrows to supplement the rectangles by
serving as visual indicator
' of what is sorted on and how
' - To create/install a macro named "SortSheet" that will serve as
the routine that sorts the sheet
' ACCEPTS: - Row number of the row to have the invisible rectangles
installed on it
' - Row number of the first row tb sorted
' - Row number of the last row tb sorted
' - Col number of first column that gets a button
' - Col number of last column that gets a button
' - Col number of first column tb sorted (generally same as first
col to get a button)
' - Col number of last column tb sorted (generally same as last
col to get a button)
' - Color tb used when drawing the Up/Down arrows. Must be valid in
Excel's scheme of things.
' e.g. 10 = Red
' - Pointer to the Excel.Worksheet where the buttons go

13002 Dim myWB As Excel.Workbook
Dim myRange As Excel.Range
Dim curCell As Excel.Range
Dim curButton As Shape
Dim curUpArrow As Shape
Dim curDownArrow As Shape
Dim myParentModule As VBComponent
Dim myCodeModule As CodeModule

Dim curRI As RangeInfo

Dim curCellAddress As String
Dim curColNumString As String
Dim myMacroCode As String

Const myArrowHeight As Long = 5
Const myArrowWidth As Long = 5

Const myMacroName As String = "SortSheet" 'This value is implicit
in myMacroCode1

' -----------------------------------------------------------
' We use these constants to assemble the macro tb added to the SS
' which does the actual sorting

Const myMacroCode1 As String = _
" Sub SortSheet() " & vbCrLf & vbCrLf & _
"'PURPOSE: - To allow user to sort the entire sheet by clicking on
a column header" & vbCrLf & _
"' - To maintain visibility of up/down arrows which
indicate which cols are sorted and" & vbCrLf & _
"' the direction of the sort" & vbCrLf & _
"'" & vbCrLf & _
"' NOTES: 1) This routine's code was generated by the same
application (""CDO"")" & vbCrLf & _
"' that created this spreadsheet. That is why the data
area's dimensions" & vbCrLf & _
"' are supplied via constants: the creating app
concatonated them into this code" & vbCrLf & _
"' Pete
Cresswell" & vbCrLf & _
"' 610-513-0066" & vbCrLf & _
" Dim myWS As Worksheet " & vbCrLf & _
" Dim myRange As Range " & vbCrLf & vbCrLf & _
" Dim i As Long " & vbCrLf & _
" Dim mySortCol As Long " & vbCrLf & _
" Dim mySortOrder As Long " & vbCrLf & vbCrLf & _
" Const rowNum_FirstData As Long = "

Const myMacroCode2 As String = " Const rowNum_LastData As Long =
"
Const myMacroCode3 As String = " Const colNum_FirstData As Long =
"
Const myMacroCode4 As String = " Const colNum_LastData As Long =
"

Const myMacroCode5 As String = _
" Set myWS = ActiveSheet " & vbCrLf & vbCrLf & _
" With myWS " & vbCrLf & _
" For i = colNum_FirstData To colNum_LastData" & vbCrLf & _
" .Shapes(""UpArrow"" & Format$(i, ""000"")).Visible =
False" & vbCrLf & _
" .Shapes(""DownArrow"" & Format$(i, ""000"")).Visible =
False" & vbCrLf & _
" Next i" & vbCrLf & vbCrLf & _
" mySortCol = .Shapes(Application.Caller).TopLeftCell.Column "
& vbCrLf & _
" Set myRange = .Range(.Cells(rowNum_FirstData,
colNum_FirstData), .Cells(rowNum_LastData, colNum_LastData)) " & vbCrLf & vbCrLf
& _
" If .Cells(rowNum_FirstData, mySortCol).Value <
..Cells(rowNum_LastData, mySortCol).Value Then " & vbCrLf & _
" mySortOrder = xlDescending " & vbCrLf & _
" .Shapes(""DownArrow"" & Format$(mySortCol,
""000"")).Visible = True" & vbCrLf & _
" Else " & vbCrLf & _
" .Shapes(""UpArrow"" & Format$(mySortCol,
""000"")).Visible = True" & vbCrLf & _
" mySortOrder = xlAscending " & vbCrLf & _
" End If " & vbCrLf & vbCrLf & _
" myRange.Sort key1:=.Cells(rowNum_FirstData, mySortCol),
order1:=mySortOrder " & vbCrLf & _
" End With " & vbCrLf & _
" End Sub "

' ------------------------------------------------------------------------
' First thing, we need to create a code module in the target spreadsheet
' that will hold the code to handle our button click events

13010 Set myWB = theWS.Parent
13011 Set myParentModule = myWB.VBProject.VBComponents.Add(vbext_ct_StdModule )
13012 Set myCodeModule = myParentModule.CodeModule

13019 myMacroCode = myMacroCode1 & theRowNum_DataFirst & vbCrLf & myMacroCode2
& theRowNum_DataLast & vbCrLf & myMacroCode3 & theColNum_ButtonFirst & vbCrLf &
myMacroCode4 & theColNum_DataLast & vbCrLf & vbCrLf & myMacroCode5

13020 With myCodeModule
13021 .InsertLines .CountOfLines + 1, myMacroCode
13029 End With

' ------------------------------------------------------------------------
' Now that we've got our macro code installed in the target Excel workbook,
' we loop through the worksheet's columns, creating a rectangle/button
' and a couple of directional indicator arrows in each column header cell
' NB: If the text in a column header is right-justified, you'll need to
' have done a .IndentLevel=1 to slide it over far enough so the Up/Down
' arrows do not conflict with it

13030 With theWS
13031 Set myRange = .Range(.Cells(theRowNum_Buttons, theColNum_ButtonFirst),
..Cells(theRowNum_Buttons, theColNum_ButtonLast))
13039 For Each curCell In myRange.Cells

13040 With curCell
13041 curCellAddress = .Address(ReferenceStyle:=xlR1C1)
13044 Set curButton = .Parent.Shapes.AddShape(Type:=msoShapeRectangle,
Top:=.Top, Height:=.Height, Width:=.Width, Left:=.Left)
13045 Set curUpArrow =
..Parent.Shapes.AddShape(Type:=msoShapeIsoscelesTr iangle, Top:=(.Top + .Height -
myArrowHeight - 4), Height:=myArrowHeight, Width:=myArrowWidth, Left:=(.Left +
..Width - myArrowWidth - 2))
13046 Set curDownArrow =
..Parent.Shapes.AddShape(Type:=msoShapeIsoscelesTr iangle, Top:=(.Top + .Height -
myArrowHeight - 4), Height:=myArrowHeight, Width:=myArrowWidth, Left:=(.Left +
..Width - myArrowWidth - 2))
13049 End With

13050 curRI = RangeAddress_Parse(curCellAddress)
13059 curColNumString = Format$(curRI.ColLeft, "000")
13060 With curButton
13061 .OnAction = myMacroName
13062 .Fill.Visible = msoFalse
13063 .Line.Visible = msoFalse
13069 End With

13100 With curUpArrow
13101 .Name = "UpArrow" & curColNumString
12109 .Visible = msoFalse

13110 With .Fill
13111 .Solid
13112 .ForeColor.SchemeColor = theArrowColor
13119 End With
13199 End With

13200 With curDownArrow
13201 .Name = "DownArrow" & curColNumString
13202 .Visible = msoFalse
13209 .IncrementRotation 180

13211 With .Fill
13212 .Solid
13213 .ForeColor.SchemeColor = theArrowColor
13219 End With
13299 End With

13990 Next curCell
13999 End With

SortButtons_Create_xit:
DebugStackPop
On Error Resume Next
Set myRange = Nothing
Set curCell = Nothing
Set curButton = Nothing
Set curDownArrow = Nothing
Set curUpArrow = Nothing
Set myParentModule = Nothing
Set myCodeModule = Nothing
Set myWB = Nothing
Exit Sub

SortButtons_Create_err:
BugAlert True, ""
Resume SortButtons_Create_xit
End Sub
-----------------------------------------------------------------------
--
PeteCresswell
 
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
Is there a template for this sort of thing? LAS New Users to Excel 2 November 27th 11 10:29 PM
Sort Button John Gregory Excel Discussion (Misc queries) 2 June 25th 08 08:27 PM
Lookup sort of thing! Paul Excel Worksheet Functions 2 June 23rd 06 03:41 PM
sort button,macro, or ? tiffky Excel Programming 3 August 28th 05 06:44 PM
Add Sort button Mike Excel Programming 2 December 17th 03 12:42 AM


All times are GMT +1. The time now is 04:23 AM.

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"