Excel VBA方法 [英] Excel VBA Dir Method

查看:135
本文介绍了Excel VBA方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下代码的形式成功多次打开文件夹中的多个文件,我了解dir如何帮助在while循环之前设置文件路径,但不太明白如何再次使用dir方法没有参数在while循环结束之前查找文件夹中的下一个文件:

I have used a form of the following code successfully many times to open multiple files in a folder and I understand how dir helps set the file path before the while loop, but don't quite understand how call the dir method again with no arguments right before the end of the while loop looks up the next file in the folder:

Sub OpenFiles()

Dim MyFolder As String
Dim MyFile As String

MyFolder = "C:\newFolder"
MyFile = Dir(MyFolder & "\*.xlsx")

Do While MyFile <> ""
Workbooks.Open Filename:=MyFolder & "\" & MyFile

MyFile = Dir

Loop
End Sub

在已经设置了一个值的字符串上调用Dir时,Dir是否设计为查找同一路径上的文件列表中的下一个文件?

When calling Dir on a string that already has been set with a value, is Dir designed to look to the next file in the list of files at that same path?

推荐答案

Dir 函数声明一个静态变量,并将它们用于以后的调用。

The Dir functions declares a Static variables and uses them for later invocations.

我猜这个逻辑是:


  1. 声明 index_of_file number_of_files 作为静态整数

  1. Declare index_of_file and number_of_files as static integers

如果提供参数,请扫描目录集 index_of_file 1 ,将 number_of_files 设置为文件夹中的文件数,并返回第一个文件。

If a parameter is supplied, scan the directory set index_of_file to 1, set number_of_files to the number of files in the folder, and return the first file.

如果未提供参数,请递增 index_of_file 并返回带有该索引的文件,或返回如果 index_of_file> number_of_files

If a parameter is not supplied, increment index_of_file and return the file with that index, or return "" if index_of_file>number_of_files

这篇关于Excel VBA方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