Select a range of cells up to last row that contains data

t.powley

Board Regular
Joined
Jun 13, 2002
Messages
76
Hi,

Can anyone help, I want to be able to use VBA to select columns A to D down to the last row that contains any data. The data could be in any row and in any column from A to D

For example:
if the last cell to contain data is B10, I would like the range to be A1:D10
if the last cell to contain data is C57, I would like the range to be A1:D57

Thank you in advanced.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try like this

Code:
Sub atest()
Dim LR As Long
LR = Columnns("A:D").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("A1:D" & LR).Select
End Sub
 
Upvote 0
wow! Thanks for the quick reply.

Works perfectly apart from a slight spelling mistake (Columnns) but seeing as you helped me out so quickly I will look past that one... lol

Cheers :)


working version

Code:
Sub atest()
Dim LR As Long
LR = Columns("A:D").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("A1:D" & LR).Select
End Sub
 
Upvote 0
If your data is always constants (that is, not formulas), then Peter's code is fine. However, if you have formulas in some of your columns, and if those formulas might end up displaying the empty string ("") under certain conditions, then Peter's code might give you the wrong result depending on the settings you used the last time you performed a Find operation (Find remembers its setting whether set manually or via code). Adding the LookIn argument and setting it depending on your data can be used to force the outcome.

Code:
For last displayed value even if that value is from a formula; however formulas displaying the empty string ("") are ignored
-------------------------------------------------------------------------------------------------------------------------------
LastUsedRow = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row

Code:
For last row containing either a value or formula even if that formula is displaying the empty string ("")
-------------------------------------------------------------------------------------------------------------------------------
LastUsedRow = ActiveSheet.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,691
Members
452,938
Latest member
babeneker

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