Last row combined with Current Region VBA

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data that has data in all columns so it is possible that the quoted formula may fail if the the last row does not contain data in column D so I may have to find a more foolproof formula that will work in all situations.
The bigger issue that I have is I'd like to somehow link the Lastrow with the CurrentRegion property so that once the last row has been identified I can effectively work on that data range. I've found that something like DataRange = Lastrow.CurrentRegion doesn't work where DataRange is the table data. How can I write this in VBA with a minimum of coding?
 
Also does this coding presuppose that you have already named the range you're looking for as Table2?
Yes, you need a Table named "Table2".

If required, it is possible to loop all Tables without hard coding the names.
 
Last edited:
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Yes, you need a Table named "Table2".

If required, it is possible to loop all Tables without hard coding the names.
Ok thanks for the clarification, the data had been named Table1 and I altered your code accordingly, still getting the same error message.
Also in your rng statement, will the macro be thrown off if the data does not have anything in cell A1?
 
Upvote 0
Ok thanks for the clarification, the data had been named Table1 and I altered your code accordingly, still getting the same error message.
You need to Have a Table named "Table2", not a named range.
If you want for a named range :
VBA Code:
Sub raneg()
Dim LastRow&, rng As Range
With ActiveSheet.Range("Table3")
    LastRow = .Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    Set rng = Range(.Cells(1, 1), Cells(LastRow, Range([A1], .Cells).Columns.Count))
End With
End Sub

Also in your rng statement, will the macro be thrown off if the data does not have anything in cell A1?
No.
 
Upvote 0
Ok thanks for the revision, it works better with the named range. My final question is though if you go to the trouble of making the received data table a named range then the named range will take care of the boundaries of the data table, wouldn't that make finding the last row/column redundant? So you'd simply set your range as Table3 and loop through that etc.
 
Upvote 0
Yes, except that you wanted the range only to the last populated row in the Table.
 
Upvote 0
As to the first requirement, If you want a foolproof method, have a look at this article - Determining worksheet data area - the function "LastUsedCell" that it contains ensures that all data cells are covered.
That doesn't cover what the OP needs.
He was looking for the last used row in a specific range, not the whole sheet.
 
Upvote 0
Hi @footoo. If a specific range's last row is needed, that's simply:

VBA Code:
With rngToUse
    lngLastRow = .Row + .Rows.Count - 1
End With

However, the initial post said:

I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data that has data in all columns so it is possible that the quoted formula may fail if the the last row does not contain data in column D so I may have to find a more foolproof formula that will work in all situations.

And it was to that that I responded.
 
Upvote 0
Hi @footoo. If a specific range's last row is needed, that's simply:

VBA Code:
With rngToUse
    lngLastRow = .Row + .Rows.Count - 1
End With

However, the initial post said:

I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data that has data in all columns so it is possible that the quoted formula may fail if the the last row does not contain data in column D so I may have to find a more foolproof formula that will work in all situations.

And it was to that that I responded.
I think you need to read the whole thread (including the links in post #2). Just about every way of getting the last row has been covered.
VBA Code:
With rngToUse
    lngLastRow = .Row + .Rows.Count - 1
End With
That doesn't locate the last data row.
 
Upvote 0

Forum statistics

Threads
1,215,764
Messages
6,126,749
Members
449,335
Latest member
Tanne

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