This question already has answers here:
Delete large number of rows (e.g. ~500k rows) based on a certain criteria
(4 answers)
EntireRow.Delete performance issue
(2 answers)
Faster way to delete rows 40k+ rows at once
(2 answers)
Closed 3 years ago.
I am relatively new to VBA.
I have made a tool for cleaning my cross references.
The way it works, it that i have the complete raw list which i loop through to delete rows where a certain condition is met.
Example:
'RACKS LOOP
'if checkbox is checked
If Sheet1.CheckBox14.Value = True Then
'loop
Dim rngRK As Range
Dim iRK As Integer, counterRK As Integer
'Set the range to evaluate to rngRK.
Set rngRK = Range("D1:D32000")
'initialize iRK to 1
iRK = 1
For counterRK = 1 To rngRK.Rows.Count
'If cell i in the range contains the value: "RACKS",
'delete the row.
If rngRK.Cells(iRK) = "RACKS" Then
rngRK.Cells(iRK).EntireRow.Delete
Else
'Else increment i
iRK = iRK + 1
End If
Next
'end loop
Else
End If
I have 30 loops like the above in this file, as i have many different conditions i need to be able to choose from.
The only issue is that i think it takes a lot of time to process.
Is there any smarter/faster way to do this?
Related
This question already has answers here:
Excel VBA: Delete rows if value not equal to a value?
(2 answers)
For Loop not fully cycling in excel VBA [duplicate]
(1 answer)
For Each loop won't delete all rows with specific values
(2 answers)
Closed 1 year ago.
I have a set of data where I'm trying to remove the N/As and shifts the data to the left. Here's my piece of code:
For Each Cell In Range
If Cell.Value = "NA" Then
Cell.Delete Shift:=xlToLeft
End If
Next Cell
However when I have two consecutive NAs, the first NA delete will shift the second NA into the first cell, where the loop is not moving onto the next cell, so as an end result I would have to run the macro multiple times to get all the NAs deleted. I tried to write a Do Until loop on top of it saying something like "do until Range.value <> "NA"", and it's erroring out. What's the best way around this?
Thanks advance for the help!
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:
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:
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)