excel_root
New Member
- Joined
- Feb 25, 2019
- Messages
- 8
Hi everyone,
I would appreciate if anyone can help me with theis code:
I am trying to find all "pairs" of duplicate values within two colums. In order to do that, I am using flags (i.e. 1 if duplicate had been found before, 0 if not). However, when I am trying to run the macro, returns an error as "Object variable or With block variable not set". I believe have to re-write the code - so there is no reference to sVal as it can be "Nothing"
Any ideas? This is a part of more complex code (described below)
Basically, I am trying to find duplicates in two different columns with different criterias. Example of talbe:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Name1[/TD]
[TD]Colour1[/TD]
[TD]Price1[/TD]
[TD]Name2[/TD]
[TD]Colour2[/TD]
[TD]Price2[/TD]
[/TR]
[TR]
[TD]Kate[/TD]
[TD]Green[/TD]
[TD]80[/TD]
[TD]Antony[/TD]
[TD]Pink[/TD]
[TD]110[/TD]
[/TR]
[TR]
[TD]Andrew[/TD]
[TD]Green[/TD]
[TD]75[/TD]
[TD]Lisa[/TD]
[TD]Blue[/TD]
[TD]99[/TD]
[/TR]
[TR]
[TD]Mark[/TD]
[TD]Yellow[/TD]
[TD]105[/TD]
[TD]Kate[/TD]
[TD]Green[/TD]
[TD]80[/TD]
[/TR]
[TR]
[TD]Kate[/TD]
[TD]Green[/TD]
[TD]80[/TD]
[TD]Kate[/TD]
[TD]Green[/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]Mark[/TD]
[TD]Yellow[/TD]
[TD]90[/TD]
[TD]Mark[/TD]
[TD]Yellow[/TD]
[TD]90[/TD]
[/TR]
[TR]
[TD]Lisa[/TD]
[TD]Red[/TD]
[TD]99[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Programm is suppose to:
Loop for each Name1 value in Name2 range
If Name1.Value is equals to Name2.Value, then check the flags (i.e. if the Name2. value had been found previously)
If flag is equal to 1 or 2, then skip this Name2 cell (as this cell had been previously matched) and continue to loop for next possible duplicate in Name2 range
if flag value is 0 check second condition (Colour)
If Colour1 is equal to Colour2 for the corresponding Names then check third condition (Price)
Else skip this Name1 cell (because it does not match as per Colours criteria) and continue to loop for next Name1.cell value
If Price 1 is equal to Price2 of the corresponding Names then mark the matching rows of the fist and second columns (Name1:Price1) (Name2:Price2) with flag1
Else mark the matching rows of the fist and second columns (Name1:Price1) (Name2:Price2) with flag2
After the loop is completed for all Name1 cells, cut all rows with flag1 and paste to new Sheet2; cut all rows with flag2 and paste to new Sheet2
remove blank rows in the main (Acitve) Sheet
I hope I explained clearly.
Hope you can help me with this
I would appreciate if anyone can help me with theis code:
Code:
Sub pairs()
Dim sh As Worksheet, tb As Long, c As Range, sVal As Range
Set sh = Sheets(1) 'Edit sheet name
tb = sh.Cells(Rows.Count, 1).End(xlUp).Row
For Each c In sh.Range("B2:B50") 'Assumes header row
Set sVal = sh.Range("A2:A" & tb).Find(c.Value, LookIn:=xlValues, LookAt:=xlWhole)
If sVal Is Nothing Then
If sVal.Offset(0, 1).Value = 1 Then GoTo NextIteration
ElseIf sVal.Offset(0, 1).Value = "" Then
fVal.Offset(0, 1).Value = 1
c.Offset(0, 1).Value = 1
Else
End If
NextIteration:
Next
End Sub
I am trying to find all "pairs" of duplicate values within two colums. In order to do that, I am using flags (i.e. 1 if duplicate had been found before, 0 if not). However, when I am trying to run the macro, returns an error as "Object variable or With block variable not set". I believe have to re-write the code - so there is no reference to sVal as it can be "Nothing"
Any ideas? This is a part of more complex code (described below)
Basically, I am trying to find duplicates in two different columns with different criterias. Example of talbe:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Name1[/TD]
[TD]Colour1[/TD]
[TD]Price1[/TD]
[TD]Name2[/TD]
[TD]Colour2[/TD]
[TD]Price2[/TD]
[/TR]
[TR]
[TD]Kate[/TD]
[TD]Green[/TD]
[TD]80[/TD]
[TD]Antony[/TD]
[TD]Pink[/TD]
[TD]110[/TD]
[/TR]
[TR]
[TD]Andrew[/TD]
[TD]Green[/TD]
[TD]75[/TD]
[TD]Lisa[/TD]
[TD]Blue[/TD]
[TD]99[/TD]
[/TR]
[TR]
[TD]Mark[/TD]
[TD]Yellow[/TD]
[TD]105[/TD]
[TD]Kate[/TD]
[TD]Green[/TD]
[TD]80[/TD]
[/TR]
[TR]
[TD]Kate[/TD]
[TD]Green[/TD]
[TD]80[/TD]
[TD]Kate[/TD]
[TD]Green[/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]Mark[/TD]
[TD]Yellow[/TD]
[TD]90[/TD]
[TD]Mark[/TD]
[TD]Yellow[/TD]
[TD]90[/TD]
[/TR]
[TR]
[TD]Lisa[/TD]
[TD]Red[/TD]
[TD]99[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Programm is suppose to:
Loop for each Name1 value in Name2 range
If Name1.Value is equals to Name2.Value, then check the flags (i.e. if the Name2. value had been found previously)
If flag is equal to 1 or 2, then skip this Name2 cell (as this cell had been previously matched) and continue to loop for next possible duplicate in Name2 range
if flag value is 0 check second condition (Colour)
If Colour1 is equal to Colour2 for the corresponding Names then check third condition (Price)
Else skip this Name1 cell (because it does not match as per Colours criteria) and continue to loop for next Name1.cell value
If Price 1 is equal to Price2 of the corresponding Names then mark the matching rows of the fist and second columns (Name1:Price1) (Name2:Price2) with flag1
Else mark the matching rows of the fist and second columns (Name1:Price1) (Name2:Price2) with flag2
After the loop is completed for all Name1 cells, cut all rows with flag1 and paste to new Sheet2; cut all rows with flag2 and paste to new Sheet2
remove blank rows in the main (Acitve) Sheet
I hope I explained clearly.
Hope you can help me with this