Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the file exists using VBA

Tags:

excel

vba

Sub test()  thesentence = InputBox("Type the filename with full extension", "Raw Data File")  Range("A1").Value = thesentence  If Dir("thesentence") <> "" Then     MsgBox "File exists." Else     MsgBox "File doesn't exist." End If  End Sub 

In this when i pickup the text value from the input box, it doesn't work. If however, if remove "the sentence" from If Dir() and replace it with an actual name in the code, it works. Can somebody help?

like image 555
Dinesh Goel Avatar asked Jul 20 '12 06:07

Dinesh Goel


People also ask

How do you check if a file exists in Excel?

You can run the macro by select Macro > Macros under the Tools menu. Then select the macro called CheckIfFileExists and click on the Run button. After you run the macro, the values in column B should indicate whether the files exist or not. You can press Alt + F11 to view the VBA code.

How do you check if a folder exists in Excel VBA?

We will use the DIR function to check if a file exists. If the file doesn't exist, we will display a “File does not exist” message to the user. If the file exists, we will open the file. We will use the DIR function to check if a folder exists.

Does workbook exist VBA?

To check if a workbook exists in a specific folder, you can use the DIR function. DIR is a function that stands for the directory. You need to specify the path of the file along with the name and extension. If a file exists, it returns the file name, otherwise a blank value.

What is DIR function in VBA?

DIR is a very special function in VBA, its job is to return a string representing the name of a file, directory, or archive that matches a specified pattern. DIR function only returns the first file name or folder name from a location that matches the specified attributes.


2 Answers

Note your code contains Dir("thesentence") which should be Dir(thesentence).

Change your code to this

Sub test()  thesentence = InputBox("Type the filename with full extension", "Raw Data File")  Range("A1").Value = thesentence  If Dir(thesentence) <> "" Then     MsgBox "File exists." Else     MsgBox "File doesn't exist." End If  End Sub 
like image 190
Cylian Avatar answered Sep 22 '22 18:09

Cylian


Use the Office FileDialog object to have the user pick a file from the filesystem. Add a reference in your VB project or in the VBA editor to Microsoft Office Library and look in the help. This is much better than having people enter full paths.

Here is an example using msoFileDialogFilePicker to allow the user to choose multiple files. You could also use msoFileDialogOpen.

'Note: this is Excel VBA code Public Sub LogReader()     Dim Pos As Long     Dim Dialog As Office.FileDialog     Set Dialog = Application.FileDialog(msoFileDialogFilePicker)      With Dialog         .AllowMultiSelect = True         .ButtonName = "C&onvert"         .Filters.Clear         .Filters.Add "Log Files", "*.log", 1         .Title = "Convert Logs to Excel Files"         .InitialFileName = "C:\InitialPath\"         .InitialView = msoFileDialogViewList          If .Show Then             For Pos = 1 To .SelectedItems.Count                 LogRead .SelectedItems.Item(Pos) ' process each file             Next         End If     End With End Sub 

There are lots of options, so you'll need to see the full help files to understand all that is possible. You could start with Office 2007 FileDialog object (of course, you'll need to find the correct help for the version you're using).

like image 35
ErikE Avatar answered Sep 22 '22 18:09

ErikE