Hello guys I am trying to build a macro that will delete the entire row if a certain value appears in a certain column. For example lets say it goes down column F, I will need the macro to delete entire rows if there is no "1" present. Is creating a macro like this possible. Thanks in advance for the help, I've also posted a sample of the data.
You can probably tweak the solution prvided by Mudraker here ( I don't have access to XL right now)
Quoting entire posts clutters the forum and makes threads hard to read !
If you are pleased with a member's answer then use the Star icon to rate it
Click here to see forum rules
HI,
The following code may help. Copy to a module and change sheet name to the sheet name in your file.
Test on copy only.
Sub FilterDeleteNon1() Application.ScreenUpdating = False Dim VisRng As Range, cell As Range Dim Myval As Integer Dim lrow As Long lrow = Sheets("Sheet2").Range("G65536").End(xlUp).Row Range("A1:G1").Select Selection.AutoFilter With Selection .AutoFilter Field:=7, Criteria1:="<> 1" '' this set the filtered data for the value End With With Worksheets("Sheet2").AutoFilter.Range Set VisRng = .Resize(.Rows.Count - 1, 1).Offset(1, 0) _ .Cells.SpecialCells(xlCellTypeVisible) '' make sure you have more than 1 row '' Myval = .Range("g2:g" & lrow).SpecialCells(xlCellTypeVisible).Count If Myval >= "2" Then Range(Cells(VisRng.Offset(1, 0).Row, 1), Cells(Range("A65536").End(xlUp).Row, 4)).EntireRow.Delete End If End With Selection.AutoFilter End Sub
Charles
There are other ways to do this, this is but 1 !
Be Sure you thank those who helped.
IF YOU'RE SATISFIED BY ANY MEMBERS RESPONSE TO YOUR ISSUE PLEASE USE THE STAR ICON AT THE BOTTOM LEFT OF THE POST UNDER THEIR NAME.
Another version-
I am not able to delete the rows containing #VALUE!. Maybe you can take care of it when you have a formula(i guess vlookup) to make it "" or blank instead of #VALUE!.Option Explicit Dim lrow As Long Dim i As Long Sub delete_rows() lrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row For i = lrow To 1 Step -1 If Range("G" & i).Value <> 1 Then Rows(i & ":" & i).Delete lrow = lrow - 1 End If Next i End Sub
Cheers,
Arlette
If I helped, Don't forget to add to my reputation (click on the star below the post)
Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
Use code tags when posting your VBA code: [code] Your code here [/code]
I have slightly modified the code of Sri arlu1201. This works ok. No modifications necessary. Convey your findings.
Option Explicit Dim lrow As Long Dim i As Long Sub delete_rows() lrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row For i = lrow To 1 Step -1 If IsError(Range("G" & i).Value) Then Rows(i & ":" & i).Delete lrow = lrow - 1 ElseIf Range("G" & i).Value <> 1 Then Rows(i & ":" & i).Delete lrow = lrow - 1 End If Next i End Sub
Last edited by kvsrinivasamurthy; 12-20-2011 at 11:34 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks