VBA DIR 기능 | Excel VBA DIR 기능을 사용하는 방법?

Excel VBA DIR 함수

VBA DIR 함수는 디렉토리 함수라고도합니다. 이것은 주어진 파일 또는 폴더의 파일 이름을 제공하는 데 사용되는 VBA의 내장 함수이지만 파일 경로를 제공해야합니다. function은 파일의 이름을 반환하는 문자열이며,이 함수에는 경로 이름과 속성 인 두 개의 인수가 있습니다.

DIR 함수는 지정된 폴더 경로에서 맨 처음 파일 이름을 반환합니다. 예를 들어, D 드라이브에 2019라는 폴더 이름이 있고 해당 폴더에 "2019 Sales"라는 이름의 파일을 엑셀하면 DIR 기능을 사용하여이 파일에 액세스 할 수 있습니다.

"VBA DIR"기능은 경로 폴더를 사용하여 파일 이름을 가져 오는 데 매우 유용합니다.

통사론

이 함수에는 두 개의 선택적 인수가 있습니다.

  • [경로 이름] : 이름 그대로 파일에 액세스 할 수있는 경로를 말합니다. 파일 이름, 폴더 이름 또는 디렉터리 일 수도 있습니다. 경로가 지정되지 않은 경우 빈 문자열 값 즉 ""를 반환합니다.
  • [속성] : 이것은 또한 선택적 인수이며 코딩에 자주 사용하지 않을 수 있습니다. [Path Name] 에서 파일의 속성을 지정할 수 있으며 DIR 함수는 해당 파일 만 찾습니다.

예 : 숨김 파일에만 액세스하려면 읽기 전용 파일 등에 액세스하려면이 인수에 지정할 수 있습니다. 다음은 사용할 수있는 속성입니다.

VBA DIR 함수 사용 예

이 VBA Dir Excel 템플릿을 여기에서 다운로드 할 수 있습니다 – VBA Dir Excel 템플릿

예 # 1 – DIR 함수를 사용하여 파일 이름에 액세스

DIR 함수를 사용하여 파일 이름에 액세스하는 간단한 예를 설명합니다. 아래 단계를 따르십시오.

1 단계 : 매크로 이름을 만듭니다.

2 단계 : 변수를 String으로 정의합니다 .

암호:

 Sub Dir_Example1 () Dim MyFile As String End Sub 

3 단계 : 이제이 변수에 대해 DIR 함수 를 사용하여 값을 할당 합니다 .

암호:

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

4 단계 : 이제 컴퓨터에 파일 폴더 경로를 복사하여 붙여 넣습니다 . 큰 따옴표로 경로 이름을 언급하십시오.

암호:

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

5 단계 : 폴더 경로를 언급 했으므로 이제 파일 이름과 확장자도 언급해야합니다. 이렇게하려면 먼저 경로 (\) 뒤에 백 슬래시를 넣어야합니다.

백 슬래시를 입력 한 후 전체 파일 이름 을 입력해야합니다 .

암호:

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

6 단계 : 메시지 상자에 변수 값을 표시합니다.

암호:

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

이제 코드를 실행하고 메시지 상자의 결과를 확인합니다.

그래서 DIR 함수는 파일 확장자와 함께 파일 이름을 반환했습니다.

예제 # 2 – DIR 함수를 사용하여 파일 열기

이제 파일을 어떻게 열 수 있습니까? 이 함수는 파일 이름을 반환 할 수 있지만 해당 파일을 여는 과정은 약간 다릅니다. 파일을 열려면 다음 단계를 따르십시오.

1 단계 : 두 개의 변수를 String으로 만듭니다 .

암호:

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

2 단계 : 이제 FolderName 변수에 폴더 경로를 할당합니다.

암호:

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

3 단계 : 이제 FileName 변수에 대해 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.