Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default Please help debug this code!

I'm trying to use the following macro (which I got from this forum) to delete
all rows in which cell values are duplicates in cols B, F, and J. Column B
contains a date, col F contains text, and column J contains text. When I run
the macro I get an error at the ".Rows(r).Delete shift:=xlUp" line. The
error is "Delete method of Range class failed." At the time of the error, r
= 4880 (a row with data in all three cols being compared). Can anyone help
debug? Here's the code:

Sub DeleteDupes()
Dim ws1 As Worksheet
Dim lastrow As Long
Dim r As Long

'Turn off warnings, etc.
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set ws1 = Worksheets("Combined Notes")
With ws1
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
For r = lastrow To 2 Step -1
If Application.And(.Cells(r, "B") = .Cells(r - 1, "B") _
, .Cells(r, "F") = .Cells(r - 1, "F"), .Cells(r, "J") = _
.Cells(r - 1, "J")) Then
.Rows(r).Delete shift:=xlUp
End If
Next r
End With

'Turn on warnings, etc.
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Please help debug this code!

Looks like you are mixing Excel language with VBA. Try this.

For r = lastrow To 2 Step -1
If .Cells(r, 2) = .Cells(r - 1, 2) And .Cells(r, 6) = .Cells(r - 1, 6) _
And .Cells(r, 10) = .Cells(r - 1, 10) Then
.Rows(r).Delete
End If
Next r


"Heliocracy" wrote:

I'm trying to use the following macro (which I got from this forum) to delete
all rows in which cell values are duplicates in cols B, F, and J. Column B
contains a date, col F contains text, and column J contains text. When I run
the macro I get an error at the ".Rows(r).Delete shift:=xlUp" line. The
error is "Delete method of Range class failed." At the time of the error, r
= 4880 (a row with data in all three cols being compared). Can anyone help
debug? Here's the code:

Sub DeleteDupes()
Dim ws1 As Worksheet
Dim lastrow As Long
Dim r As Long

'Turn off warnings, etc.
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set ws1 = Worksheets("Combined Notes")
With ws1
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
For r = lastrow To 2 Step -1
If Application.And(.Cells(r, "B") = .Cells(r - 1, "B") _
, .Cells(r, "F") = .Cells(r - 1, "F"), .Cells(r, "J") = _
.Cells(r - 1, "J")) Then
.Rows(r).Delete shift:=xlUp
End If
Next r
End With

'Turn on warnings, etc.
Application.DisplayAlerts = True
Application.ScreenUpdating = True

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
debug code mohavv Excel Discussion (Misc queries) 1 November 27th 07 09:56 PM
Debug Error in Code Karen McKenzie Excel Discussion (Misc queries) 2 May 18th 07 04:25 PM
Need help with code debug please justme Excel Programming 2 January 4th 07 06:09 AM
Excel 2000 Code works except in debug mode Bob Smedley Excel Programming 0 January 24th 06 01:21 AM
Code hangs Excel ... Why? ... and how can i debug?? Simon May Excel Programming 1 January 17th 05 06:22 AM


All times are GMT +1. The time now is 02:11 AM.

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"