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
Related
I am trying to select all rows and columns so that i can apply Pivot. There are 12327 rows in all and 7 columns. But LastRow2 and LastCol2 both display row count value ie 12327 but actual count of column is only 7.
With Sheet2
LastRow2= .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol2= .Cells(1, .Columns.Count).End(xlToLeft).Column
Set PRange2= .Range("A1", .Cells(LastRow2, LastCol2))
End With
MsgBox LastCol2 & " " & LastRow2
This is second pivot in my table, this code shows correct values in first pivot table code.
Well, there are a few possible traps:
The documentation of Range.End is not very good. It tells you, it gives the same result as some keyboard combination - that is far from a clear definition. That's why I really try to avoid Range.End altogether and use Find instead (inside some udf).
Since it is not clearly documented, it may very well have a different definition of "last" and of "empty". Let me be more clear:
If your range is more than one column, whose last row will it pick?
If your cell had changes in formatting, do you consider it empty? Does Range.End treat it the same way?
Hope this helps. I'm not allowed to comment, just to answer, to that's what I had to do ;)
Best,
YeOldHinnerk
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.
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.
I am developing a dashboard in excel. And I am looking for calculating row count. (How many records are present) ..
Since there are some blank cells I thought to go from bottom to up. I use the following
Range("A1048576").Select
Selection.End(xlUp).Select
After this execution the active cell is at A113 which means the row count is 113.
My question is how to get this number 113 from the active cell?
You can use this:
Dim lastrow as Long
lastrow = Cells(Rows.Count,"A").End(xlUp).Row
lastrow will contain number of last empty row in column A, in your case 113
Here is what I usually use for that:
lastrow = WorksheetFunction.CountA(Columns("A:A"))
This will return the number of non-empty cells in Column "A" which is what I think you're after. Hope this helps.
The best way to get the count of rows/records (in most cases) is to use .UsedRange.Rows.Count. You can assign the return value to a variable like this:
lastRow = Sheets(1).UsedRange.Rows.Count
If you use a function that includes a column (such as column A) as shown in other examples, that will only get you the count of rows in that column, which may or may not be what you're going for. One caveat: if you have formatted rows below your last row with a value then it will return that row number.
If there is a slight chance that the last row of the worksheet is not empty, you should add an IsEmpty() check to #simoco 's solution. Therefore; following is a function that returns the last used row and check if the last row of the worksheet is empty:
Function lastRow(WS As Worksheet, iColumn As String) As Long
If Not IsEmpty(WS.Range(iColumn & WS.Rows.Count)) Then
lastRow = WS.Rows.Count
Else
lastRow = WS.Range(iColumn & WS.Rows.Count).End(xlUp).Row
End If
End Function
I would like to copy the value of the last row in a specific Column in to another cell,
for example this is the code which I a suing to find which is the last used rown in the column G
Dim LastRow As Long
LastRow = Range("G65536").End(xlUp).Row
Right now LastRow get's the value of the address of the row, I need this code modified this way so it will copy the Value of the LastRow (not the address) and than past this value of this cell in to another Cell with address "Q1"
If anywone can Help let me know, thanks!
According to Microsoft MVP:
You should not use numbers because different versions of Excel have different number of maximum rows.
.Cells(.Rows.Count, "Q") is the very last cell at the bottom of column Q.
.Cells(.Rows.Count, "Q").End(xlUp).Row is the row number of the last non-blank cell in column Q.
So you should use
Cells(Rows.Count, "G").End(xlUp).Row
To find number of rows used:
ActiveSheet.UsedRange.Rows.Count
Range.Copy method has parameter which allows you to copy Range into required destination.
According to MSDN Range.Copy syntax looks as follows:
expression.Copy(Destination)
expression A variable that represents a Range object.
In your situation you can call it as follows:
Range("G65536").End(xlUp).Copy Range("Q1")
Where Range("Q1") should be changed to any other cell if required