Funkcija VBA DIR | Kako uporabiti funkcijo Excel VBA DIR?

Funkcija Excel VBA DIR

Funkcija VBA DIR je znana tudi kot imeniška funkcija, to je vgrajena funkcija v VBA, ki se uporablja za določanje imena datoteke dane datoteke ali mape, vendar moramo zagotoviti pot do datoteke, izhod, ki jo vrne ta Funkcija je niz, saj vrne ime datoteke, tej funkciji pripadata dva argumenta, ki sta ime poti in atributi.

Funkcija DIR vrne prvo ime datoteke v navedeni poti mape. Na primer, na vašem disku D, če imate ime mape z imenom 2019 in v tej mapi, če imate datoteko Excel z imenom »Prodaja leta 2019«, lahko do te datoteke dostopate s funkcijo DIR.

Funkcija »VBA DIR« je zelo koristna pri pridobivanju imena datoteke z uporabo mape poti.

Sintaksa

Ta funkcija ima dva neobvezna argumenta.

  • [Ime poti]: Kot ime pove, kakšna je pot do datoteke. To je lahko tudi ime datoteke, ime mape ali imenika. Če katera pot ni dodeljena, vrnem prazno vrednost niza, tj.
  • [Atributi]: To je tudi neobvezen argument in tega morda ne boste uporabljali zelo pogosto pri kodiranju. Atribut datoteke lahko določite v [Ime poti] in funkcija DIR išče samo te datoteke.

Na primer: Če želite dostopati samo do skritih datotek, če želite dostopati samo do datotek samo za branje itd ..., lahko v tem argumentu določimo. Spodaj so atributi, ki jih lahko uporabimo.

Primeri uporabe funkcije VBA DIR

To predlogo VBA Dir Excel lahko prenesete tukaj - Predloga VBA Dir Excel

Primer # 1 - Dostop do imena datoteke s funkcijo DIR

Pojasnil vam bom preprost primer dostopa do imena datoteke s funkcijo DIR. Sledite spodnjim korakom.

1. korak: Ustvarite ime makra.

2. korak: Spremenljivko definirajte kot niz .

Koda:

 Sub Dir_Example1 () Zatemni MyFile kot niz na koncu Sub 

3. korak: Zdaj bomo tej spremenljivki dodelili vrednost s funkcijo DIR .

Koda:

 Sub Dir_Example1 () Dim MyFile kot niz MyFile = Dir (End Sub 

4. korak: Zdaj kopirajte in prilepite pot do mape datotek v računalnik. Ime poti navedite v dvojnih narekovajih.

Koda:

 Sub Dir_Example1 () Dim MyFile kot niz MyFile = Dir ("E: \ VBA Predloga Konec Sub 

5. korak: Omenil sem svojo pot do mape, zdaj pa moramo omeniti tudi ime datoteke in njeno pripono. Za to moramo najprej narediti poševnico po poti (\)

Po vnosu poševnice nazaj moramo vnesti celotno ime datoteke .

Koda:

 Sub Dir_Example1 () Zatemni MyFile kot niz MyFile = Dir ("E: \ VBA Predloga \ VBA Dir Excel Template.xlsm") Konec Sub 

6. korak: V oknu za sporočilo prikažite vrednost spremenljivke.

Koda:

 Sub Dir_Example1 () Zatemni MyFile kot niz MyFile = Dir ("E: \ VBA Predloga \ VBA Dir Excel Template.xlsm") MsgBox MyFile End Sub 

Zdaj zaženite kodo in poglejte, kakšen je rezultat okna za sporočilo.

Tako je funkcija DIR vrnila ime datoteke s pripono datoteke.

2. primer - Odprite datoteko s funkcijo DIR

Kako naj zdaj odpremo datoteko? Ta funkcija lahko vrne ime datoteke, vendar je odpiranje te datoteke nekoliko drugačen postopek. Sledite spodnjim korakom, da odprete datoteko.

1. korak: Ustvari dve spremenljivki kot niz .

Koda:

 Sub Dir_Example2 () Dim Ime mape kot niz Dim ime datoteke kot konec niza Sub 

2. korak: Zdaj spremenljivki FolderName dodelite pot do mape.

Koda:

 Sub Dir_Example2 () Dim Ime mape kot niz Dim ime datoteke kot niz FolderName = "E: \ VBA Predloga \" Konec Sub 

3. korak: Zdaj za spremenljivko FileName moramo dobiti ime datoteke s pomočjo funkcije DIR .

Koda:

 Sub Dir_Example2 () Dim FolderName kot niz Dim Dim File Name kot niz 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.