Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
ms ms is offline
external usenet poster
 
Posts: 5
Default SpecialCells Method selects valid records

This procedure is used to delete any records where a negative sign exists
anywhere in a value in the D column. It works fine with multiple records, but
if there is only one record in the file, with or without a negative sign in the
file being formatted, the record is deleted.
If I change the SpecialCells Method to r.SpecialCells(xlCellTypeAllValidation)
it doesn't delete the one record but also does not remove records where the
negative sign exists when multiple records exist.

Here is a sample file with 2 records. One has a negative sign, the other does
not and therefore should not be deleted.
A,1111111111,042804,030171,13174.20
A,1111111111,041904,030221-,4243.30

How can I change this to work in both situations?


Sub CleanCancelledChks()
Dim r As Range
Dim r1 As Range
With ActiveSheet
Set r = .Range(.Range("D2"), .Range("D" & _
Rows.Count).End(xlUp))
.Columns("D:D").AutoFilter Field:=1, Criteria1:="=*-*"
On Error Resume Next
'Extra variable is to prevent specialcells method from returning
error1004 when no negative values(ie. cancelled chks) exist.
Set r1 = Nothing
Set r1 = r.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilterMode = False
If Not r1 Is Nothing Then _
r1.EntireRow.Delete
End With
End Sub


Thanks,
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default SpecialCells Method selects valid records

Using autofilter is clever, but it always treats the top row in the range as
the header row and it will always be visible.

Also, it will always delete it.

So is row2 a header line or a record. You claim it works with multiple
rows, but I suspect you are always deleteing row 2 regardless of whether it
contains a negative or not.

Assuming row 1 is the header line I would use

Sub CleanCancelledChks()
Dim r As Range
Dim r1 As Range
With ActiveSheet
Set r = .Range(.Range("D1"), .Range("D" & _
Rows.Count).End(xlUp))
.Columns("D:D").AutoFilter Field:=1, Criteria1:="=*-*"
' step down to row 2
set r = r.offset(1,0).Resize(r.rows.count - 1)
On Error Resume Next
'Extra variable is to prevent specialcells method from returning
error1004 when no negative values(ie. cancelled chks) exist.
Set r1 = Nothing
Set r1 = r.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilterMode = False
If Not r1 Is Nothing Then _
r1.EntireRow.Delete
End With
End Sub

--
Regards,
Tom Ogilvy

"ms" wrote in message
...
This procedure is used to delete any records where a negative sign exists
anywhere in a value in the D column. It works fine with multiple records,

but
if there is only one record in the file, with or without a negative sign

in the
file being formatted, the record is deleted.
If I change the SpecialCells Method to

r.SpecialCells(xlCellTypeAllValidation)
it doesn't delete the one record but also does not remove records where

the
negative sign exists when multiple records exist.

Here is a sample file with 2 records. One has a negative sign, the other

does
not and therefore should not be deleted.
A,1111111111,042804,030171,13174.20
A,1111111111,041904,030221-,4243.30

How can I change this to work in both situations?


Sub CleanCancelledChks()
Dim r As Range
Dim r1 As Range
With ActiveSheet
Set r = .Range(.Range("D2"), .Range("D" & _
Rows.Count).End(xlUp))
.Columns("D:D").AutoFilter Field:=1, Criteria1:="=*-*"
On Error Resume Next
'Extra variable is to prevent specialcells method from returning
error1004 when no negative values(ie. cancelled chks) exist.
Set r1 = Nothing
Set r1 = r.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilterMode = False
If Not r1 Is Nothing Then _
r1.EntireRow.Delete
End With
End Sub


Thanks,



  #3   Report Post  
Posted to microsoft.public.excel.programming
ms ms is offline
external usenet poster
 
Posts: 5
Default SpecialCells Method selects valid records

Thank you again Tom.
I did have to make a small change or two.
I had to move the "On Error Resume Next" above the r.Offset line to prevent the
1004 error. And the range had to start at "D2" rather than D1. All the valid
lines are rolled up into row 1 so nothing happens.
I tested it with one record and with several and it seems to work. I will test
it further.

