excel - 在添加数组之前检查文件扩展名是否为 .xlsx
问题描述
我为我创建了一个循环来获取特定文件夹中的文件,但我想知道如何确保将添加到 myArray 中的文件名仅包含 .xlsx 或 .xls?
Function listFiles(ByVal get_Path As String)
Dim myArray As Variant
Dim i As Integer
Dim counter As Integer
Dim xFile As Object
Dim FileServ As Object
Dim the_Folder As Object
Dim the_Files As Object
Set FileServ = CreateObject("Scripting.FileSystemObject")
Set the_Folder = FileServ.GetFolder(get_Path)
Set the_Files = the_Folder.Files
'Check if folder is empty
If the_Files.Count = 0 Then
Exit Function
End If
'Declare the size of Array based on the number of Files inside the folder
ReDim myArray(1 To the_Files.Count)
'Loop through each file and assign each filename in Array
i = 1
For Each xFile In the_Files
myArray(i) = xFile.Name
i = i + 1
Next
listFiles = myArray
End Function
解决方案
你可以这样做:
i = 0
For Each xFile In the_Files
If xFile.Name Like "*.xls" or xFile.Name Like "*.xlsx" Then
i = i + 1
myArray(i) = xFile.Name
End If
Next
Redim Preserve myArray(1 to i)
推荐阅读
- git-svn - git svn clone 不会克隆 SVN 存储库的隐藏文件夹/文件
- python - 计算从 x 到 x 个周期的连续出现次数
- prolog - 给定重复次数的另一个列表的重复列表
- json - 使用 KeyValuePair 创建 json 字符串
有数组元素 - javascript - Vue.js 基于数组索引的数据转换
- java - 是否可以在spring boot/java中实现每个用户在不同时间动态调度一个任务
- android - Dagger2 地图多重绑定 - [Dagger/MissingBinding]
- typescript - PKCS#7 DER 证书到 PEM(使用椭圆曲线)和 Typescript
- sharepoint - 有人建议 sharepoint office365 为 sharepoint 站点中的所有列表提供一个列表表单吗?
- chokidar - 使用节点添加监视文件夹