Find the last row of data in a Macro

Dan Wilson

Active Member
Joined
Feb 5, 2006
Messages
448
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...
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,477
Office Version
  1. 2019
Platform
  1. Windows
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
 

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
3,951
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
 
Solution

Dan Wilson

Active Member
Joined
Feb 5, 2006
Messages
448
Office Version
  1. 365
Platform
  1. Windows
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...
 

Dan Wilson

Active Member
Joined
Feb 5, 2006
Messages
448
Office Version
  1. 365
Platform
  1. Windows
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...
 

Watch MrExcel Video

Forum statistics

Threads
1,127,098
Messages
5,622,678
Members
415,920
Latest member
ExcelNoob28

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
Top