Solved

Identify a section of an Excel sheet and then sort it.

Posted on 2013-11-09
3
193 Views
Last Modified: 2014-01-09
In my Excel data sheet, I already have chosen a row number as the start of a section of data rows, how can VBA determine the last row of that section (this is the row that has no data/blank in column A). So now, I have identified the start and stop row numbers.

Then if I have a column number (another integer) I would like the VBA to sort the section of rows in descending order. (at that point I will sum the first n entries to get a concentration value of some type(ie ;the top 10 items encompass 22% of the total value...')
 
This will help me learn how to identify variable sized data blocks and then sort/manipulate them.

Thanks.

In my sample file, the data starts on row 2 and I would sort on column 3, descending.
0
Comment
Question by:donohara1
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Steven Harris
Comment Utility
In my sample file, the data starts on row 2 and I would sort on column 3, descending.

No sample file attached.

Have you tried using named ranges?
0
 
LVL 18

Expert Comment

by:Steven Harris
Comment Utility
Alternatively, you can use a table:

A1 is Header - DATA
A2:A36 - data values

Turn A1 to A36 into a table, where DATA is the Label.

From VBA, you can reference this as Range("DATA")

To sort these descending, use:

Sub Sort()
    mySort = Range("data1").Sort(Key1:=Range("a1"), order1:=xlDescending)
End Sub

Open in new window


Now that you have a table, any additional data values entered are now added to the same Label (i.e. your code does not have to change).
0
 
LVL 31

Accepted Solution

by:
Rob Henson earned 500 total points
Comment Utility
Will all of column A be populated until you reach the bottom of the data range? In other words, no blank cells part way down.

If so you can identify the last row in a number of ways eg

Range("A1").Select
Selection.End(xlDown).Select
LastRow=ActiveCell.Row

Open in new window

There are other options that don't involve selecting.

Thanks
Rob H
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now