This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I find last row that contains data in the excel sheet with a macro?
Error Finding Last Used cell In VBA
If I have a spreadsheet and the largest row number which has data is 10,434 is there a function which will give me the row number 10,434?
Thank you
Sub largestrow()
Dim largestrowNum As Long
Dim col As Integer
For col = 1 To Columns.Count
lastrow = Application.WorksheetFunction.Max(Cells(65536, col).End(xlUp).Row, largestrowNum)
Next col
MsgBox "Largest row number =" & largestrowNum
End Sub
(will work as long as you have less than 65536 rows)
Related
This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed last year.
In column A I have several values. As values are added or removed, in excel vba I want to extract all values from column A down to the last value
How can i create this?
Werte = Sheets("Messauswertung").Range("A1:A100").Value
This is not a the optimal way
Thanks!
On way is
dim lastRow as Long
With Sheets("Messauswertung")
lastRow = .Cells(rows.Count, 1).End(xlUp).Row 'last used row
Werte = .Range("A1:A" & lastRow).Value
End With
By the way, each sheet has a .Name but also a .CodeName (Sheet1).
I find it much better to (eventually change the CodeName and) use that one.
Easier to read, to write, and protected from name changes by users.
This question already has answers here:
Excel VBA, getting range from an inactive sheet
(3 answers)
Why does Range work, but not Cells?
(1 answer)
Closed 1 year ago.
I need to filter some data based on duration (column) and copy the ids (other column) of the result to another sheet.
I have tried the following, but I always get 1 as the amount of filtered rows (actually it's over 100) and an error on the copy attempt:
dataSheet.UsedRange.AutoFilter Field:=durationColIndex, Criteria1:="<100"
Dim quickRange As Range, quickCount As Long
Set quickRange = dataSheet.UsedRange.SpecialCells(xlCellTypeVisible)
quickCount = quickRange.Rows.Count '<----- does not work, is always 1
If quickCount > 0 Then
Set specialIdsSheet = Sheets.Add 'defined before
'copy col 1
quickRange.Range(Cells(1, idIndex), Cells(25, idIndex)).Copy '<-- other issue. 25 to substitue the not working quickCount
specialIdsSheet.Cells(1, 1).PasteSpecial
End If
I have tried to work around it for some hours now, but I stuck and don't understand why it is not working.
I had a solution before that was using ActiveSheet and Select, but I wanted to move away from that.
ActiveSheet.Range("A1").CurrentRegion.Range(Cells(1, idIndex), Cells(ActiveCell.CurrentRegion.Rows.Count, idIndex)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Thanks in advance!
This question already has answers here:
Finding the Last Row and using it in a formula
(2 answers)
Closed 5 years ago.
I would like this Code to count the number of incidents in a column starting at Cell A3 and then go the last cell in the column without me having to define it.
ActiveCell.FormulaR1C1 = _
"=COUNTIF(Data!R[-2]C[-3]:R[647]C[-3],Data!R[-2]C[-3])"
Still new to VBA and learning.
Here's a simple way to locate the last used cell in column C. There are several variations to finding lastRow, this is just one of them. Add your code accordingly.
Dim LastRow As Long
Dim lastCell As Long
lastCell = Range("C65536").End(xlUp).Cell '<<--for finding last active cell reference
lastRow = Range("C65536").End(xlUp).Row '<<--for finding last row number
This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 6 years ago.
Follow on from earlier question. I am trying to count the number of rows in a table which contain data (not counting number of rows which are part of the table).
For example, my table is:
My code is:
With ThisWorkbook.Worksheets(1)
Set ATB = .ListObjects("Table1")
.Activate
numberRows = .Cells(.Rows.Count, "A").End(xlUp).Row
End With`
This returns the wrong number (trust me that column A has the same data count)
Likewise, I want to use VBA to resize the table nicely but need this row count for that.
Try this
Sub check()
With ThisWorkbook.Worksheets("Sheet1")
Set ATB = .ListObjects("Table1")
.Activate
numberRows = Application.CountA(.Range("A:A"))
End With
End Sub
Try this excel formula
=COUNTA(Table1[A1])
This question already has answers here:
Copy every nth line from one sheet to another
(8 answers)
Closed 7 years ago.
Suppose I have a table in excel containing 1000 rows and 10 columns.
How can I copy every 7th row from this table to a new table whose first row will be this 7th row, second row will be that table 14th row and so on.
I have never done these kind of things in excel before.
How to do it?
If you want to stick with plain Excel (no VBA). Add two columns at the end of your table. The first being a count of the line, the second flagging if the line count is divisible by 7 - I used the formula =IF(MOD(D4,7)=0,"Divisible by 7", "-").
Then filter the table on the 'Mark every 7th item' column, and copy and paste to new table.
You need a macro. Press alt + F11
Basically you run that macro that goes like this
sub Copyer()
dim I as integer
Dim K as integer
I = 7
K = 1
while (Activesheet.Range("A" & I ).Value <> "")
DestinationSheet.Range("A" & K ).Value = Activesheet.Range("A" & I).Value
K = K + 1
I = I + 7
Loop
End Sub
Code may need some grooming but that's the idea