Solved

Vba to delete entire row

Posted on 2016-11-14
6
138 Views
Last Modified: 2016-11-15
Can an expert provide me with VBA code that will delete the entire row if it finds the number 1 in column ‘O’ [must be a singular 1 not 21, 31 etc]

Many thanks in advance
0
Comment
Question by:Jagwarman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 34

Expert Comment

by:Norie
ID: 41887468
Try this.

Dim I As Long

    For I = Range("O" & Rows.Count).End(xlUp).Row Step -1
        If Cells(I, "O").Value = 1 Then
            Rows(I).EntireRow.Delete
        End If
    Next I

Open in new window

0
 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 250 total points
ID: 41887474
Here
Sub Demo()
    Dim lngLastRow As Long
    Dim lngCurrentRow As Long

    With ActiveSheet
        lngLastRow = Range("O" & Rows.Count).End(xlUp).Row
        For lngCurrentRow = lngLastRow To 1 Step -1
            With .Cells(lngCurrentRow, "O")
                If Not IsError(.Value) Then
                    If .Value = "1" Then .EntireRow.Delete
                End If
            End With
        Next lngCurrentRow
    End With
End Sub

Open in new window

0
 

Author Comment

by:Jagwarman
ID: 41887478
Hi Norie

this row gives me a syntax error

For I = Range("O" & Rows.Count).End(xlUp).Row Step -1
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 34

Accepted Solution

by:
Norie earned 250 total points
ID: 41887481
Too early in the morning.:)

This is how it should read.
Dim I As Long

    For I = Range("O" & Rows.Count).End(xlUp).Row To 1 Step -1
        If Cells(I, "O").Value = 1 Then
            Rows(I).EntireRow.Delete
        End If
    Next I

Open in new window

0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41887488
HI,

pls try
Sub Macro1()
'
' Macro1 Macro
'
   Set c = Range("O" & Rows.Count)
   For lCol = 1 To WorksheetFunction.CountIf(Range("O:O"), 1)
        Set c = Range("O:O").Find(What:="1", LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False)
            c.EntireRow.Delete
   Next lCol
 
End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:Jagwarman
ID: 41887494
Many thanks both really good but EE forces me to make one Best.

Regards
Jagwarman
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

635 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