Conditional If then structure VBA

WindsorKnot

Board Regular
Joined
Jan 4, 2009
Messages
160
Hi,

I'm having trouble with my code below.

Basically I want the code to determine if the cell before and after the active cell contains a value and if both these conditions are true to delete the active row.

I keep getting a runtime error 1004 when I try this.

Thanks!

Code:
Sub rowdelete()
    Dim i As Integer
    Sheet4.Activate
    For i = 1 To 30000
        If Cells(i, 1).Value = IsEmpty(ActiveCell) Then
            If Cells(i, 1).Offset(-1, 0).Value = "" _
            And Cells(i, 1).Offset(1, 0).Value = "" Then
                Cells(i, 1).Select
                Selection.Delete Shift:=xlUp
            End If
        End If
    Next i
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
There's no need to select and if you are using a For Next Loop to delete you want to work backward through the rows not forward. Also you can't check an offset of above Row 1 because there is no such row there, no A0.

Your description says you want to delete the row if the row before it and the row after it have a value but your code checks for blank, which is it?
 
Upvote 0
There's no need to select and if you are using a For Next Loop to delete you want to work backward through the rows not forward. Also you can't check an offset of above Row 1 because there is no such row there, no A0.

Your description says you want to delete the row if the row before it and the row after it have a value but your code checks for blank, which is it?

If the cell before and after have a value and active cell is blank
 
Upvote 0
You should take HOTPEPPERS advice and not select for starters.

Your error in your existing code is here:

Code:
Cells(i, 1).Value = IsEmpty(ActiveCell)

I believe it should be:
Code:
IsEmpty(Cells(i,1))

If you need help implementing HOTPEPPERS suggestion post back.

Cheers

Dan
 
Upvote 0
its been a late night. This is current code same runtime error 1004

Sub rowdelete()
Dim i As Integer
Sheet4.Activate
For i = 1 To 30000
If IsEmpty(Cells(i, 1)) Then
If Not IsEmpty(Cells(i - 1, 1)) _
And Not IsEmpty(Cells(i + 1, 1)) Then
Rows(i, 1).Delete
End If
End If
Next i
End Sub
 
Upvote 0
Do you actually have 30,000 rows of data?

Does this work for you?

Code:
Sub test()
Dim i As Long
For i = 30000 To 2 Step -1
    If Cells(i - 1, 1) = "" And Cells(i + 1, 1) = "" Then Rows(i).Delete
Next
End Sub
 
Upvote 0
Do you actually have 30,000 rows of data?

Does this work for you?

Code:
Sub test()
Dim i As Long
For i = 30000 To 2 Step -1
    If Cells(i - 1, 1) = "" And Cells(i + 1, 1) = "" Then Rows(i).Delete
Next
End Sub

HotPepper you truely are an MVP this works perfectly. Yes I really do have 30,000 rows of data. Its from an incurred/paid lag triangle report
 
Upvote 0
Just a note, if you wanted to do this manually instead of with code, Put a number next to the rows (Put in 1 then 2 then highlight and double click the bottom right corner of the selection)

Sort by any column where the blanks appear

Delete the blank rows

Resort by your row numbers you entered earlier.

Done

Should take about 15 seconds to do this.

Cheers

Dan
 
Upvote 0
HotPepper, the cat just ate my toungue. I've noticed that it skipped over some rows and the loop doesn't end until I force it to end :( any follow ups?
 
Upvote 0

Forum statistics

Threads
1,203,491
Messages
6,055,727
Members
444,814
Latest member
AutomateDifficulty

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top