ฟังก์ชัน VBA DIR | วิธีใช้ฟังก์ชัน Excel VBA DIR

ฟังก์ชัน Excel VBA DIR

ฟังก์ชันVBA DIRเรียกอีกอย่างว่าฟังก์ชันไดเร็กทอรีซึ่งเป็นฟังก์ชัน inbuilt ใน VBA ซึ่งใช้เพื่อตั้งชื่อไฟล์ของไฟล์หรือโฟลเดอร์ที่กำหนดให้กับเรา แต่เราจำเป็นต้องระบุพา ธ สำหรับไฟล์ผลลัพธ์ที่ส่งกลับมา ฟังก์ชันเป็นสตริงเนื่องจากส่งคืนชื่อไฟล์มีอาร์กิวเมนต์สองรายการสำหรับฟังก์ชันนี้ซึ่ง ได้แก่ ชื่อพา ธ และแอตทริบิวต์

ฟังก์ชัน DIR ส่งคืนชื่อไฟล์แรกในเส้นทางโฟลเดอร์ที่ระบุ ตัวอย่างเช่นใน D Drive ของคุณหากคุณมีชื่อโฟลเดอร์ชื่อ 2019 และในโฟลเดอร์นั้นถ้าคุณเก่งไฟล์ชื่อ“ ยอดขาย 2019” คุณจะสามารถเข้าถึงไฟล์นี้ได้โดยใช้ฟังก์ชัน DIR

ฟังก์ชัน“ VBA DIR” มีประโยชน์อย่างมากในการตั้งชื่อไฟล์โดยใช้โฟลเดอร์พา ธ

ไวยากรณ์

ฟังก์ชันนี้มีอาร์กิวเมนต์ที่เป็นทางเลือกสองแบบ

  • [ชื่อพา ธ ]:ตามชื่อบอกว่าพา ธ ในการเข้าถึงไฟล์คืออะไร ซึ่งอาจเป็นชื่อของไฟล์ชื่อโฟลเดอร์หรือไดเร็กทอรีก็ได้เช่นกัน หากไม่ได้กำหนดเส้นทางใด ๆ ฉันจะส่งคืนค่าสตริงว่างเช่น“”
  • [แอตทริบิวต์]:นี่เป็นอาร์กิวเมนต์ที่เป็นทางเลือกและคุณอาจไม่ได้ใช้สิ่งนี้บ่อยนักในการเขียนโค้ด คุณสามารถระบุแอตทริบิวต์ของไฟล์ใน[Path Name]และฟังก์ชัน DIR จะค้นหาเฉพาะไฟล์เหล่านั้น

ตัวอย่างเช่นหากคุณต้องการเข้าถึงเฉพาะไฟล์ที่ซ่อนอยู่หากคุณต้องการเข้าถึงเฉพาะไฟล์แบบอ่านอย่างเดียวเป็นต้น ... เราสามารถระบุในอาร์กิวเมนต์นี้ได้ ด้านล่างนี้คือคุณสมบัติที่เราสามารถใช้ได้

ตัวอย่างการใช้ฟังก์ชัน VBA DIR

คุณสามารถดาวน์โหลด VBA Dir Excel Template ได้ที่นี่ - VBA Dir Excel Template

ตัวอย่าง # 1 - การเข้าถึงชื่อไฟล์โดยใช้ฟังก์ชัน DIR

ฉันจะอธิบายตัวอย่างง่ายๆของการเข้าถึงชื่อไฟล์โดยใช้ฟังก์ชัน DIR ทำตามขั้นตอนด้านล่าง

ขั้นตอนที่ 1:สร้างชื่อมาโคร

ขั้นตอนที่ 2:กำหนดตัวแปรสตริง

รหัส:

 Sub Dir_Example1 () Dim MyFile เป็น String End Sub 

ขั้นตอนที่ 3:ตอนนี้สำหรับตัวแปรนี้เราจะกำหนดค่าโดยใช้ฟังก์ชั่น DIR

รหัส:

 Sub Dir_Example1 () Dim MyFile เป็น String MyFile = Dir (End Sub 

ขั้นตอนที่ 4:ตอนนี้คัดลอกและวางเส้นทางโฟลเดอร์ไฟล์บนคอมพิวเตอร์ของคุณ พูดถึงชื่อพา ธ ในเครื่องหมายคำพูดคู่

รหัส:

 Sub Dir_Example1 () Dim MyFile เป็น String MyFile = Dir ("E: \ VBA Template End Sub 

ขั้นตอนที่ 5:ฉันได้พูดถึงเส้นทางของฉันไปยังโฟลเดอร์แล้วตอนนี้เราต้องพูดถึงชื่อไฟล์และนามสกุลด้วย ในการทำสิ่งแรกที่เราต้องทำคือเราต้องใส่แบ็กสแลชหลังเส้นทาง (\)

หลังจากป้อนเครื่องหมายเราต้องป้อนชื่อไฟล์เต็ม

รหัส:

 Sub Dir_Example1 () Dim MyFile เป็น String MyFile = Dir ("E: \ VBA Template \ VBA Dir Excel Template.xlsm") End Sub 

ขั้นตอนที่ 6:แสดงค่าของตัวแปรในกล่องข้อความ

รหัส:

 Sub Dir_Example1 () Dim MyFile เป็น String MyFile = Dir ("E: \ VBA Template \ VBA Dir Excel Template.xlsm") MsgBox MyFile End Sub 

ตอนนี้เรียกใช้รหัสและดูว่าผลลัพธ์ของกล่องข้อความคืออะไร

ดังนั้นฟังก์ชัน DIR จึงส่งคืนชื่อไฟล์ที่มีนามสกุลไฟล์

ตัวอย่าง # 2 - เปิดไฟล์โดยใช้ฟังก์ชัน DIR

ตอนนี้เราจะเปิดไฟล์ได้อย่างไร? ฟังก์ชันนี้สามารถส่งคืนชื่อไฟล์ได้ แต่การเปิดไฟล์นั้นเป็นกระบวนการที่แตกต่างกันเล็กน้อย ทำตามขั้นตอนด้านล่างเพื่อเปิดไฟล์

ขั้นตอนที่ 1:สร้างสองตัวแปรเป็นสตริง

รหัส:

 Sub Dir_Example2 () Dim FolderName เป็น String Dim FileName เป็น String End Sub 

ขั้นตอนที่ 2:ตอนนี้สำหรับตัวแปรFolderNameกำหนดเส้นทางโฟลเดอร์

รหัส:

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

ขั้นตอนที่ 3:ตอนนี้สำหรับตัวแปรชื่อไฟล์ที่เราต้องการที่จะได้รับชื่อไฟล์โดยใช้ฟังก์ชั่น DIR

รหัส:

 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.