Question : search file with exact full name in VBA

Dear Expert,

I have one file xls called "advancedata2010" in my folder, and I want to create
one new other file called "data2010" without the word of "advanced". The code is as
follows. But it won't create file of data2010 , and then  I solved  the problem
by rename the similar name file of "advanceddata2010" to "advacnedata", re-run
the program, and then the file of "data2010" is shown up and created

So my question is how to filesearch the eaxct name of file in VBA ?

Please advise
Duncan

Sub main()
Dim wkdir As String
Dim datafilename As String
wkdir = "d:\excel\"
datafilename = "data2010"
Call DoesWorkBookExist(wkdir, datafilename)
Sub DoesWorkBookExist(wkdir As String, datafilename As String)
'Test to see if a Workbook exists
Dim i As Integer
With Application.FileSearch
.LookIn = wkdir
'* represents wildcard characters
.Filename = datafilename
If .Execute > 0 Then 'Workbook exists
 '               Debug.Print "There is a Workbook."
           Else 'There is NOt a Workbook
  '             Debug.Print "The Workbook does not exist"
                'Workbooks.Add
                Workbooks.Add.SaveAs wkdir & datafilename
                ActiveWorkbook.Close False
            End If

    End With

End Sub

Answer : search file with exact full name in VBA

It's a placeholder.

Trying again...

exact match:

If Dir("d:\excel\somefile.xls") <> "" Then
   'code for file exists
Else
   'code for file does not exist
End If

partial match:

If Dir("d:\excel\*somefile*.xls*") <> "" Then
   'code for at least 1 match
Else
   'code for no matches
End If

Random Solutions  
 
programming4us programming4us