Newbie Question - help with code
Hi All, :wave:
I'm REALLY new to visual basic. I'm creating a macro that will sort data on a large excel spreadsheet. The data is constantly changing (the number of rows change... number of columns is fixed). The sort will always be starting from the same point (row 12).
What code will work for this? Will a Do Loop work? How do I write the code so that it will select all rows until there is no more data?
Thanks so much for the help!
Re: Newbie Question - help with code
Welcome to the Forums.
Something like this...
VB Code:
Range("A12:G12").Sort Key1:=Range("A12"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Re: Newbie Question - help with code
Hi,
In fat, you could select all the data - "A12:IV65536" and put in enough keys to sort one column after another. Key1, as RobDog shows, is the first sort key, but you can add more. So, for example, you could sort by column A and where items in column A are the same, sort by column C and so on. Or by row, it doesn't matter.
Not entirely sure what you mean by "Select all rows until there is no more data..." unless you mean that you are sorting by row instead of by column and you want to do all of them, in which case what I have said about selecting all the data and setting the appropriate paramter to sort by row will be sufficient.
The Office VBA Help on the Sort method is pretty clear, and tells you all the possible options for the various parameters.
HTH
zaza
Re: Newbie Question - help with code
Thanks for the speedy replies!
I'm looking to select all rows with data starting with Row 12 (to be sorted later). So I wanted to select all rows until it hit a blank row. This is a large report where the number of rows changes (columns are fixed), so the code needs to take that into consideration.
From there I would sort whatever I needed... I wasn't as concerned with the sort part as I was with the Selection part.
Thanks again!!!
Re: Newbie Question - help with code
Rob's Code will do your sort without need for selection. If you need the rows to actually be selected for another reason, this will do it:
VB Code:
Range("A12:G" & Range("A12").End(xlDown).Row).Select