Understand .Cells(.Rows.Count,“A”).End(xlUp).row

%3CLINGO-SUB%20id%3D%22lingo-sub-292728%22%20slang%3D%22en-US%22%3EUnderstand%20.Cells(.Rows.Count%2C%E2%80%9CA%E2%80%9D).End(xlUp).row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292728%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22votecell%20post-layout--left%22%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%20class%3D%22postcell%20post-layout--right%22%3E%3CDIV%20class%3D%22post-text%22%3E%3CP%3EI%20was%20just%20wondering%20if%20you%20could%20help%20me%20better%20understand%20what%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E.Cells(.Rows.Count%2C%22A%22).End(xlUp).row%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Edoes.%20I%20understand%20the%20portion%20before%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E.End%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Epart.%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-292728%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-293397%22%20slang%3D%22en-US%22%3ERe%3A%20Understand%20.Cells(.Rows.Count%2C%E2%80%9CA%E2%80%9D).End(xlUp).row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-293397%22%20slang%3D%22en-US%22%3E%3CP%3EVery%20clear%20your%20comment.%3CBR%20%2F%3EThank%20you%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3EJan%20Karel%20Pieterse%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-293341%22%20slang%3D%22en-US%22%3ERe%3A%20Understand%20.Cells(.Rows.Count%2C%E2%80%9CA%E2%80%9D).End(xlUp).row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-293341%22%20slang%3D%22en-US%22%3EOne%20reason%20to%20start%20from%20the%20bottom%20is%20if%20your%20column%20contains%20empty%20cells%20somewhere%20in%20the%20middle%20that%20is%20where%20the%20cursor%20would%20stop.%20Or%20if%20you%20are%20on%20the%20last%20filled%20cell%20and%20then%20hit%20control%2Bdown%2C%20you%20end%20up%20on%20row%201048576.%20Thus%20starting%20from%20row%201048576%20and%20going%20up%20is%20a%20good%20idea.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-293230%22%20slang%3D%22en-US%22%3ERe%3A%20Understand%20.Cells(.Rows.Count%2C%E2%80%9CA%E2%80%9D).End(xlUp).row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-293230%22%20slang%3D%22en-US%22%3E%3CP%3EI%20understand%20better%20now.%3CBR%20%2F%3EThis%20line%20means%3A%20selects%20(.Select)%20the%20last%20non-empty%20cell%20(.End)%20up%20(xlUp)%20from%20the%20last%20cell%20(%26amp%3B%20Rows.Count)%20of%20column%20A%20(Range%20(%22A%22))%3CBR%20%2F%3ESo%20this%20command%20line%20behaves%20exactly%20as%20if%20you%20were%20in%20the%20last%20cell%20of%20column%20A%20(cell%20A1048576%20for%20the%202007%20version%20of%20Excel)%20and%20you%20press%20CTRL%20%2B%20up%20arrow.%3CBR%20%2F%3EWhy%20go%20from%20the%20bottom%20of%20the%20sheet%20to%20the%20top%20and%20not%20the%20other%20way%20around%3F%20Quite%20simply%2C%20to%20avoid%20a%20selection%20error%20if%20it%20turned%20out%20that%20the%20array%20contains%20empty%20cells.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-293032%22%20slang%3D%22en-US%22%3ERe%3A%20Understand%20.Cells(.Rows.Count%2C%E2%80%9CA%E2%80%9D).End(xlUp).row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-293032%22%20slang%3D%22en-US%22%3E%3CP%3EGood%20evening%20Mr.%20Jan%20Karel%20Pieterse%2C%20I%20still%20do%20not%20understand.%20Please%20be%20more%20explicit.%20Why%20use%20this%20piece%20of%20code%3F%20And%20also%20explain%20me%20each%20from%20this%20code%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-292777%22%20slang%3D%22en-US%22%3ERe%3A%20Understand%20.Cells(.Rows.Count%2C%E2%80%9CA%E2%80%9D).End(xlUp).row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-292777%22%20slang%3D%22en-US%22%3EIt%20is%20the%20same%20as%20going%20to%20row%201%20million%20and%20then%20pressing%20control%2Barrow-up%20key.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1970600%22%20slang%3D%22en-US%22%3ERe%3A%20Understand%20.Cells(.Rows.Count%2C%E2%80%9CA%E2%80%9D).End(xlUp).row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1970600%22%20slang%3D%22en-US%22%3EI%20see%20this%20is%202%2B%20years%20old%2C%20please%20can%20I%20seek%20some%20clarifications%3CBR%20%2F%3EPlease%20can%20I%20understand%20this%20way%2C%20the%20sequence%20of%20the%20execution%20is%20confusing%20me%3CBR%20%2F%3ECells(.Rows.Count%2C%E2%80%9CA%E2%80%9D).End(xlUp).row%3CBR%20%2F%3EPlease%20clear%20my%20understanding%2C%20Say%20cursor%20is%20at%20A1%3CBR%20%2F%3ECells()%20is%20an%20object%20having%20the%20method%20End()%2C%20which%20is%20executed%20first%20so%20it%20goes%20to%20the%20end%2C%20while%20going%20to%20the%20end%20it%20calculates%20the%20rows%20(%20or%20while%20traversing%20back%20to%20A1%20%3F)%20so%20now%20we%20have%20total%20rows%20for%20column%20A.%20Please%20can%20you%20explain%20me%20this%20and%20can%20I%20apply%20same%20logic%20to%20all%20functions..%3C%2FLINGO-BODY%3E
Contributor
 

