Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Can someone give me a simple macro to sort a column?

Hi-
I have a whole bunch of data in several columns and I want to set up the
spreadsheet so when I click the column title it sorts the data (keeping all
the rows together still) ascending and then if I click the column title again
it sorts it descending and so on. I want to do this for every column. I
know very little about macro coding and I would really appreciate it if
someone could give me a sample one and instructions as I feel this must be
pretty simple. I tried searching on google but I can't seem to find
anything. I would really apprecriate any help anyone might be able to offer.
Thanks!
-Dan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Can someone give me a simple macro to sort a column?

Dan,

Here is one way. Be aware though that once you enter your code this won't
allow you to change row 1 headings, so get the data set-up first


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A1:IV1")) Is Nothing Then
With Target
If .Characters(Len(.Value), 1).Font.Name < "Marlett" Then
.Value = .Value & " t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
If Right(.Value, 1) = "t" Then
.EntireColumn.Sort key1:=.Offset(1, 0), _
order1:=xlAscending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "u"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
Else
.EntireColumn.Sort key1:=.Offset(1, 0), _
order1:=xlDescending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
.Offset(1, 0).Activate
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Dan Orth" wrote in message
...
Hi-
I have a whole bunch of data in several columns and I want to set up the
spreadsheet so when I click the column title it sorts the data (keeping

all
the rows together still) ascending and then if I click the column title

again
it sorts it descending and so on. I want to do this for every column. I
know very little about macro coding and I would really appreciate it if
someone could give me a sample one and instructions as I feel this must be
pretty simple. I tried searching on google but I can't seem to find
anything. I would really apprecriate any help anyone might be able to

offer.
Thanks!
-Dan



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Can someone give me a simple macro to sort a column?

Thanks - that is just what I want except is there a way for the rows to stay
together? I want the data in the columns to always line up by row so I
believe I can only have it sorted by one column at a time. Is there anyway
to modify the code to get this? Thanks for your help!
-Dan


"Bob Phillips" wrote in message
...
Dan,

Here is one way. Be aware though that once you enter your code this won't
allow you to change row 1 headings, so get the data set-up first


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A1:IV1")) Is Nothing Then
With Target
If .Characters(Len(.Value), 1).Font.Name < "Marlett" Then
.Value = .Value & " t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
If Right(.Value, 1) = "t" Then
.EntireColumn.Sort key1:=.Offset(1, 0), _
order1:=xlAscending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "u"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
Else
.EntireColumn.Sort key1:=.Offset(1, 0), _
order1:=xlDescending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
.Offset(1, 0).Activate
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Dan Orth" wrote in message
...
Hi-
I have a whole bunch of data in several columns and I want to set up the
spreadsheet so when I click the column title it sorts the data (keeping

all
the rows together still) ascending and then if I click the column title

again
it sorts it descending and so on. I want to do this for every column. I
know very little about macro coding and I would really appreciate it if
someone could give me a sample one and instructions as I feel this must
be
pretty simple. I tried searching on google but I can't seem to find
anything. I would really apprecriate any help anyone might be able to

offer.
Thanks!
-Dan





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Can someone give me a simple macro to sort a column?

Dan,

Yeah, it's pretty straight-forward

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A1:IV1")) Is Nothing Then
With Target
If .Characters(Len(.Value), 1).Font.Name < "Marlett" Then
.Value = .Value & " t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
If Right(.Value, 1) = "t" Then
Me.Cells.Sort key1:=.Offset(1, 0), _
order1:=xlAscending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "u"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
Else
Me.Cells.Sort key1:=.Offset(1, 0), _
order1:=xlDescending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
.Offset(1, 0).Activate
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

It's good this isn't it, I like it myself with this modified version.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Dan" wrote in message
...
Thanks - that is just what I want except is there a way for the rows to

stay
together? I want the data in the columns to always line up by row so I
believe I can only have it sorted by one column at a time. Is there

anyway
to modify the code to get this? Thanks for your help!
-Dan


"Bob Phillips" wrote in message
...
Dan,

Here is one way. Be aware though that once you enter your code this

