remove duplicate with exceptional

kelvin_9

Active Member
Joined
Mar 6, 2015
Messages
460
Office Version
  1. 2019
hello everyone,

i was wondering can i amend my code below into something that it will not remove duplicate if "UNASSIGNEDSHIFT UNASSIGNEDSHIFT" was found in column B?

here is my code:
Columns("B:B").Select
Selection.Copy
Columns("Q:Q").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$Q:$Q").RemoveDuplicates Columns:=1, Header:=xlNo

Book1
BCDEFG
1NamePerson No.JOBStart TimeEnd Time
2Hillary182348:4518:15
3Oscar1826211:3021:00
4Miki1837611:3021:00
5UNASSIGNEDSHIFT UNASSIGNEDSHIFTUNASSIGNEDSHIFT7108308113:1522:45
6UNASSIGNEDSHIFT UNASSIGNEDSHIFTUNASSIGNEDSHIFT7108326713:1522:45
Sheet1


thank you very much for your guidance
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Please try this:

VBA Code:
Sub CopyAndRemoveDups()
  Dim Cel As Range
  Dim Rng As Range
  Dim m As Long
  
  Range("Q:Q").Value = Range("B:B").Value
  
  Set Rng = Intersect(Range("Q:Q"), ActiveSheet.UsedRange)
  
  For Each Cel In Rng
    If Cel.Value <> "UNASSIGNEDSHIFT UNASSIGNEDSHIFT" Then
      m = Application.CountIf(Rng, Cel.Value)
      If m > 1 Then
        Cel.ClearContents
      End If
    End If
  Next Cel
  Rng.SpecialCells(xlCellTypeBlanks).Select
  Selection.Delete Shift:=xlUp
        
      
End Sub
 
Upvote 0
it will not remove duplicate if "UNASSIGNEDSHIFT UNASSIGNEDSHIFT" was found in column B?
I'm not entirely sure how to interpret that. If the code below does not do what you want could you please post a slightly larger set of sample data and the expected results and explain again in relation to that new sample?

Also, when posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details.

VBA Code:
Sub Test()
  Range("B1", Range("B" & Rows.Count).End(xlUp)).Copy Range("Q1")
  With Range("Q1", Range("Q" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(#=""UNASSIGNEDSHIFT UNASSIGNEDSHIFT"",#&""$$$""&ROW(#),IF(#="""","""",#))", "#", .Address))
    .RemoveDuplicates Columns:=1, Header:=xlNo
    .Replace What:="$$$*", Replacement:="", LookAt:=xlPart
  End With
End Sub
 
Upvote 0
thank you very much for your reply Jeffrey Mahoney and Peter_SSs

both solution can slove my question prefectly!!

thank you very much for your guidance
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
Hi

feel so sorry to interrupt again
when there are more than 1 start time and end time for "UNASSIGNEDSHIFT UNASSIGNEDSHIFT" from the source
the result will only return the first one of start time and end time, can this be fixed?
source:
Book1
BCDEFG
1NamePerson No.JOBStart TimeEnd Time
2Hillary182348:4518:15
3Oscar1826211:3021:00
4Miki1837611:3021:00
5UNASSIGNEDSHIFT UNASSIGNEDSHIFTUNASSIGNEDSHIFT714801859:1518:45
6UNASSIGNEDSHIFT UNASSIGNEDSHIFTUNASSIGNEDSHIFT714801879:1518:45
7UNASSIGNEDSHIFT UNASSIGNEDSHIFTUNASSIGNEDSHIFT7148019511:3021:00
8UNASSIGNEDSHIFT UNASSIGNEDSHIFTUNASSIGNEDSHIFT7148025111:3021:00
9UNASSIGNEDSHIFT UNASSIGNEDSHIFTUNASSIGNEDSHIFT7148025311:3021:00
Sheet1


after load:
Book1
ABCDEF
1NameJOBStart TimeEnd TimeActual HourName
5UNASSIGNEDSHIFT714801959:1521:0011UNASSIGNEDSHIFT UNASSIGNEDSHIFT
6UNASSIGNEDSHIFT714801959:1521:0011UNASSIGNEDSHIFT UNASSIGNEDSHIFT
7UNASSIGNEDSHIFT714801959:1521:0011UNASSIGNEDSHIFT UNASSIGNEDSHIFT
8UNASSIGNEDSHIFT714801959:1521:0011UNASSIGNEDSHIFT UNASSIGNEDSHIFT
9UNASSIGNEDSHIFT714801959:1521:0011UNASSIGNEDSHIFT UNASSIGNEDSHIFT
10#VALUE!
Sheet1


thank you very much for your guidance again
 
Upvote 0
I don't understand the requirement.

if start time and end time only have 1 section, example 09:15-18:45, it is fine when filter set for duplicate remove without remove "UNASSIGNEDSHIFT UNASSIGNEDSHIFT" criteria

however, if start time and end time have more than 1 section, example 09:15-18:45, 11:30-21:00.....etc, the result return only 09:15-21:00

sorry for my english presentation

thank you very much for your guidance
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top