Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default variation of "delete all except"


i have some code someone from here was gracious enought to provide
while back. i am trying to create a variation to what i have. i woul
like it to delete all except each criteria but i also want to keep th
four rows immediately following each found exception. is thi
possible? thanks in advance for your help-here is the code:

Sub DeleteAllExcept()
Dim Exceptions As Variant
Dim cell As Range, LastCell As Range
Dim MyDelRng As Range
Dim WB As Workbook, SH As Worksheet
Dim CountryCol As String
Dim blKeep As Boolean

Exceptions = Array("RH - MONTH TOTAL:", "RO - MONTH TOTAL:", "SO
MONTH TOTAL:", "SH - MONTH TOTAL:")
Set WB = Workbooks("poreport-test.xls")
Set SH = WB.Sheets("poreport")
CountryCol = "A"

Set LastCell = SH.Cells(Rows.Count, CountryCol).End(xlUp)
For Each cell In Range(SH.Cells(1, CountryCol), LastCell)
blKeep = False
On Error Resume Next
blKeep = Application.Match(cell.Value, Exceptions, 0)
On Error Resume Next

If Not blKeep Then
If MyDelRng Is Nothing Then
Set MyDelRng = cell
Else
Set MyDelRng = Union(cell, MyDelRng)
End If
End If
Next cell

If Not MyDelRng Is Nothing Then
MyDelRng.EntireRow.Delete
Else
MsgBox "No data found to delete"
End If

End Su

--
acarri
-----------------------------------------------------------------------
acarril's Profile: http://www.excelforum.com/member.php...fo&userid=1002
View this thread: http://www.excelforum.com/showthread.php?threadid=27302

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default variation of "delete all except"

Try this against a copy of your worksheet.

Option Explicit
Sub DeleteAllExcept()

Dim Exceptions As Variant
Dim MyDelRng As Range
Dim WB As Workbook, SH As Worksheet
Dim CountryCol As String
Dim FirstRow As Long
Dim LastRow As Long
Dim iRow As Long

Exceptions = Array("RH - MONTH TOTAL:", "RO - MONTH TOTAL:", _
"SO - MONTH TOTAL:", "SH - MONTH TOTAL:")

Set WB = Workbooks("poreport-test.xls")
Set SH = WB.Sheets("poreport")

'for testing
'Set SH = ActiveSheet

CountryCol = "A"

With SH

FirstRow = 1
LastRow = .Cells(.Rows.Count, CountryCol).End(xlUp).Row

iRow = 1
Do
If iRow LastRow Then
Exit Do
End If

If IsError(Application.Match(.Cells(iRow, _
CountryCol).Value, Exceptions, 0)) Then
'not found
If MyDelRng Is Nothing Then
Set MyDelRng = .Cells(iRow, "A")
Else
Set MyDelRng = Union(.Cells(iRow, "A"), MyDelRng)
End If
iRow = iRow + 1
Else
'skip around this one and the next 4
iRow = iRow + 1 + 4
End If
Loop
End With

If Not MyDelRng Is Nothing Then
MyDelRng.EntireRow.Delete
Else
MsgBox "No data found to delete"
End If

End Sub

acarril wrote:

i have some code someone from here was gracious enought to provide a
while back. i am trying to create a variation to what i have. i would
like it to delete all except each criteria but i also want to keep the
four rows immediately following each found exception. is this
possible? thanks in advance for your help-here is the code:

Sub DeleteAllExcept()
Dim Exceptions As Variant
Dim cell As Range, LastCell As Range
Dim MyDelRng As Range
Dim WB As Workbook, SH As Worksheet
Dim CountryCol As String
Dim blKeep As Boolean

Exceptions = Array("RH - MONTH TOTAL:", "RO - MONTH TOTAL:", "SO -
MONTH TOTAL:", "SH - MONTH TOTAL:")
Set WB = Workbooks("poreport-test.xls")
Set SH = WB.Sheets("poreport")
CountryCol = "A"

Set LastCell = SH.Cells(Rows.Count, CountryCol).End(xlUp)
For Each cell In Range(SH.Cells(1, CountryCol), LastCell)
blKeep = False
On Error Resume Next
blKeep = Application.Match(cell.Value, Exceptions, 0)
On Error Resume Next

If Not blKeep Then
If MyDelRng Is Nothing Then
Set MyDelRng = cell
Else
Set MyDelRng = Union(cell, MyDelRng)
End If
End If
Next cell

If Not MyDelRng Is Nothing Then
MyDelRng.EntireRow.Delete
Else
MsgBox "No data found to delete"
End If

End Sub

--
acarril
------------------------------------------------------------------------
acarril's Profile: http://www.excelforum.com/member.php...o&userid=10027
View this thread: http://www.excelforum.com/showthread...hreadid=273021


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
Excel "Move or Copy" and "Delete" sheet functions dsiama Excel Worksheet Functions 1 December 28th 07 01:57 PM
change "true" and "false" to "availble" and "out of stock" inthestands Excel Worksheet Functions 2 July 19th 07 07:05 PM
Count occurences of "1"/"0" (or"TRUE"/"FALSE") in a row w. conditions in the next BCB New Users to Excel 7 May 13th 06 10:02 PM
Adding "New" "Insert" "Delete" into a workbook to change from data 1 to data 2 etc Bob Reynolds[_2_] Excel Programming 0 March 4th 04 08:52 PM


All times are GMT +1. The time now is 02:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"