VBA Excel Removing Duplicate Rows

So, I'm trying to delete any rows that have a duplicate in column C. This is a column of 700 records, however this value depends on different data, so I implemented the "LastRow" function. Here is my code:

Public Function LastRowInCRC() As Long

    Dim wsCRC As Worksheet
    Set wsCRC = Worksheets("CRC")

    With wsCRC
        LastRowInCRC = .Cells(.Rows.Count, "C").End(xlUp).Row
    End With

End Function

Sub DeleteDupRowsCRC()

    Dim wsCRC As Worksheet
    Set wsCRC = Worksheets("CRC")

    Dim lrowcrc As Long
    lrowcrc = CRC.LastRowInCRC

    'Debug.Print "C8:C" & lrowcrc

    With wsCRC

        .Range("C8:C" & lrowcrc).RemoveDuplicates Columns:=Array(3)

    End With

End Sub

I get an "application-specific or object-dependent" error in the following line during step-by-step debugging:

.Range("C8:C" & lrowcrc).RemoveDuplicates Columns:=Array(3)

Any ideas what is going wrong? I call "C8: C" and lrowcrc in the nearest window that is commented out and it gives me the correct range values, so I don’t think the problem is this, but I can’t find what is wrong ... any help is greatly appreciated .

+4
source share
2 answers

Array(3) Array(1), .


: : C , :

Option Explicit

Public Function LastRowInCRC() As Long

    Dim wsCRC As Worksheet
    Set wsCRC = Worksheets(1)

    With wsCRC
        LastRowInCRC = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

End Function

Sub DeleteDupRowsCRC()

    Dim wsCRC As Worksheet
    Set wsCRC = Worksheets(1)

    Dim lrowcrc As Long
    lrowcrc = LastRowInCRC

    'Debug.Print "C8:C" & lrowcrc

    With wsCRC

        .Range("C1:C" & lrowcrc).RemoveDuplicates Columns:=Array(1)

    End With

End Sub

Array(3) , .Range . C. , . Array(3), A1:C .

+3

, . ,

With wsCRC
    .Range(Cells(8, 3), Cells(lrowcrc, 3)).Select
    .Range(Cells(8, 3), Cells(lrowcrc, 3)).RemoveDuplicates Columns:=1, Header:=xlYes

End With

, ,

lrowcrc = LastRowInCRC

wsCRC. [8].SpecialCells(xlCellTypeLastCell).Row

+1

Source: https://habr.com/ru/post/1680944/


All Articles