#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default sort macro

Hello,
I would like to write a macro to sort data from a list which is getting
longer and longer. How to tell the macro to go to the last non empty cell?
Thanks in advance,

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default sort macro

Hi,

You are always better posting your code. The line below sets a range of the
used cells in column A

Set myrange = Range("A1:A" & Cells(Cells.Rows.Count, "A").End(xlUp).Row)

If you wanted to start in (say) A4 then you would change it to this

Set myrange = Range("A4:A" & Cells(Cells.Rows.Count, "A").End(xlUp).Row)
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Jean-Marie" wrote:

Hello,
I would like to write a macro to sort data from a list which is getting
longer and longer. How to tell the macro to go to the last non empty cell?
Thanks in advance,

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,522
Default sort macro

The best way is to set up a self adjusting named range
On the sheet desiredInsertnamedefinename it SortRangein the formula box
=offset($a$1,0,0,counta($a:$a),12)
Look in the help index for OFFSET for more info

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Jean-Marie" wrote in message
...
Hello,
I would like to write a macro to sort data from a list which is getting
longer and longer. How to tell the macro to go to the last non empty cell?
Thanks in advance,


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default sort macro

Thanks Mike for this speedy answer.
Since I'm preety new to this, what do you mean by "You are always better
posting your code"???

"Mike H" wrote:

Hi,

You are always better posting your code. The line below sets a range of the
used cells in column A

Set myrange = Range("A1:A" & Cells(Cells.Rows.Count, "A").End(xlUp).Row)

If you wanted to start in (say) A4 then you would change it to this

Set myrange = Range("A4:A" & Cells(Cells.Rows.Count, "A").End(xlUp).Row)
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Jean-Marie" wrote:

Hello,
I would like to write a macro to sort data from a list which is getting
longer and longer. How to tell the macro to go to the last non empty cell?
Thanks in advance,

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default sort macro

Hi,

I assumed you had some code that wasn't working the way you wanted it to do
so the best way to get and answer is to include a copy of that code with your
question.
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Jean-Marie" wrote:

Thanks Mike for this speedy answer.
Since I'm preety new to this, what do you mean by "You are always better
posting your code"???

"Mike H" wrote:

Hi,

You are always better posting your code. The line below sets a range of the
used cells in column A

Set myrange = Range("A1:A" & Cells(Cells.Rows.Count, "A").End(xlUp).Row)

If you wanted to start in (say) A4 then you would change it to this

Set myrange = Range("A4:A" & Cells(Cells.Rows.Count, "A").End(xlUp).Row)
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Jean-Marie" wrote:

Hello,
I would like to write a macro to sort data from a list which is getting
longer and longer. How to tell the macro to go to the last non empty cell?
Thanks in advance,



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 561
Default sort macro

Sometimes it is "easier" to declare a "List" ["Table" in "2007/2010"] and to
name it DATA, SORTRANGE, or what ever - the MAcrro should then be referred to
that 'Name'.
When using this kind of "List" it gets Dynamic when adding data to it.
Micky


"Don Guillett" wrote:

The best way is to set up a self adjusting named range
On the sheet desiredInsertnamedefinename it SortRangein the formula box
=offset($a$1,0,0,counta($a:$a),12)
Look in the help index for OFFSET for more info

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Jean-Marie" wrote in message
...
Hello,
I would like to write a macro to sort data from a list which is getting
longer and longer. How to tell the macro to go to the last non empty cell?
Thanks in advance,


.

  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default sort macro

I like to pick out a column that always has data in it if the row is used. And
I use a row that always has data in it if the column is used (like headers in
row 1).

In this sample, I used column A and row 1:

Option Explicit
Sub Testme()

Dim LastRow as long
Dim LastCol as long
dim wks as worksheet
dim myRng as range

set wks = worksheets("somesheetnamehere")

with wks
lastrow = .cells(.rows.count,"A").end(xlup).row
lastcol = .cells(1, .columns.count).end(xltoleft).column

set myrng = .range("A1", .cells(lastrow, lastcol))
end with

with myrng
.sort key1:=.columns(1), order1:=xlAscending, _
Header:=xlYes, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom
end with

End with

I like this technique from Debra Dalgleish's site:
http://contextures.com/xlSort02.html#Rectangles
Sort With Invisible Rectangles

Jean-Marie wrote:

Hello,
I would like to write a macro to sort data from a list which is getting
longer and longer. How to tell the macro to go to the last non empty cell?
Thanks in advance,


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default sort macro

Thank you Don. It works!

"Don Guillett" wrote:

The best way is to set up a self adjusting named range
On the sheet desiredInsertnamedefinename it SortRangein the formula box
=offset($a$1,0,0,counta($a:$a),12)
Look in the help index for OFFSET for more info

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Jean-Marie" wrote in message
...
Hello,
I would like to write a macro to sort data from a list which is getting
longer and longer. How to tell the macro to go to the last non empty cell?
Thanks in advance,


.

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
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 02:20 PM
Using Macro to sort without clicking on macro button dd Excel Discussion (Misc queries) 3 May 3rd 07 07:00 PM
Sort Macro kronik Excel Discussion (Misc queries) 0 March 13th 06 04:58 PM
Sort Macro Sprinks Excel Discussion (Misc queries) 1 April 19th 05 05:58 PM
Sort Macro Big Tony New Users to Excel 4 January 31st 05 02:17 PM


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