Find the last row of data in a Macro

Dan Wilson

Well-known Member
Joined
Feb 5, 2006
Messages
504
Office Version
  1. 365
Platform
  1. Windows
Good day. I am running Excel out of Office365 (updated) on Windows 10 Home. I have a workbook that creates a worksheet by extracting data from my Music folder. The Music folder now contains 4500 files and is constantly being updated, so the number of files in that folder varies every day. The Macro in the worksheet copies eight elements from the File Properties of each file in the Music folder. All of that works. After creating each worksheet, I would like to be able to sort the data in the new worksheet using one or two of the columns in the new worksheet. Since the count of Rows in the worksheet changes, I need to have the Macro determine the last Row of data in the worksheet. Any help is appreciated.
Thank you,
Dan Wilson...
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi,
Providing sheets are not protected try using Range.CurrentRegion Property as this autmatically expands to the entire region

as an example something like following may do what you want

VBA Code:
Range("A1").CurrentRegion.Sort key1:=Range("D1"), order1:=xlAscending, _
                               key2:=Range("F1"), order2:=xlAscending, _
                                Header:=xlYes

You will need to adapt the idea to meet specific project need

Dave
 
Upvote 0
VBA Code:
Option Explicit

Sub Test()
Dim LastRow, Cells As Range
    With ActiveSheet
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    MsgBox LastRow
    End With
End Sub
 
Upvote 0
Solution
Hi,
Providing sheets are not protected try using Range.CurrentRegion Property as this autmatically expands to the entire region

as an example something like following may do what you want

VBA Code:
Range("A1").CurrentRegion.Sort key1:=Range("D1"), order1:=xlAscending, _
                               key2:=Range("F1"), order2:=xlAscending, _
                                Header:=xlYes

You will need to adapt the idea to meet specific project need

Dave
Good day DMT32. Thank you for responding. I will try your suggestion.
Dan Wilson...
 
Upvote 0
VBA Code:
Option Explicit

Sub Test()
Dim LastRow, Cells As Range
    With ActiveSheet
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    MsgBox LastRow
    End With
End Sub
Good day Logit. Thank you for responding. I will try your suggestion.
Dan Wilson...
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,142
Members
448,551
Latest member
Sienna de Souza

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