I have a macro to search duplicate file name in excel file (LogFile.xlsx). If found show it on macro file (SearchDuplicate.xlsm) otherwise insert the file name in logfile.
Please !! help !
Please !! help !
Code:
Sub Button1_Click()
Dim MyFolder As String, MyFile As String
Dim LastRowLogFile As Long, m As Long
Dim FileNames() As String
Dim i As Integer, j As Integer, k As Integer
i = 1
j = 1
k = 1
MyFolder = ThisWorkbook.Path
MyFile = Dir(MyFolder & "\*.txt")
Application.ScreenUpdating = False
Workbooks.Open (MyFolder & "\Log\LogFile.xlsx")
Workbooks("LogFile.xlsx").Sheets(1).Activate
LastRowLogFile = ActiveSheet.UsedRange.Rows.Count
m = LastRowLogFile - 1
ReDim FileNames(1 To m) As String
For i = 1 To m Step 1
FileNames(i) = ActiveSheet.Cells(i + 1, 2)
Next i
Do While MyFile <> ""
For j = 1 To m Step 1
If MyFile Like "*" & FileNames(j) & "*" Or FileNames(j) Like "*" & MyFile & "*" Then 'Here the problem
Workbooks("SearchDuplicate.xlsm").Sheets(1).Activate
Sheets(1).Cells(k + 1, 1) = k
Sheets(1).Cells(k + 1, 2) = MyFile
Sheets(1).Cells(k + 1, 3) = j
k = k + 1
End If
Next j
Workbooks("LogFile.xlsx").Sheets(1).Activate
Sheets(1).Cells(LastRowLogFile + 1, 1) = LastRowLogFile 'First Row contains heading
Sheets(1).Cells(LastRowLogFile + 1, 2) = MyFile
Sheets(1).Cells(LastRowLogFile + 1, 3) = Now
Kill (MyFolder & "\" & MyFile)
LastRowLogFile = LastRowLogFile + 1
MyFile = Dir
Loop
Workbooks("LogFile.xlsx").Close SaveChanges:=True
Application.ScreenUpdating = True
MsgBox ("Search Complete !!!")
End Sub
Last edited: