Solved

How do I delete rows in MS Excel VBA if a condition is met

Posted on 2016-08-09
6
20 Views
Last Modified: 2016-09-01
I know how to delete a row in Excel with VBA, but where I am struggling is how to delete rows if a certain condition is met.  For example for any row where Column H (Status) is = to 'Closed' then I wish to delete these rows.  Suggestions ?
0
Comment
Question by:upobDaPlaya
  • 3
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points (awarded by participants)
ID: 41749816
You may try this....

Sub DeleteRows()
Dim lr As Long, i As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "H").End(xlUp).Row
For i = lr To 2 Step -1
    If Cells(i, "H") = "Closed" Then
        Rows(i).Delete
    End If
Next i
Application.ScreenUpdating = True
End Sub

Open in new window

1
 
LVL 31

Expert Comment

by:Rob Henson
ID: 41750001
Apply a filter to the data and Column H filter so only "Closed" is visible.

Select the visible rows or even just one column of visible rows and delete. The rows hidden by the filter will be unaffected.

Thanks
Rob H
0
 

Author Comment

by:upobDaPlaya
ID: 41755180
It works great.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:upobDaPlaya
ID: 41755181
Thanks for the assist it works great...
0
 

Author Comment

by:upobDaPlaya
ID: 41755182
Thx for the assist
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41779497
The chosen answer resolves the issue considering the original requirement was to delete the rows based on a condition through VBA not with manual steps.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now