won't
allow you to change row 1 headings, so get the data set-up first


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A1:IV1")) Is Nothing Then
With Target
If .Characters(Len(.Value), 1).Font.Name < "Marlett" Then
.Value = .Value & " t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
If Right(.Value, 1) = "t" Then
.EntireColumn.Sort key1:=.Offset(1, 0), _
order1:=xlAscending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "u"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
Else
.EntireColumn.Sort key1:=.Offset(1, 0), _
order1:=xlDescending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
.Offset(1, 0).Activate
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Dan Orth" wrote in message
...
Hi-
I have a whole bunch of data in several columns and I want to set up

the
spreadsheet so when I click the column title it sorts the data (keeping

all
the rows together still) ascending and then if I click the column title

again
it sorts it descending and so on. I want to do this for every column.

I
know very little about macro coding and I would really appreciate it if
someone could give me a sample one and instructions as I feel this must
be
pretty simple. I tried searching on google but I can't seem to find
anything. I would really apprecriate any help anyone might be able to

offer.
Thanks!
-Dan







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Can someone give me a simple macro to sort a column?

Thanks a lot! That is exactly what I was looking for!

-Dan


"Bob Phillips" wrote in message
...
Dan,

Yeah, it's pretty straight-forward

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A1:IV1")) Is Nothing Then
With Target
If .Characters(Len(.Value), 1).Font.Name < "Marlett" Then
.Value = .Value & " t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
If Right(.Value, 1) = "t" Then
Me.Cells.Sort key1:=.Offset(1, 0), _
order1:=xlAscending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "u"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
Else
Me.Cells.Sort key1:=.Offset(1, 0), _
order1:=xlDescending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
.Offset(1, 0).Activate
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

It's good this isn't it, I like it myself with this modified version.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Dan" wrote in message
...
Thanks - that is just what I want except is there a way for the rows to

stay
together? I want the data in the columns to always line up by row so I
believe I can only have it sorted by one column at a time. Is there

anyway
to modify the code to get this? Thanks for your help!
-Dan


"Bob Phillips" wrote in message
...
Dan,

Here is one way. Be aware though that once you enter your code this

won't
allow you to change row 1 headings, so get the data set-up first


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("A1:IV1")) Is Nothing Then
With Target
If .Characters(Len(.Value), 1).Font.Name < "Marlett" Then
.Value = .Value & " t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
If Right(.Value, 1) = "t" Then
.EntireColumn.Sort key1:=.Offset(1, 0), _
order1:=xlAscending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "u"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
Else
.EntireColumn.Sort key1:=.Offset(1, 0), _
order1:=xlDescending, _
header:=xlYes
.Value = Left(.Value, Len(.Value) - 1) & "t"
.Characters(Len(.Value), 1).Font.Name = "Marlett"
End If
.Offset(1, 0).Activate
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Dan Orth" wrote in message
...
Hi-
I have a whole bunch of data in several columns and I want to set up

the
spreadsheet so when I click the column title it sorts the data
(keeping
all
the rows together still) ascending and then if I click the column
title
again
it sorts it descending and so on. I want to do this for every column.

I
know very little about macro coding and I would really appreciate it
if
someone could give me a sample one and instructions as I feel this
must
be
pretty simple. I tried searching on google but I can't seem to find
anything. I would really apprecriate any help anyone might be able to
offer.
Thanks!
-Dan








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
divide column(x) by column(y) to give column(x/y) in excel? James New Users to Excel 2 April 24th 23 11:46 AM
Use macro to sort column Paula Excel Worksheet Functions 2 August 14th 09 06:59 PM
Hopefully a simple macro to precatenate data in a column Jon in Canby Or. Excel Discussion (Misc queries) 7 April 30th 08 02:33 PM
Macro To Sort By Column Header Names Derek Hart Excel Worksheet Functions 6 December 26th 07 09:41 PM
Filter/sort data to give highest value from each day Cootha Excel Discussion (Misc queries) 10 August 28th 06 08:18 AM


All times are GMT +1. The time now is 06:09 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"