vba delete entire row based on input value

abhinav023

New Member
Joined
Jul 29, 2014
Messages
1
Hi

I'm trying to write a code so that excel would go through each cell in a range and if the cell value is equal to "inputbox entered value" it will write ok and delete everything else.

The problem is that if i'm assigning any other value in adjacent column or changing font types etc. then it works fine. but if i use the delete row option, it skips a few item. Please find below the code i wrote:-

Sub LoopA()
Dim cell As Range
Dim Portionnumber As Integer
Dim Msg As String

Portionnumber = InputBox("Enter the portion to be billed")

If Portionnumber <> 1001 And Portionnumber <> 1002 And Portionnumber <> 1003 And Portionnumber <> 1004 Then
Msg = "Enter a valid Portion number i.e. 1001/1002/1003/1004"
MsgBox Msg
Exit Sub
End If

For Each cell In Range("A:A")
If IsEmpty(cell) Then
Exit Sub

ElseIf cell.Value = "Portion" Then
cell.Offset(0, 1).Activate
ActiveCell.Value = "Type"
ElseIf cell.Value = Portionnumber Then
cell.Offset(0, 1).Activate
ActiveCell.Value = "OK"
Else: cell.EntireRow.Delete

End If
Next cell

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Welcome to the Board!

Whenever deleting entire rows, you want to loop through your range backwards, otherwise you will miss rows (as your loop moves down and your data moves up).

I would find your last row with data like this:
Code:
Dim myLastRow as Long
myLastRow = Cells(Rows.Count,"A").End(xlUp).Row
Then loop through all your cells in column A like this:
Dim myRow as Long
Code:
For myRow = myLastRow to 1 Step -1
    ...
Next myRow
You would reference column A within your loop like this:
Code:
Cells(myRow,"A")
likewise, column B would be:
Code:
Cells(myRow,"B")

Try re-writing your code with that in mind, and see if you can get it to work.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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