If Not function proceeds when value *is* the one specified

后端 未结 1 1169
情深已故
情深已故 2020-11-30 15:37

I am trying to write a script which checks for duplicate values in another worksheet, but I cannot get it to work. At line problem the If function always

相关标签:
1条回答
  • 2020-11-30 16:39

    Use On Error Resume Next judiciously.

    Don't use On Error Resume Next for the entire code - it will hide all your errors. Use it only when it's really needed.

    Using On Error Resume Next means telling the code to Shut UP and do what you want. In most cases it will do what you want... Shut Up and perform... but then you will not get the expected results or totally wrong results as shown below !!! (SiddharthRout ©:)

    enter image description here

    Change

    Err.Clear
    On Error Resume Next
    Set wsDel = Sheets("Matching Contacts")
    wsDel.Delete
    

    to

    On Error Resume Next
    Set wsDel = Sheets("Matching Contacts")
    On Error GoTo 0
    If Not wsDel Is Nothing Then wsDel.Delete
    

    line On Error GoTo 0 will return your error handler to default mode.


    Some issues with your code:

    1) In line If Not LocatedCell = "Nothing" Then you tries to identify whether your cells value doesn't equal string "Nothing" which is uncorrect.

    To check whether the .Find function returns any cell, change

    If Not LocatedCell = "Nothing" Then 
    

    to

    If Not LocatedCell Is Nothing Then
    

    2) change With Sheets(mailerSheet).Range("A:A") to With mailerSheet.Range("A:A")

    3) as @SiddharthRout mentioned in comments below,

    The copy has to be right before the paste special. Excel is very well known for clearing the clipboard if you perform some specific actions

    if you are going to change interior color and copy row, change

    'With_
       LocatedCell.EntireRow.Copy_
       '.Interior.ColorIndex = 4 'green
    'End With
    

    to

    With LocatedCell.EntireRow
         .Interior.ColorIndex = 4 'green
         .Copy
    End With
    

    4) and of course: How to avoid using Select/Active statements

    0 讨论(0)
提交回复
热议问题