Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBA Delete function

Posted on 2009-12-24
5
Medium Priority
?
234 Views
Last Modified: 2012-05-08
I'm trying to write a peice of code that will go through a table of records, and based on two fields where that data matches, it deletes all other records and just keeps those records where the two fields have dups.  I've posted my code below.  Can someone help me get this right?

Thanks.
Sub delete()
    Dim rng As Range
    Dim Celle As Range
         
    Set rng = Range("b2:b" & Cells(65536, "b").End(xlUp).Row)
    
    
    For Each Celle In rng
        If Celle = Celle.Offset(-1, 0) And Celle.Offset(0, 2) <> Celle.Offset(-1, 2) Then
            Celle.Offset(-1, 0).EntireRow.delete
            Else
                If Celle <> Celle.Offset(-1, 0) Then
                    Celle.Offset(-1, 0).EntireRow.delete
                End If
        End If
        
    Next Celle

End Sub

Open in new window

0
Comment
Question by:Luis5011
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:ioane
ID: 26120681
Hmmm...It really depends on exactly what you're wanting to delete.

The example code is nothing like what you've described above.

Something like this perhaps?
Sub delete()
    Dim rng As Range
    Dim Celle As Range
         
    Set rng = Range("b2:b" & Cells(65536, "b").End(xlUp).Row)
    
    For Each Celle In rng
        If Celle <> Celle.Offset(-1, 0) Or Celle.Offset(0, 2) <> Celle.Offset(-1, 2) Then
            Celle.Offset(-1, 0).EntireRow.delete
        End If
    Next Celle

End Sub

Open in new window

0
 
LVL 35

Expert Comment

by:Norie
ID: 26121925
I'm sorry that doesn't make a lot of sense, and I can see one potential flaw in the code you've posted - calling the sub 'delete'.

Bad idea.
0
 

Author Comment

by:Luis5011
ID: 26134455
Imnorie, are you referring to my code or that which Tamtrak posted...or both?
0
 
LVL 13

Accepted Solution

by:
ioane earned 1200 total points
ID: 26167680
Luis5011,

How did the code I gave you go?

Imnorie was right about calling the sub delete as it is a keyword that most objects contain in vba to delete instances of themselves and, if not careful, you could delete something you don't want to by mistake.


0
 

Author Closing Comment

by:Luis5011
ID: 31669838
Tramtrak,
Yes, it did work, with some modifications I made based on some things I changed in my data file.  I also changed the name of the procedure.

Thanks for your help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

810 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