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
Solved

Vba to delete entire row

Posted on 2016-11-14
6
52 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
6 Comments
 
LVL 33

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 33

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 50

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

809 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