0

I have a table that ranges from "A:EV".

I want to find the last row of only range "A:DD".

The columns might have blank cells, so I need to go through all and find the furthest row of columns A to DD.

How can I code it?

9
  • 1
    Same way you check for a single column. Then generate a loop to go from column A to DD checking where the last row. Store the maximum value. This link should give you a good starting point Commented May 6, 2016 at 16:20
  • 1
    @ScottCraner Have a snickers, you are going blind, that is the same link I posted 8) Better?. Commented May 6, 2016 at 16:27
  • I did not see the edit sorry. On vacation and using my phone. Commented May 6, 2016 at 16:29
  • My problem is also that I get the last row of the table and not with actual data because the table has some blanks in the last rows and it is giving me wrong number. Commented May 6, 2016 at 16:30
  • Then use the FIND() method in the link provided by @ForwardEd . It will find the last row with data not formatting. Commented May 6, 2016 at 16:32

3 Answers 3

1

Modified from HERE

Sub foo() With Sheets("Sheet9") 'Change to your sheet If Application.WorksheetFunction.CountA(.Cells) <> 0 Then lastrow = .Range("A:DD").Find(What:="*", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row Else 'Used 1000 to prove that it was not defaulting to this. 'Change to 1 when using in actual code. lastrow = 1000 'Change to 1 when using. End If MsgBox lastrow End With End Sub 

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Else lastrow= 1000? shouldn't that be 1 for an empty worksheet? Assuming that is the only time you would fall into the else statment
@ForwardEd yes but I wanted to show that it was not defaulting to the else. I will change it and put a note.
0

And to adjust the range a bit, change the two column letters in these two lines.

This one searches over columns A:DD.

lastrow = .Range("A:DD").Find(What:="*", _ After:=.Range("A1"), _ 

This version narrows the search range to Y:DD

lastrow = .Range("Y:DD").Find(What:="*", _ After:=.Range("Y1"), _ 

1 Comment

And with a little voodo, if you are interested in also getting the column where the lastrow was found but don't want to exract it from .Address, use this.
0

And with a little voodoo, if you are interested in also getting the column where the last row was found but don't want to extract it from .Address, use this.

sub voodoo() Dim theresult As Variant With Sheets("Sheet9") 'Change to your sheet If Application.WorksheetFunction.CountA(.Cells) <> 0 Then Set theresult = .Range("Y:DD").Find(What:="*", _ After:=.Range("Y1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False) Else 'Used 1000 to prove that it was not defaulting to this. 'Change to 1 when using in actual code. lastrow = 1000 'Change to 1 when using. End If MsgBox ("LastRow " & theresult.Row & " column " & theresult.Column) End With End Sub 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.