"Like" operator not working properly

haribole

New Member
Joined
Apr 10, 2018
Messages
19
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 !

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:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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