VBA

Moderators: PavlinII
Number of threads: 1614
Number of posts: 3000

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Delete entire ROW if a cell contains a phrase Posted by heweaver on 1 Dec 2008 at 10:53 AM
I am trying to write a small piece of code for an Excel macro. I am working with some ladies here who use an excel spreadsheet to export some of the numbers from their system to their customer.

I need a quick macro that can examine each line of the .xls file and if a certain phrase appears in a cell (for instance, any cell in column C) then have it delete that entire line.

Additionally, if another macro (or maybe even an extension of the first macro, but it may be easier to have two separate macros) that could look at another row, like row FIVE, and if it contains all zeros for cells D-AA (or whatever it goes to) then I would like to have it remove that entire row if it has nothing but zeros from a certain cell over.

Any help would be greatly appreciated. I haven't had the privilege to work much with Excel with regards to creating macros and such.

Thanks in advance!
Report
Re: Delete entire ROW if a cell contains a phrase Posted by shasur on 1 Dec 2008 at 11:29 PM
Hi

Here is a simple one that checks for certain text (Beauty) in column C of the current sheet and deletes the entire row if it is found

Sub DeleteRow()
    Dim i1 As Long
    Dim iMax As Long
    Dim sText As String
    
    sText = "Beauty"
    sText = LCase(sText)
    iMax = Cells.SpecialCells(xlCellTypeLastCell).Row
    
    For i1 = iMax To 1 Step -1
        If InStr(1, LCase(Cells(i1, 3)), sText) <> 0 Then
            Rows(i1).EntireRow.Delete
        End If
    Next i1
    

End Sub


Cheers
Shasur

: I am trying to write a small piece of code for an Excel macro. I am
: working with some ladies here who use an excel spreadsheet to export
: some of the numbers from their system to their customer.
:
: I need a quick macro that can examine each line of the .xls file and
: if a certain phrase appears in a cell (for instance, any cell in
: column C) then have it delete that entire line.
:
: Additionally, if another macro (or maybe even an extension of the
: first macro, but it may be easier to have two separate macros) that
: could look at another row, like row FIVE, and if it contains all
: zeros for cells D-AA (or whatever it goes to) then I would like to
: have it remove that entire row if it has nothing but zeros from a
: certain cell over.
:
: Any help would be greatly appreciated. I haven't had the privilege
: to work much with Excel with regards to creating macros and such.
:
: Thanks in advance!
:

VBA Tips & Tricks (http://vbadud.blogspot.com)

C# Code Snippets (http:dotnetdud.blogspot.com)
Report
Re: Delete entire ROW if a cell contains a phrase Posted by slong on 29 Oct 2012 at 6:41 AM
Hello,
I tried this example in my own macro, but I (unsuccessfully) tried to convert to just clear the contents of the cell if certain (parts of) words appeared. Can you tell me what I did wrong?

Dim i1 As Long
Dim iMax As Long
Dim sText As String

sText = "cipan" Or "ce Ty"
sText = LCase(sText)
iMax = Cells.SpecialCells(xlCellTypeLastCell).cell

For i1 = iMax To 1 Step -1
If InStr(1, LCase(Cells(i1, 3)), sText) <> 0 Then
Cells(i1).ClearContents
End If
Next i1

Report
Re: Delete entire ROW if a cell contains a phrase Posted by slong on 29 Oct 2012 at 6:43 AM
Hello,
I tried this example in my own macro, but I (unsuccessfully) tried to convert to just clear the contents of the cell if certain (parts of) words appeared. Can you tell me what I did wrong?

Dim i1 As Long
Dim iMax As Long
Dim sText As String

sText = "cipan" Or "ce Ty"
sText = LCase(sText)
iMax = Cells.SpecialCells(xlCellTypeLastCell).cell

For i1 = iMax To 1 Step -1
If InStr(1, LCase(Cells(i1, 3)), sText) <> 0 Then
Cells(i1).ClearContents
End If
Next i1




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.