Solved

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

Posted on 2013-11-09
3
194 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
ID: 39635879
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
ID: 39635889
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 32

Accepted Solution

by:
Rob Henson earned 500 total points
ID: 39638663
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

821 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