• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 677
  • Last Modified:

Excel - VBA (Identifying Last Row)

I have a complex macro where I declared my last row on the spreadsheet as follows:

Open in new window

The first 6 columns on the spreadsheet only has data in the header and no other information.
The 7th column is where my data actually start.
LastRowGDTS1 = Cells(Rows.Count, 7).End(xlUp).Row

Open in new window

I am inputting formulas into columns 1-6 and coding for it to be copied down to the last row where data is based on the data starting in column 7.
For some reason, the formula is being copied down to approximately 200 extra rows where there is no data.  What am I doing wrong?

Here is how I declared my last row:
LastRowGDTS1 = Cells(Rows.Count, 7).End(xlUp).Row

Open in new window

Here is the code I used to copy the formulas down in columns 1-6
'Copy formulas down
    If LastRowGDTS1 > 2 Then Selection.AutoFill Destination:=Range("A2:F2" & LastRowGDTS1)
  • 2
1 Solution
Martin LissOlder than dirtCommented:
Here is the text of an article that I found on the web a long time ago. Try one or more of them depending on what your data looks like,

LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
which is not very exact, because Excel doesn't keep track of the last cell in a very adequate form.
Another method to find the last used row in a particular column is:

lngLastRow = Range("A65536").End(xlUp).Row
LstRow = .Cells(Rows.Count, 1).End(xlUp).Row

but this doesn't tell you FOR SURE the last used row in the entire sheet, unless you can be certain that Column A holds the data.
A couple extra methods are more reliable.
LastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row¿or¿LastRow = ActiveSheet.UsedRange.Rows.Count
ArisaAnsarAuthor Commented:
Thanks for responding.  I used this code to declare my last row but it does not seem to be working.  As mentioned, the 1st column to have data is column 7.

LstRow = .Cells(Rows.Count, 1).End(xlUp).Row

Open in new window

Which is the best way to do this so that it accurately find the last cell in column 7 and then copy the formulas in column A-F down on to that last cell in column 7?
NorieVBA ExpertCommented:
There's nothing wrong with the way you are finding the last row.

The problem is that you have A2:F2 & LastRowGDTS1.

This means that if LastRowGTDS1 is 10 you will fill A2:F210, rather than A2:F10.

Try this.
 If LastRowGDTS1 > 2 Then Selection.AutoFill Destination:=Range("A2:F" & LastRowGDTS1) 

Open in new window

ArisaAnsarAuthor Commented:
Thanks.  That worked.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now