Funzione VBA DIR - Come utilizzare la funzione DIR VBA di Excel?

Funzione Excel VBA DIR

La funzione VBA DIR è anche nota come funzione directory, questa è una funzione incorporata in VBA che viene utilizzata per darci il nome file di un determinato file o di una cartella ma dobbiamo fornire il percorso per il file, l'output restituito da questo la funzione è una stringa poiché restituisce il nome del file, ci sono due argomenti per questa funzione che sono il nome del percorso e gli attributi.

La funzione DIR restituisce il primo nome di file nel percorso della cartella specificato. Ad esempio, nel tuo D Drive, se hai un nome di cartella chiamato 2019 e in quella cartella, se hai un file Excel denominato "Vendite 2019", puoi accedere a questo file utilizzando la funzione DIR.

La funzione "VBA DIR" è molto utile per ottenere il nome del file utilizzando la cartella del percorso.

Sintassi

Questa funzione ha due argomenti opzionali.

  • (Path Name): come dice il nome, qual è il percorso per accedere al file. Questo potrebbe essere il nome del file, il nome della cartella o anche la directory. Se non viene assegnato alcun percorso, restituirò un valore di stringa vuoto, ad esempio "
  • (Attributi): anche questo è un argomento facoltativo e potresti non usarlo molto spesso nella codifica. È possibile specificare l'attributo del file in (Path Name) e la funzione DIR cerca solo quei file.

Ad esempio: se vuoi accedere solo ai file nascosti, se vuoi accedere solo ai file di sola lettura, ecc … possiamo specificare in questo argomento. Di seguito sono riportati gli attributi che possiamo utilizzare.

Esempi di utilizzo della funzione DIR VBA

Esempio # 1 - Accesso al nome del file utilizzando la funzione DIR

Ti spiegherò il semplice esempio di accesso al nome del file utilizzando la funzione DIR. Segui i passaggi seguenti.

Passaggio 1: crea un nome per la macro.

Passaggio 2: definire la variabile come stringa .

Codice:

Sub Dir_Example1 () Dim MyFile As String End Sub

Passaggio 3: ora, per questa variabile, assegneremo un valore utilizzando la funzione DIR .

Codice:

Sub Dir_Example1 () Dim MyFile As String MyFile = Dir (End Sub

Passaggio 4: ora copia e incolla il percorso della cartella del file sul tuo computer. Indica il nome del percorso tra virgolette.

Codice:

Sub Dir_Example1 () Dim MyFile As String MyFile = Dir ("E: VBA Template End Sub

Passaggio 5: ho menzionato il mio percorso alla cartella, ora dobbiamo menzionare anche il nome del file e la sua estensione. Per fare questa prima cosa che dobbiamo fare è mettere una barra rovesciata dopo il percorso ()

Dopo aver inserito la barra rovesciata, dobbiamo inserire il nome completo del file .

Codice:

Sub Dir_Example1 () Dim MyFile As String MyFile = Dir ("E: VBA Template VBA Dir Excel Template.xlsm") End Sub

Passaggio 6: mostra il valore della variabile nella finestra del messaggio.

Codice:

Sub Dir_Example1 () Dim MyFile As String MyFile = Dir ("E: VBA Template VBA Dir Excel Template.xlsm") MsgBox MyFile End Sub

Ora esegui il codice e guarda qual è il risultato della finestra di messaggio.

Quindi la funzione DIR ha restituito il nome del file con l'estensione del file.

Esempio n. 2: aprire il file utilizzando la funzione DIR

Ora come apriamo il file? Questa funzione può restituire il nome del file, ma l'apertura di quel file è un processo leggermente diverso. Segui i passaggi seguenti per aprire il file.

Passaggio 1: crea due variabili come String .

Codice:

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

Passaggio 2: ora, per la variabile FolderName , assegna il percorso della cartella.

Codice:

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:

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

Rendi visibile la finestra immediata premendo Ctrl + G.

Ora esegui il codice. Otterremo tutti i nomi dei file nella finestra immediata.

Articoli interessanti...