Have a good weekend.
Mark


Tom Ogilvy wrote:

Using autofilter is clever, but it always treats the top row in the range as
the header row and it will always be visible.

Also, it will always delete it.

So is row2 a header line or a record. You claim it works with multiple
rows, but I suspect you are always deleteing row 2 regardless of whether it
contains a negative or not.

Assuming row 1 is the header line I would use

Sub CleanCancelledChks()
Dim r As Range
Dim r1 As Range
With ActiveSheet
Set r = .Range(.Range("D1"), .Range("D" & _
Rows.Count).End(xlUp))
.Columns("D:D").AutoFilter Field:=1, Criteria1:="=*-*"
' step down to row 2
set r = r.offset(1,0).Resize(r.rows.count - 1)
On Error Resume Next
'Extra variable is to prevent specialcells method from returning
error1004 when no negative values(ie. cancelled chks) exist.
Set r1 = Nothing
Set r1 = r.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilterMode = False
If Not r1 Is Nothing Then _
r1.EntireRow.Delete
End With
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default SpecialCells Method selects valid records

I guess I misread/misunderstood the problem you were having.

Sub CleanCancelledChks()
Dim r As Range
Dim r1 As Range
With ActiveSheet
Set r = .Range(.Range("D2"), .Range("D" & _
Rows.Count).End(xlUp))
if r(1).Row = 2 and r.rows.count = 1 then
if instr(r,"-") then _
r.EntireRow.Delete
elseif r(1).Row = 1 then
Exit sub
else
.Columns("D:D").AutoFilter Field:=1, Criteria1:="=*-*"
' step down to row 2
set r = r.offset(1,0).Resize(r.rows.count - 1)
On Error Resume Next
'Extra variable is to prevent specialcells method from returning
'error1004 when no negative values(ie. cancelled chks) exist.
Set r1 = Nothing
Set r1 = r.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilterMode = False
If Not r1 Is Nothing Then _
r1.EntireRow.Delete
End if
End With
End Sub


--
Regards,
Tom Ogilvy

"ms" wrote in message
...
Thank you again Tom.
I did have to make a small change or two.
I had to move the "On Error Resume Next" above the r.Offset line to

prevent the
1004 error. And the range had to start at "D2" rather than D1. All the

valid
lines are rolled up into row 1 so nothing happens.
I tested it with one record and with several and it seems to work. I will

test
it further.

Have a good weekend.
Mark


Tom Ogilvy wrote:

Using autofilter is clever, but it always treats the top row in the

range as
the header row and it will always be visible.

Also, it will always delete it.

So is row2 a header line or a record. You claim it works with multiple
rows, but I suspect you are always deleteing row 2 regardless of whether

it
contains a negative or not.

Assuming row 1 is the header line I would use

Sub CleanCancelledChks()
Dim r As Range
Dim r1 As Range
With ActiveSheet
Set r = .Range(.Range("D1"), .Range("D" & _
Rows.Count).End(xlUp))
.Columns("D:D").AutoFilter Field:=1, Criteria1:="=*-*"
' step down to row 2
set r = r.offset(1,0).Resize(r.rows.count - 1)
On Error Resume Next
'Extra variable is to prevent specialcells method from

returning
error1004 when no negative values(ie. cancelled chks) exist.
Set r1 = Nothing
Set r1 = r.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilterMode = False
If Not r1 Is Nothing Then _
r1.EntireRow.Delete
End With
End Sub



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
Please post this thread a correct full method, method about Nast Runsome New Users to Excel 8 February 25th 08 03:29 PM
Please post this thread a complete correct method, method about te Nast Runsome New Users to Excel 0 February 23rd 08 09:42 PM
Specialcells Bruno Uato Charts and Charting in Excel 0 October 7th 05 07:42 PM
specialcells method errors when criteria not found ms Excel Programming 8 April 22nd 04 09:53 PM
SpecialCells(xlCellTypeFormulas) Kevin Gabbert Excel Programming 1 January 28th 04 05:06 PM


All times are GMT +1. The time now is 12:13 PM.

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

About Us

"It's about Microsoft Excel"