Conceptual understanding of xlDown - excel

We find last row using code line
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
I am refreshing my conceptual understanding of xlDown. I refer to data in column B in Sheet1 as appended below.
My understaqnding of End(xlDown) has been that it searches from the top down finding the last used cell before a blank cell.Its concept can be simulated by pressing ctrl down. If we highlight a column that contains data intermixed with blanks and then press ctrl down - it will go to the cell before the first blank cell. Pressing ctrl down in Column B Sheet 1 takes to B2 and then to B18. But if I use the following code snippet I get the last row as 1048576.
What logic this code snippet follows?
Can anyone explain it please to clarify this trivial issue.

You will get the same result if you remove .End():
lastRow = .Cells(.Rows.Count, "B").Row
This is because ws.Cells(ws.Rows.Count, "B") is already the last possible cell of the B column. Calling .End() on it is allowed, but does nothing because there is nowhere to go.
Otherwise your understanding is correct.

Related

finding last row of highlighted cell

How do i find what the last row of of a column that is highlighted?
Currently I only know how to find last row that is used.
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
You are not 100% correct with your statement - with your current code you will get the row number of the last row of column A (that's what the 1 in your statement stands for) - not neccessarily the last row in use of the whole sheet.
If you want to know the last row of a specific column, just change this 1 to the column number you are interested. Probably with "highlighted" you mean the active cell, so that would be
LastRow = Cells(Rows.Count, Activecell.Column).End(xlUp).Row
A rather complete discussion about how to get the last row/column/cell can be found at Find last used cell in Excel VBA
If I have understood your requirement correctly then you will need an approach like this.
lngLastRow = Selection.Cells(Selection.Cells.Count, 1).Row

Copy range to last blank line

I have an Excel file with three sheets with ten columns (A through J).
Sheet1 is a data entry sheet that uses VBA code to transfer a record to the next blank line in Sheet2 where each data entry row is stored. This is working.
Sheet3 is an EDIT sheet to modify a previously-entered record on sheet2.
Using the EDIT sheet, I have VBA code that looks for the matching claim number on sheet2 and clears out the comments.
PROBLEM:
The next time there is a record with comments transferred to sheet2 using the ".End(xlUp)" method it puts that comment on J2 vs. J10 because J1-9 were empty.
QUESTION:
How can I put the comment for claim #10 on J10?
As a workaround I put a dash on each empty line where there are no comments, but that is not an optimal solution as there are many hands that touch that workbook.
In Sheet2 decide on a column that you know will always have a value (Column A for example). Work out the last row number using that column
lastRow = Sheet2.Cells(Rows.Count, "A").End(xlUp).Row + 1
And then use lastRow to update the new comment
Sheet2.Cells(lastRow, "J") = theNewComment

selection of multiple columns with end(xlDown).End(xlUp).Row

I'm doing debug for a VBA template.
The Macro let users choose a excel file to import, then it will check each columns whether they correpond the requirements.
When I try to import a file, I alway get error "oveeflow" with the following line:
'maxRow = ws.Range(Cells(StartRow, AdditionalInfoColumns.OtherInfo).Address).End(xlDown).Row
My colleague tells me that I just need to add end(xlDown).End(xlup) like this:
maxRow = ws.Range(Cells(StartRow, AdditionalInfoColumns.OtherInfo).Address).End(xlDown).End(xlDown).End(xlUp).Row
And it does work! I don't know why. So I try to do it in a simpler way as following:
maxRow = ws.Range(Cells(StartRow, 1).Address, Cells(StartRow, 3).Address).End(xlDown).End(xlUp).Row
It works as well.
Can someone please explain why does it work? Thank you for your answer.
PS: The value of xlDown and xlUp are -4121 and -4162.
Best Regards
Kai
First of all End(xlDown) and End(xlUp) is nothing more than …
End(xlDown) = Pressing Ctrl + Arrow down
End(xlUp) = Pressing Ctrl + Arrow up
you can test this manually by selecting a range/cell and press the combination to see the effect on your actual worksheet.
The overflow error just comes because if you eg. select cell A1 in an empty worksheet and do .End(xlDown) it selects the very last cell of the worksheet. And if you now insert something it fails because the maximum rows of Excel are exceeded.
To find the last used row of a column it's better to use End(xlUp) instead of End(xlDown) eg:
LastUsedRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'finds the last used row in column A
End(xldown) has the same effect as pressing control and the down arrow (and End(xlright) is control and right arrow. If you try those key presses in a cell you will see it jumps to the next intersection of filled and empty cells (or the edge of the spreadsheet if there's no filled cells in the way)

VBA code for copy until last row

How can I modify
XLApp.Range("A1:K1" & LastRow).Copy
if I want to copy the A1:K1 untill the last row there is data in one of the cells in the selected area . Sorry for my poor English.
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
will give you the last row in column no 1 ("A"). For last row in column k, you need to use 11 in place of 1.
The code below shows a generic way to do that:
Range(Range("A1", Range("A1").End(xlToRight)), Range("A1", Range("A1").End(xlDown))).Rows.Count
you can change the A1 reference to other if you want. The codes copy a range the starts from A1 to right and from right to down.

.End(xlToRight) within a range

I've just got a quick one wondering if I can somehow alter the following snippet to include .End(xlToRight) instead of defining L6 (the result is the same).
Sub Test()
Dim LastCol As String
With Worksheets("Sheet1")
LastCol = .Cells(5, .Columns.Count).End(xlToLeft).Address
.Range(Range("A5"), LastCol).Copy
.Range("B5:L5", Range("B5:L5").Offset(LastRow - FirstRow, 0)).PasteSpecial xlPasteFormulas
.Range("B6", .Cells.SpecialCells(xlCellTypeLastCell)).Copy
.Range("B6").PasteSpecial xlPasteValues
End With
End Sub
Many thanks for any help :)
EDIT: updated pdw TonyDallimore (see response below)
Tony, the above code is the sum of what I've been trying to get working. Within the with statement, I'm looking to copy the contents of row 5, and paste them down to the nth row - which is defined by a list already present in columnA. The last line per your advice will then paste the values of all but the top row (r5) to preserve transparency for the user, while minimising file size.
The middle bit is the remaining 'work in progress', as L5 is not certain to be the farmost column.
Both questions
.End(xxx) is the same as Ctrl+Arrow from the keyboard and stops at the first cell that is different from the current cell. So if you start at a cell with a value it stops at a cell without a value or vice versa.
The trouble with starting top left and using xlDown and xlToRight is that it will stop at a blank cell in the middle of your table. Fine if you absolutely cannot have a blank cell in the middle, but XlUp or xlToLeft from bottom right are safer.
Question 1
Is your problem that .End(xxx).Column will return 12 and you do not know how to turn it into the letter "L"?
Is so, there are lots of choices. I think the easiest is to remember that
.Cells(6,Columns.Count).End(xlToLeft).Address
would return "$L$6".
Question 2
Does .Cells(1000, ColRange) represent the bottom right cell?
.Cells.SpecialCells(xlCellTypeLastCell) might be an easier option.

Resources