Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Sorting Question

I am using Excel 2010 and I have some "sort code" that is the result
of the macro recorder. Is there anything that I can use in place of
G2:G109 as the range address? The data can be more or less the next
time that I run the macro and therefore I'd rather not use a hard
coded range. I tried using things like xlDown and few other things
that I found from research but nothing really worked. Thanks very
much.

'sort
ActiveWorkbook.Worksheets("My Calcs").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("My Calcs").Sort.SortFields.Add
Key:=Range( _
"G2:G109"), SortOn:=xlSortOnValues, Order:=xlAscending,
DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("My Calcs").Sort
.SetRange Range("A1").CurrentRegion
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 587
Default Sorting Question

hi John,

Set rng = ActiveWorkbook.Worksheets("My Calcs").Range("G2:G" & Range("G" & Cells.Rows.Count).End(xlUp).Row)

ActiveWorkbook.Worksheets("My Calcs").Sort.SortFields.Add Key:=rng, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal



--
isabelle



Le 2012-01-21 15:15, John Menken a écrit :
I am using Excel 2010 and I have some "sort code" that is the result
of the macro recorder. Is there anything that I can use in place of
G2:G109 as the range address? The data can be more or less the next
time that I run the macro and therefore I'd rather not use a hard
coded range. I tried using things like xlDown and few other things
that I found from research but nothing really worked. Thanks very
much.

'sort
ActiveWorkbook.Worksheets("My Calcs").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("My Calcs").Sort.SortFields.Add
Key:=Range( _
"G2:G109"), SortOn:=xlSortOnValues, Order:=xlAscending,
DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("My Calcs").Sort
.SetRange Range("A1").CurrentRegion
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Sorting Question

Thank you isabelle.
I tried your code but unfortunately it does not perform the sort.
It doesn't throw an error but it doesn't sort by the intended column
which is column G.
Thanks.
  #4   Report Post  
Posted to microsoft.public.excel.programming
Ken Ken is offline
external usenet poster
 
Posts: 207
Default Sorting Question

John
It is usually better to give the sort range a range name and address
the range name in your code; something like range("data") rather than
the cells. you may need to make it a dynamic range name so it will
expand if you add new data, assuming the addition of more data is what
causes the range to change. Alternatively, you could use a table
which will exand automatically when you add data.
I hope this helps.
Ken


On Jan 21, 3:25*pm, isabelle wrote:
hi John,

Set rng = ActiveWorkbook.Worksheets("My Calcs").Range("G2:G" & Range("G" & Cells.Rows.Count).End(xlUp).Row)

ActiveWorkbook.Worksheets("My Calcs").Sort.SortFields.Add Key:=rng, _
* * SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

--
isabelle

Le 2012-01-21 15:15, John Menken a écrit :



I am using Excel 2010 and I have some "sort code" that is the result
of the macro recorder. Is there anything that I can use in place of
G2:G109 as the range address? The data can be more or less the next
time that I run the macro and therefore I'd rather not use a hard
coded range. I tried using things like xlDown and few other things
that I found from research but nothing really worked. Thanks very
much.


'sort
ActiveWorkbook.Worksheets("My Calcs").Sort.SortFields.Clear
* * *ActiveWorkbook.Worksheets("My Calcs").Sort.SortFields.Add
Key:=Range( _
* * * * *"G2:G109"), SortOn:=xlSortOnValues, Order:=xlAscending,
DataOption:= _
* * * * *xlSortNormal
* * *With ActiveWorkbook.Worksheets("My Calcs").Sort
* * * * *.SetRange Range("A1").CurrentRegion
* * * * *.Header = xlYes
* * * * *.MatchCase = False
* * * * *.Orientation = xlTopToBottom
* * * * *.SortMethod = xlPinYin
* * * * *.Apply
* * *End With


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Sorting Question

I understand the concept you are expressing, it's just when I use the
recorder to select all data before giving it a range names it places
the code as a cell addressed range name.
Do you know the code for selecting all the data without specifying a
range name?
Thanks.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Sorting Question

John Menken submitted this idea :
I understand the concept you are expressing, it's just when I use the
recorder to select all data before giving it a range names it places
the code as a cell addressed range name.
Do you know the code for selecting all the data without specifying a
range name?
Thanks.


I believe Ken is referring to the extensive amount of cleanup you'll
need to do in the code generated by the macro recorder. This is normal
since the macro recorder is prone to create the most inefficient code
you'll ever imagine. Thus, in the code where you see the sort range
address, swap that out for your defined name for the sort range.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


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 Question Jennifer Excel Discussion (Misc queries) 0 April 14th 08 10:11 PM
Re my question on sorting judie Excel Worksheet Functions 1 April 2nd 07 02:06 AM
Sorting question-is this possible JOUIOUI Excel Programming 1 March 6th 07 10:44 PM
Sorting Question srroduin Excel Programming 1 May 30th 06 11:41 PM
Sorting question Erin[_5_] Excel Programming 1 February 16th 04 02:47 AM


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