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])
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:
Excel VBA Delete Rows
(1 answer)
Faster way to delete rows 40k+ rows at once
(2 answers)
Closed 3 years ago.
The below code only seems to work on the first result. I have to re-run it for the other results to be removed. Could anyone take a look and tell me why please? Thanks
I've tried copy and pasting the code several times to compensate but it gives me an error about duplicate code
Dim cell As Range
For Each cell In [AE1:AE2000]
If cell.Value = "REMOVE" Then Range(cell.Offset(0, -5).Address & ":" & cell.Offset(0, 0).Address).Delete Shift:=xlUp
Next cell
One thing you must do when deleting rows is loop backwards; otherwise you may skip rows, which is what is happening to you by the sounds of it. You have to use a counter when looping backwards, such as
Sub x()
Dim r As Long, cell As Range
Set cell = Range("AE1:AE2000")
For r = cell.Count To 1 Step -1
If UCase(cell(r).Value) = "REMOVE" Then
Range(cell(r).Offset(0, -5), cell(r)).Delete Shift:=xlUp
End If
Next r
End Sub
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:
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)