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
Excel Macro - Delete Row if cell contains Posted by pizzabot on 26 Sept 2007 at 11:42 AM
I'm trying to write an excel macro that will delete a row if the active cell in column B does not contain "21475". The follow is what I have written but when it's ran, it deletes all the rows. I would appreciate any and all help. Thanks in advance.

Sub Delete_Rows_ColB()
' This macro deletes all rows on the active worksheet
' that do not have 21475 in column B.
 Dim rng As Range, cell As Range, del As Range
 Dim strCellValue As String
 Set rng = Intersect(Range("B:B"), ActiveSheet.UsedRange)
 For Each cell In rng
   
   strCellValue = (cell.Value)
   If InStr(strSiteLink, "21475") = 0 Then
       If del Is Nothing Then
          Set del = cell
       Else: Set del = Union(del, cell)
       End If
    End If
 Next cell
 On Error Resume Next
 del.EntireRow.Delete
End Sub
Report
Re: Excel Macro - Delete Row if cell contains Posted by atuna on 3 Sept 2008 at 5:40 AM
: I'm trying to write an excel macro that will delete a row if the
: active cell in column B does not contain "21475". The follow is what
: I have written but when it's ran, it deletes all the rows. I would
: appreciate any and all help. Thanks in advance.
:
:
: 
: Sub Delete_Rows_ColB()
: ' This macro deletes all rows on the active worksheet
: ' that do not have 21475 in column B.
:  Dim rng As Range, cell As Range, del As Range
:  Dim strCellValue As String
:  Set rng = Intersect(Range("B:B"), ActiveSheet.UsedRange)
:  For Each cell In rng
:    
:    strCellValue = (cell.Value)
:    If InStr(strSiteLink, "21475") = 0 Then
:        If del Is Nothing Then
:           Set del = cell
:        Else: Set del = Union(del, cell)
:        End If
:     End If
:  Next cell
:  On Error Resume Next
:  del.EntireRow.Delete
: End Sub
: 
:

if you replace this row
If InStr(strSiteLink, "21475") = 0 Then

with this
If InStr(strCellValue , "21475") > 0 Then

it works.



Report
Re: Excel Macro - Delete Row if cell contains Posted by atezza on 17 Jun 2010 at 7:59 PM
Then how to delete a row if the active cell in column B does not contain "21475" in every worksheet?
Thanks in advance



 

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.