VBA DIR फंक्शन - Excel VBA DIR फ़ंक्शन का उपयोग कैसे करें?

एक्सेल VBA DIR फ़ंक्शन

VBA DIR फ़ंक्शन को डायरेक्टरी फ़ंक्शन के रूप में भी जाना जाता है, यह VBA में एक इनबिल्ट फ़ंक्शन है जो हमें किसी दिए गए फ़ाइल या फ़ोल्डर का फ़ाइल नाम देने के लिए उपयोग किया जाता है, लेकिन हमें फ़ाइल के लिए पथ प्रदान करने की आवश्यकता है, इसके द्वारा लौटाए गए आउटपुट फ़ंक्शन स्ट्रिंग है क्योंकि यह फ़ाइल का नाम देता है, इस फ़ंक्शन के दो तर्क हैं जो पथ का नाम और गुण हैं।

DIR फ़ंक्शन निर्दिष्ट फ़ोल्डर पथ में बहुत पहले फ़ाइल नाम देता है। उदाहरण के लिए, आपके डी ड्राइव में, यदि आपके पास 2019 नामक एक फ़ोल्डर का नाम है और उस फ़ोल्डर में, यदि आप "2019 बिक्री" नामक फ़ाइल को एक्सेल करते हैं, तो आप डीआईआर फ़ंक्शन का उपयोग करके इस फ़ाइल तक पहुंच सकते हैं।

"VBA DIR" फ़ंक्शन अपने पथ फ़ोल्डर का उपयोग करके फ़ाइल का नाम प्राप्त करने में बहुत सहायक है।

वाक्य - विन्यास

इस फ़ंक्शन के दो वैकल्पिक तर्क हैं।

  • (पथ का नाम): जैसा कि नाम कहता है, फ़ाइल तक पहुंचने का मार्ग क्या है। यह फ़ाइल का नाम, फ़ोल्डर का नाम या निर्देशिका भी हो सकता है। यदि कोई रास्ता नहीं सौंपा गया है, तो मैं एक रिक्त स्ट्रिंग मान लौटाऊंगा, "
  • (गुण): यह भी एक वैकल्पिक तर्क है, और आप इसे कोडिंग में बहुत बार उपयोग नहीं कर सकते हैं। आप (पथ नाम) में फ़ाइल की विशेषता निर्दिष्ट कर सकते हैं , और DIR फ़ंक्शन केवल उन फ़ाइलों के लिए दिखता है।

उदाहरण के लिए: यदि आप केवल छिपी हुई फ़ाइलों का उपयोग करना चाहते हैं, यदि आप केवल पढ़ने के लिए केवल फ़ाइलों आदि का उपयोग करना चाहते हैं … हम इस तर्क में निर्दिष्ट कर सकते हैं। नीचे वे विशेषताएँ हैं जिनका हम उपयोग कर सकते हैं।

VBA DIR फ़ंक्शन का उपयोग करने के उदाहरण

उदाहरण # 1 - डीआईआर फ़ंक्शन का उपयोग करके फ़ाइल नाम तक पहुंचना

मैं आपको DIR फ़ंक्शन का उपयोग करके फ़ाइल नाम तक पहुंचने का सरल उदाहरण समझाऊंगा। नीचे दिए गए चरणों का पालन करें।

चरण 1: एक मैक्रो नाम बनाएँ।

चरण 2: चर को स्ट्रिंग के रूप में परिभाषित करें ।

कोड:

उप Dir_Example1 () मंद MyFile स्ट्रिंग अंत के रूप में

चरण 3: अब, इस चर के लिए, हम DIR फ़ंक्शन का उपयोग करके मान प्रदान करेंगे ।

कोड:

उप Dir_Example1 () स्ट्रिंग MyFile = Dir (समाप्ति उप) के रूप में मंद MyFile

चरण 4: अब अपने कंप्यूटर पर फ़ाइल फ़ोल्डर पथ को कॉपी और पेस्ट करें । डबल-कोट्स में पाथनाम का उल्लेख करें।

कोड:

उप Dir_Example1 () स्ट्रिंग MyFile = Dir के रूप में मंद MyFile ("E: VBA टेम्पलेट अंत उप

चरण 5: मैंने फ़ोल्डर में अपने पथ का उल्लेख किया है, अब हमें फ़ाइल नाम और उसके विस्तार का भी उल्लेख करना होगा। पहली चीज़ जो हमें करने की ज़रूरत है, उसे करने के लिए हमें पथ () के बाद बैकस्लैश डालना होगा

बैकस्लैश में प्रवेश करने के बाद, हमें पूर्ण फ़ाइल नाम दर्ज करना होगा ।

कोड:

सब Dir_Example1 () स्ट्रिंग MyFile के रूप में मंद MyFile = Dir ("E: VBA टेम्पलेट VBA Dir Excel Template.xlsm") समाप्ति उप

चरण 6: संदेश बॉक्स में चर का मान दिखाएँ।

कोड:

उप Dir_Example1 () स्ट्रिंग MyFile के रूप में डिम MyFile = Dir ("E: VBA टेम्पलेट VBA डर्क Excel Template.xlsm") MsgBox MyFile अंत उप

अब कोड को रन करें और देखें कि संदेश बॉक्स का परिणाम क्या है।

इसलिए डीआईआर फ़ंक्शन ने फ़ाइल नाम को फ़ाइल एक्सटेंशन के साथ वापस कर दिया।

उदाहरण # 2 - DIR फ़ंक्शन का उपयोग करके फ़ाइल खोलें

अब हम फाइल को कैसे खोलें? यह फ़ंक्शन फ़ाइल नाम वापस कर सकता है, लेकिन उस फ़ाइल को खोलना एक अलग प्रक्रिया है। फ़ाइल खोलने के लिए नीचे दिए गए चरणों का पालन करें।

चरण 1: स्ट्रिंग के रूप में दो चर बनाएं ।

कोड:

उप Dir_Example2 () डिम फोल्डरनाम स्ट्रिंग के रूप में डिम फाइलनाम के रूप में स्ट्रिंग एंड सब

चरण 2: अब, फ़ोल्डरनाम चर के लिए, फ़ोल्डर पथ असाइन करें।

कोड:

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 - Multiple Open 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 to 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:

उप Dir_Example4 () फ़ाइलनाम के रूप में मंद फ़ाइलनाम = डीआर ("E: VBA टेम्पलेट ", vbDirectory) फ़ाइलनाम करते समय "डीबग करें। फ़ाइल का नाम फ़ाइल नाम" - डीओ (अंत) लूप अंत उप

Ctrl + G दबाकर तत्काल विंडो दिखाई दे

अब कोड को रन करें। हमें तत्काल विंडो में सभी फ़ाइल नाम मिलेंगे।

दिलचस्प लेख...