#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 15
Default A Sorting Problem

In Excel 2007, I'm trying to sort on column A, with headers of :x: and "y".
There is a formula in column A:
=IF(B2="","",B2), then copied down.

I need to have Rows 2-4 (cells that don't display anything, but have a
formula in them) to sort below the cells which display characters. Does
anyone have a suggestion on how to accomplish this?

I am using a command button to initiate the sort - the VBA code for the
command button is shown below the spreadsheet.

A B
1 y z
2
3
4
5 a a
6 c c
7 m m
8 r r
9 x x

--------------------
Private Sub CommandButton1_Click()
'
Application.Goto Reference:="data"
ActiveWorkbook.Worksheets("data").Sort.SortFields. Clear
ActiveWorkbook.Worksheets("data").Sort.SortFields. Add Key:=Range( _
"A2:A20"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("data").Sort
.SetRange Range("A1:B20")
.Header = xltrue
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,480
Default A Sorting Problem

Hi

It worked fine for me when I modified to

Private Sub CommandButton1_Click()
'
ActiveWorkbook.Worksheets("data").Sort.SortFields. Clear
ActiveWorkbook.Worksheets("data").Sort.SortFields. Add Key:=Range( _
"A2:A20"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=
_
xlSortNormal
With ActiveWorkbook.Worksheets("data").Sort
.SetRange Range("A1:B20")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub

xlTrue is not valid it should be xlYes
--
Regards
Roger Govier

"k1ngr" wrote in message
...
In Excel 2007, I'm trying to sort on column A, with headers of :x: and
"y".
There is a formula in column A:
=IF(B2="","",B2), then copied down.

I need to have Rows 2-4 (cells that don't display anything, but have a
formula in them) to sort below the cells which display characters. Does
anyone have a suggestion on how to accomplish this?

I am using a command button to initiate the sort - the VBA code for the
command button is shown below the spreadsheet.

A B
1 y z
2
3
4
5 a a
6 c c
7 m m
8 r r
9 x x

--------------------
Private Sub CommandButton1_Click()
'
Application.Goto Reference:="data"
ActiveWorkbook.Worksheets("data").Sort.SortFields. Clear
ActiveWorkbook.Worksheets("data").Sort.SortFields. Add Key:=Range( _
"A2:A20"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=
_
xlSortNormal
With ActiveWorkbook.Worksheets("data").Sort
.SetRange Range("A1:B20")
.Header = xltrue
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default A Sorting Problem

Unqualified ranges will refer to the active sheet if the code is in a general
module.

Unqualified ranges will refer to the worksheet that holds the code if the code
is in a worksheet module.

And your _Click event looks like the code is in a worksheet module.

So qualify all your ranges:

Private Sub CommandButton1_Click()

Application.Goto Reference:="data"
ActiveWorkbook.Worksheets("data").Sort.SortFields. Clear
ActiveWorkbook.Worksheets("data").Sort.SortFields. Add _
Key:=ActiveWorkbook.Worksheets("data").Range( _
"A2:A20"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("data").Sort
.SetRange ActiveWorkbook.Worksheets("data").Range("A1:B20")
.Header = xltrue
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub

You may want to try it without the application.goto:
Private Sub CommandButton1_Click()

with ActiveWorkbook.Worksheets("data")

.Sort.SortFields.Clear
.Sort.SortFields.Add _
Key:=.Range("A2:A20"), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal

With .Sort
.SetRange .Range("A1:B20")
.Header = xltrue
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
end with

End Sub

(uncompiled, untested.)



k1ngr wrote:

In Excel 2007, I'm trying to sort on column A, with headers of :x: and "y".
There is a formula in column A:
=IF(B2="","",B2), then copied down.

I need to have Rows 2-4 (cells that don't display anything, but have a
formula in them) to sort below the cells which display characters. Does
anyone have a suggestion on how to accomplish this?

I am using a command button to initiate the sort - the VBA code for the
command button is shown below the spreadsheet.

A B
1 y z
2
3
4
5 a a
6 c c
7 m m
8 r r
9 x x

--------------------
Private Sub CommandButton1_Click()
'
Application.Goto Reference:="data"
ActiveWorkbook.Worksheets("data").Sort.SortFields. Clear
ActiveWorkbook.Worksheets("data").Sort.SortFields. Add Key:=Range( _
"A2:A20"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("data").Sort
.SetRange Range("A1:B20")
.Header = xltrue
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub


--

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
Sorting VBA problem wynand Excel Discussion (Misc queries) 6 February 7th 08 06:38 PM
sorting problem MJM Excel Discussion (Misc queries) 3 February 22nd 07 10:20 PM
SORTING PROBLEM Grace Excel Worksheet Functions 2 September 27th 06 08:10 PM
Sorting problem Becks Excel Discussion (Misc queries) 6 May 4th 06 02:36 PM
Sorting problem Chris Excel Worksheet Functions 1 November 3rd 04 11:27 PM


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