I have a problem when I'm trying to implement some code for a large excelfile. The purpose of the VBA-code is to delete duplicate cells, row by row. If I start with a sheet looking something like this:
shoe shoe horse
cat horse dog dog
cat cat horse
horse
cat cat
shoe shoe
the I want the result to be:
shoe horse
cat horse dog
cat horse
horse
cat
shoe
My VBA code works but since my real sheet contains over 450 000 rows it takes about 30min to run the code. I hope that one of you could help me see wat I could change in the code to make it faster.
Here is the code:
Thanks!Code:Option Explicit 'Replace these three constants with maximum number of rows and columns and your Sheet name 'Paste this macro code in the Thisworkbook object in the VB Editor, then run it manually Private Const MAXROWS = 483184 Private Const MAXCOLS = 50 Private Const SHTNAME = "Sheet1" Private Sub Remove_Duplicates() Dim i As Long Dim j As Long Dim k As Long Dim x As Long Dim y As Long Dim colsmax As Long Dim delFlag As Boolean Dim strTable(MAXCOLS) As String Dim delTable(MAXCOLS) As String Dim mySheet As Worksheet Dim myRange As Range Dim strElement As String colsmax = MAXCOLS Set mySheet = ThisWorkbook.Sheets(SHTNAME) For i = 1 To MAXROWS x = 0 y = 0 For j = 1 To colsmax delFlag = False Set myRange = mySheet.Cells(i, j) If x = 0 Then strTable(x) = Trim(myRange.Value) x = x + 1 Else For k = 1 To x If Trim(myRange.Value) = strTable(k - 1) Then delFlag = True Exit For End If Next k If delFlag = True Then delTable(y) = CStr(j) y = y + 1 Else strTable(x) = Trim(myRange.Value) x = x + 1 End If End If Next j For k = y To 1 Step -1 j = CLng(delTable(k - 1)) Set myRange = mySheet.Cells(i, j) myRange.Delete (XlDeleteShiftDirection.xlShiftToLeft) Next k Next i End Sub




Reply With Quote