I was just wondering if you could help me better understand what .Cells(.Rows.Count,"A").End(xlUp).row does. I understand the portion before the .End part.

7 Replies
It is the same as going to row 1 million and then pressing control+arrow-up key.

Good evening Mr. Jan Karel Pieterse, I still do not understand. Please be more explicit. Why use this piece of code? And also explain me each from this code

I understand better now.
This line means: selects (.Select) the last non-empty cell (.End) up (xlUp) from the last cell (& Rows.Count) of column A (Range ("A"))
So this command line behaves exactly as if you were in the last cell of column A (cell A1048576 for the 2007 version of Excel) and you press CTRL + up arrow.
Why go from the bottom of the sheet to the top and not the other way around? Quite simply, to avoid a selection error if it turned out that the array contains empty cells.

One reason to start from the bottom is if your column contains empty cells somewhere in the middle that is where the cursor would stop. Or if you are on the last filled cell and then hit control+down, you end up on row 1048576. Thus starting from row 1048576 and going up is a good idea.

Very clear your comment.
Thank you Jan Karel Pieterse

I see this is 2+ years old, please can I seek some clarifications
Please can I understand this way, the sequence of the execution is confusing me
Cells(.Rows.Count,“A”).End(xlUp).row
Please clear my understanding, Say cursor is at A1
Cells() is an object having the method End(), which is executed first so it goes to the end, while going to the end it calculates the rows ( or while traversing back to A1 ?) so now we have total rows for column A. Please can you explain me this and can I apply same logic to all functions..

@chancelin

 

 

  1. Cell(Rows.Count, "A") means that your reference is a cell on column "A" at last row of your sheet "Rows.Count"
  2. End(xlUp) select the first or last filled row to the direction "Up", So if Cell(Rows.Count, "A") is an empty Cell it will go Up untill it finds a filled row
  3. Row returns the number of the row based on the selected Cell

If you have a spread sheet with 1000 lines with data, the Cell(1000,"A") will be your last filled line, Cell(Rows.Count, "A").End(xlUp).Row will return 1000

 

Sub checkLastFilledLine()

Dim lastLine As Long

lastLine = Cell(Rows.Count, 1).End(xlUp).Row

msgbox lastLine

End Sub

 

The above Sub will pop up a message box with the number of the last line on column "A" that is not blank