How to delete rows based on empty cell value in VBA [duplicate] - vba

This question already has answers here:
Code in VBA loops and never ends. How to fix this?
(2 answers)
Closed 5 years ago.
I am trying to delete entire rows based on whether the cell value in the D column is NULL or not. My entire code so far is:
Sub DeleteNULL()
Dim i As Long
For i = 2 To 119713
If IsEmpty(Range("Di")) = True Then
Rows([i]).EntireRow.Delete
Else
If IsEmpty(Range("Di")) = False Then
Next i
End If
End Sub
I keep getting compile errors, either If without Else or Next without For, how should I fix this?
Thanks in advance.

A few things:
Placement of a lot of syntax is off.
When adding or deleting rows, you need to loop backwards based on how Excel handles these events.
See code below:
Sub DeleteNULL()
Dim i As Long
For i = 119713 To 2 Step -1
If IsEmpty(Range("D" & i)) Then Rows([i]).EntireRow.Delete
Next i
End Sub

Related

For loop from 1 to incrementing value [duplicate]

This question already has answers here:
Change length of For loop while in the loop
(3 answers)
Closed 4 years ago.
I'm trying to run "for loop" certain times and occasionally add an extra row in the range being under the loop. Let's say, that example has originally 15 rows, and during the loop, the condition is true 2 times, so it should add +2 to the total number of rows. However, the code doesn't seem to execute the loop for those added rows and exits right after passing the value of orc=15.
Code below:
sub loop_to_orc()
dim i as integer, orc as integer
dim operations as range
set operations = range(cells(1, 1), cells(, 1).end(xldown))
orc = operations.rows.count
for i = 1 to orc
if cells(i,1)>0 then
rows(i+1).insert
end if
orc = operations.rows.count
next i
end sub
Where am I wrong? Is there any method to actually run the loop for added rows?
You could introuduce one additional variable:
Dim rowsAdded As Long
rowsAdded = 1
And then use it like this:
for i = 1 to orc
if cells(i,1)>0 then
rows(i+rowsAdded).insert
i = i - 1
rowsAdded = rowsAdded + 1
end if
next i
This way, you won't increment i when rows is inserted, but you select i in a loop, so we have to add rowsAdded.

Deleting Columns based on header value, withut skipping columns. Using VBA

I am trying to delete columns in the data I have based on the header values, using VBA. I am definitely a novice at this, so would appreciate any help. At the moment I have managed to find some code that can do this, except every time I run the macro it deletes some of the columns, but appears to skip some of the columns. I think because when a column is deleted the column then moves to a new location, ie F5 gets deleted so G5 moves to F5 and then manages to escape the query. This is the code
Sub DeleteSpecifcColumn()
Set MR = Range("A1:D1")
For Each cell In MR
If cell.Value = "old" Then cell.EntireColumn.Delete
Next
End Sub
This is a classical problem ,If you want to delete rows or columns always begin deleting from the end .
Sub DeleteSpecifcColumn()
For i = 4 To 1 Step -1
If Cells(1, i) = "old" Then
Cells(1, i).EntireColumn.Delete
End If
Next i
End Sub

Can't copy formula? [duplicate]

This question already has answers here:
How do I put double quotes in a string in vba?
(5 answers)
Closed 5 years ago.
I can't operate the code without it stating the sub or function is not defined in the formula I'm trying to paste down column v.
Sub SEALANTSCHEDULIZER()
' 1. Unhide All Columns
Columns.EntireColumn.Hidden = False
' 2. Clear All color highlights
ActiveSheet.UsedRange.Interior.ColorIndex = 0
' 3. Drag formulas in all columns down to last row (if necessary) – use column A (ID) as row count
LastRowColumnA = cell(Rows.count, 1).End(xlUp).Row
Range("V2:V" & LastRowColumnA).Formula = "=IF(OR(W2="Yes", AE2="Yes"), "Yes", "No")"
End Sub
You need to double the " around the text strings.
"=IF(OR(W2=""Yes"", AE2=""Yes""), ""Yes"", ""No"")"

Can't get excel macro to return after a for loop [duplicate]

This question already has answers here:
VBA macro to delete rows quickly
(4 answers)
Closed 5 years ago.
I'm trying to make a little code that deletes an entire row when certain text is written in a cell.
Sub Delete_Rows()
For Each c In Range("B1:B20").Cells
If c.Value = "text" Then
c.EntireRow.Delete
End If
Next c
End Sub
This is what I wrote in my excel sheet
The problem is when I run it and the condition to delete a cell is met, excel automatically scrolls to the next row without evaluating the current cell, it skips like this
I run it again and only "text" from numbers 4 and 9 remain
After I run it a third time it finally deletes every cell with "text" written in it.
I tried to use a while loop instead, offset the selection, use an integer to subtract the loop iteration and several other ways and have had no luck.
Any ideas?
Two options are:
delete from the bottom up, or
delete all relevant rows at once (shown below):
Sub Delete_Rows()
Dim deleteRange As Range
For Each c In Range("B1:B20")
If c.Value = "text" Then
If deleteRange Is Nothing Then
Set deleteRange = c
Else
Set deleteRange = Union(c, deleteRange)
End If
End If
Next c
deleteRange.EntireRow.Delete
End Sub

Replacing IF with loops [duplicate]

This question already has answers here:
VBa conditional delete loop not working
(4 answers)
Closed 8 years ago.
I have a spreadsheet with a load of values in and a button.In the code button I have the
following VBA code
If Range("N2").Value = "100" Then
Rows ("2:2").Select
Selection.Delete
End if
The idea is that if 100 is placed in that row then that row will be deleted.But obviously there will be problems if I have loads of rows,this will mean loads of "IFs"
My question is this,is there any way this could be done with a loop.So I could jsut loop through all the rows and if the conditions are met then that Row will be deleted.In the If statement above I have to specify the rows selected,so how would a loop do this??
Any help greatly appreciated.
kind regards
j
Set a range and do the following loops
For Each row In rng.Rows
For Each cell in row.Cells
'Do Something
Next cell
Next row