Link to home
Start Free TrialLog in
Avatar of donohara1
donohara1

asked on

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

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.
Avatar of Steven Harris
Steven Harris
Flag of United States of America image

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?
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).
ASKER CERTIFIED SOLUTION
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial