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!
Related
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?
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:
VBa conditional delete loop not working
(4 answers)
Closed 4 years ago.
I have the following For Loop. It is supposed to delete lines with all the . 's.
It is deleting the lines, but when there are rows are like the picture, the code is skipping one of them and deleting one of them.
For row_num = 8 To max_row
page_title = Sheets("Stack").Range("C" & row_num + 1).value
If page_title = " ....................................................................................................................................................................................................................................................................................................." Then
Sheets("Stack").Range("C" & row_num + 1).EntireRow.delete
End If
Debug.Print page_title
Next row_num
Can anyone help with deleting sequential rows that contain .'s ?
This is beacuse the logic of the routine. For example, if you delete row 5, now row 6 will be row 5 and so on, so row 6 will not be read. Insted of delete the row, try setting the values of the column as Empty
Sheets("Stack").Range("C" & row_num + 1).EntireRow = Empty
And you will see all lines will be read.
After you finish, you can tell excel/vba to roganize your matriz according to one column, so empty rows will be organized at the end, is that to say, will become part of the rest empty spacies of the sheet
This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 5 years ago.
How do I adjust the below code to work with the equivalent of crtl shift end in VBA (which I think is xlLastCell or xlCellTypeLastCell)?
My sheet has 64 columns I only want to select Column 1 to 58 where I have data starting on row 2. The below works unless the sheet is blank and then all rows from row 2 to 1048575 are selected. Because I have protected cells below row 200,000 my script errors with the below.
With Worksheet(WorkSheet)
.Range(.Cells(2,1).End(XlDown),.Cells(2,58)).Select
End With
Selection.ClearContents
Start at row 200,000 and look up.
With Worksheet(WorkSheet)
.Range(.Cells(200000, "A").End(XlUP), .Cells(2, "BF")).ClearContents
End With
'possible alternate to avoid clearing row 1
With Worksheet(WorkSheet)
.Range(.Cells(200000, "A").End(XlUP).offset(1, 0), .Cells(2, "BF")).ClearContents
End With
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])