View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
[email protected] paul.robinson@it-tallaght.ie is offline
external usenet poster
 
Posts: 789
Default Macro Issue with loop

Hi
I havn't checked this, but maybe assigning myName to a deleted row is
causung the problem. Try this slight variation of your code;

Sub deleteDuplicates()

Dim myName As Variant 'Name column
Set myName = Range("C2")


Do While Not IsEmpty(myName)


Set myAddress = myName.Offset(0, 1)
Set myCity = myName.Offset(0, 2)
Set myState = myName.Offset(0, 3)


Set nextName = myName.Offset(1, 0)
Set nextAddress = myAddress.Offset(1, 0)
Set nextCity = myCity.Offset(1, 0)
Set nextState = myState.Offset(1, 0)


If myName = nextName And _
myAddress = nextAddress And _
myCity = nextCity And _
myState = nextState Then
nextName.EntireRow.Delete
Else
Set myName = nextName
End If
Loop
End Sub

The IsEmpty property can be tricky too. I usually use

If Trim(myName.Value)<""

incase there is none printing information in the cell.

regards
Paul

Dileep Chandran wrote:

Hello everyone,

I got this macro, to delete duplicate data, from this newsgroup last
week.
I am facing an issue with the same as it start checking again from the
column1 once a duplicate is deleted. Can anyone help me to sort out
this issue?

The macro reads:

Sub deleteDuplicates()


Dim myName As Variant 'Name column
Set myName = Range("C2")


Do While Not IsEmpty(myName)


Set myAddress = myName.Offset(0, 1)
Set myCity = myName.Offset(0, 2)
Set myState = myName.Offset(0, 3)


Set nextName = myName.Offset(1, 0)
Set nextAddress = myAddress.Offset(1, 0)
Set nextCity = myCity.Offset(1, 0)
Set nextState = myState.Offset(1, 0)


If myName = nextName And _
myAddress = nextAddress And _
myCity = nextCity And _
myState = nextState Then
myName.EntireRow.Delete
End If


Set myName = nextName


Loop
End Sub


Thanks
-Dileep


End Sub