פונקציית VBA DIR | כיצד להשתמש בפונקציה VBA DIR של Excel?

פונקציית VBA DIR של Excel

פונקציית VBA DIR מכונה גם פונקציית המדריך, זו פונקציה מובנית ב- VBA המשמשת לתת לנו את שם הקובץ של קובץ נתון או תיקיה, אך עלינו לספק את הנתיב לקובץ, את הפלט המוחזר על ידי זה הפונקציה היא מחרוזת מכיוון שהיא מחזירה את שם הקובץ, ישנם שני ארגומנטים לפונקציה זו שהם שם הנתיב והתכונות.

פונקציית DIR מחזירה את שם הקובץ הראשון בנתיב התיקיה שצוין. לדוגמה, בכונן D שלך אם יש לך שם תיקיה בשם 2019 ובתיקייה זו, אם אתה מצטיין לקובץ בשם "מכירות 2019", תוכל לגשת לקובץ זה באמצעות פונקציית DIR.

הפונקציה "VBA DIR" עוזרת מאוד להשיג את שם הקובץ באמצעות תיקיית הנתיב שלו.

תחביר

לפונקציה זו שני ארגומנטים אופציונליים.

  • [שם נתיב]: כמו שהשם אומר מה הנתיב לגישה לקובץ. זה יכול להיות שם הקובץ, שם התיקיה או הספריה. אם לא מוקצה נתיב כלשהו אני אחזיר ערך מחרוזת ריק כלומר ""
  • [תכונות]: זהו גם טיעון אופציונלי וייתכן שלא תשתמשו בו לעתים קרובות מאוד בקידוד. באפשרותך לציין את תכונת הקובץ ב- [שם נתיב] והפונקציה DIR מחפשת רק את הקבצים האלה.

לדוגמא: אם ברצונך לגשת לקבצים מוסתרים בלבד, אם ברצונך לגשת רק לקבצים לקריאה בלבד וכו '... אנו יכולים לציין בטיעון זה. להלן התכונות בהן אנו יכולים להשתמש.

דוגמאות לשימוש בפונקציה VIR DIR

אתה יכול להוריד תבנית VBA Dir Excel זו כאן - תבנית VBA Dir Excel

דוגמה מס '1 - גישה לשם הקובץ באמצעות פונקציית DIR

אסביר לך את הדוגמה הפשוטה לגישה לשם הקובץ באמצעות פונקציית DIR. בצע את השלבים הבאים.

שלב 1: צור שם מאקרו.

שלב 2: הגדר את המשתנה כמחרוזת .

קוד:

 Sub Dir_Example1 () עמעום MyFile כמו Sub String End 

שלב 3: עכשיו עבור משתנה זה, נקצה ערך באמצעות פונקציית DIR .

קוד:

 תת Dir_Example1 () עמע את MyFile כמחרוזת MyFile = Dir (Sub Sub 

שלב 4: כעת העתק והדבק את נתיב תיקיית הקבצים במחשב שלך. ציין את שם הנתיב במרכאות כפולות.

קוד:

 תת Dir_Example1 () עמע את MyFile כמחרוזת MyFile = Dir ("E: \ VBA תבנית סוף משנה 

שלב 5: הזכרתי את דרכי לתיקיה, כעת עלינו להזכיר גם את שם הקובץ ואת סיומתו. כדי לעשות את הדבר הראשון שעלינו לעשות הוא עלינו לשים קו נטוי אחורי אחרי הנתיב (\)

לאחר הזנת הנטייה האחורית עלינו להזין את שם הקובץ המלא .

קוד:

 תת Dir_Example1 () עמע את MyFile כמחרוזת MyFile = Dir ("E: \ VBA Template \ VBA Dir Excel Template.xlsm") Sub Sub 

שלב 6: הראה את ערך המשתנה בתיבת ההודעה.

קוד:

 תת Dir_Example1 () עמע את MyFile כמחרוזת MyFile = Dir ("E: \ VBA Template \ VBA Dir Excel Template.xlsm") MsgBox MyFile End Sub 

כעת הפעל את הקוד וראה מה התוצאה של תיבת ההודעות.

אז פונקציית DIR החזירה את שם הקובץ עם סיומת הקובץ.

דוגמה מס '2 - פתח קובץ באמצעות פונקציית DIR

Now how do we open the file? This function can return file name but opening that file is a bit different process. Follow the below steps to open the file.

Step 1: Create two variables as String.

Code:

 Sub Dir_Example2() Dim FolderName As String Dim FileName As String End Sub 

Step 2: Now for FolderName variable assign the folder path.

Code:

 Sub Dir_Example2() Dim FolderName As String Dim FileName As String FolderName = "E:\VBA Template\" End Sub 

Step 3: Now for the FileName variable, we need to get the file name by using the DIR function.

Code:

 Sub Dir_Example2() Dim FolderName As String Dim FileName As String FolderName = "E:\VBA Template\" FileName = Dir( End Sub 

Step 4: Now for Path Name we have already assigned a path to the variable FolderPath, so we can directly supply the variable here.

Code:

 Sub Dir_Example2() Dim FolderName As String Dim FileName As String FolderName = "E:\VBA Template\" FileName = Dir(FolderName End Sub 

Step 5: Now we need to supply the file name. By using the ampersand symbol (&) assign the file name.

Code:

 Sub Dir_Example2() Dim FolderName As String Dim FileName As String FolderName = "E:\VBA Template\" FileName = Dir(FolderName & "VBA Dir Excel Template.xlsm") End Sub 

Step 6: Now use the WORKBOOKS.OPEN method.

Code:

 Sub Dir_Example2() Dim FolderName As String Dim FileName As String FolderName = "E:\VBA Template\" FileName = Dir(FolderName & "VBA Dir Excel Template.xlsm") Workbooks.Open End Sub 

Step 7: File Name is a combination of FolderPath & FileName. So combine these two.

Code:

 Sub Dir_Example2() Dim FolderName As String Dim FileName As String FolderName = "E:\VBA Template\" FileName = Dir(FolderName & "VBA Dir Excel Template.xlsm") Workbooks.Open FolderName & FileName End Sub 

Now run this code it will open the mentioned file name.

Example #3 – Open Multiple Workbooks using DIR Function

Actually, we can access all the workbooks in the folder. In order to access each and every file we cannot mention all the file names directly, but we can use the wildcard character to refer the file.

The asterisk (*) is one of those wildcard characters. It identifies any number of characters. For example, if you want to access all the macro files in the folder you can use the asterisk as the wildcard i.e. “*.xlsm*”

Here * will match any file name with the extension of the file is equal to “xlsm”.

Code:

 Sub Dir_Example3() Dim FolderName As String Dim FileName As String FolderName = "E:\VBA Template\" FileName = Dir(FolderName & "*.xlsm*") Do While FileName  "" Workbooks.Open FolderName & FileName FileName = Dir() Loop End Sub 

Now the above code will open all the files in the folder path.

FileName = Dir() the reason why I have used this line because, in order to access the next file in the folder, we have to make the existing file name to nil. The moment we make the existing file name to nil when the loop runs for the second time it will take the next file in the folder.

Example #4 – Get all the File Names in the Folder

Suppose if you want the list of all the file names in the folder we can also do this by using attributes.

Code:

 Sub Dir_Example4() Dim FileName As String FileName = Dir("E:\VBA Template\", vbDirectory) Do While FileName  "" Debug.Print FileName FileName = Dir() Loop End Sub 

Make the immediate window visible by pressing Ctrl + G.

Now run the code we will get all the file names in the immediate window.