ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   sorting data based on variable number of rows (https://www.excelbanter.com/excel-programming/399528-sorting-data-based-variable-number-rows.html)

Andrew[_56_]

sorting data based on variable number of rows
 
Hello,
I am trying to create a data entry sheet which automatically sorts
itself each time it opens. It should sort according to a specific row
(D). And I want it to only sort the rows which have data in them. I
have a counter which tells me how many rows have data. I use the
snippet below which works if I already know how many rows of data I
have. In the code shown I sort down to row 300. Suppose I have a
variable named N which is equal to the number of rows. How can I get
the code to sort N rows?


Worksheets("data").Range("A2,F300").Sort
Key1:=Worksheets("data").Range("D2")

thanks,
Andrew


marcus[_3_]

sorting data based on variable number of rows
 
Hi Andrew

This will sort your data based on the populated data in Column F.
Change F to which ever row contains the most relevant data.

Take care

Marcus

Sub Sortme()
Dim N As Long

N = Range("F" & Rows.Count).End(xlUp).Row
Worksheets("sheet2").Range("A2:F" & N).Select

'Sort part add if needed.
Selection.Sort Key1:=Range("D2"), Order1:=xlAscending,
Header:=xlGuess, OrderCustom:=1

End Sub



All times are GMT +1. The time now is 08:59 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com