Dynamic Range in a macro

deuxo

New Member
Joined
Mar 25, 2002
Messages
13
I am looking for an examlple of a macro or the function I should use that will sort rows and columns of information in a worksheet where the number of rows or columns or both change daily. Thanks.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
deuxo
you could also check out dave hawley's site at http://www.ozgrid.com. the vba page has a bit on dynamic ranges. think it's called 'dynamic range via vba'. doesn't solve the whole problem though - doesn't include anything about sorting data.
This message was edited by anno on 2002-04-17 19:43
 
Upvote 0
Hi,

Not sure I follow what you are trying to do. Here is a routine which cycles through each column of the active sheet and sorts each ascending.

This treats all columns independent of one another, meaning it will *not* sort the expanded selection (like the choice that pops up when Excel tries to figure out what to sort).

If you need to sort on more than 3 columns with grouped data, then this is not the technique to do so.

No error checking is present, and the macro assumes row 1 houses the category headers. That can be changed if needed.

'---begin VBA---
Sub test()
Dim lastcol As Integer
Dim x As Integer, currlast As Long

With ActiveSheet
.UsedRange
lastcol = .Cells.SpecialCells(xlCellTypeLastCell).Column

For x = 1 To lastcol
currlast = .Cells(Rows.Count, x).End(xlUp).Row
.Range(.Cells(1, x), .Cells(currlast, x)).Sort _
Key1:=.Cells(2, x), Order1:=xlAscending, Header:=xlYes
Next x

End With

End Sub
'---end VBA---

HTH,
Jay
 
Upvote 0

Forum statistics

Threads
1,214,530
Messages
6,120,071
Members
448,943
Latest member
sharmarick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top