作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 ParamArray 打开多个文件。
如何将工作簿分配给数组以单独访问它们?例如,获取每个工作簿的最后一行。
错误是
subscript out of range
Sub main()
Call COMBINED_FILES("wk1.csv","wk2.xlsx","wk3.xls","wk4.csv")
End Sub
Sub COMBINED_FILES(ParamArray SOURCE_FILE_NAME_WITH_EXTENTION() As Variant)
Dim MASTER_WORK_BOOK_PATH As String
MASTER_WORK_BOOK_PATH = ActiveWorkbook.Path & "\"
Dim MASTER_WORK_BOOK As Workbook
Set MASTER_WORK_BOOK = ActiveWorkbook
Dim I As Integer
For I = 1 To UBound(SOURCE_FILE_NAME_WITH_EXTENTION)
Dim Resultworkbook() As Workbook
Set Resultworkbook(I) = Workbooks.Open(MASTER_WORK_BOOK_PATH & SOURCE_FILE_NAME_WITH_EXTENTION(I))
Debug.Print SOURCE_FILE_NAME_WITH_EXTENTION(I)
Next I
End sub
最佳答案
COMBINED_FILES
中参数的括号不需要Dim MASTER_WORK_BOOK As Workbook
未使用 Integer
- Why Use Integer Instead of Long? For i = LBound(array) to Ubound(array)
ReDim ResultWorkbook(UBound(sourceFileNameWithExtension)) As Workbook
用于初始化和维度化 ResultWorkbook()
.如果未标注尺寸,则无法访问。 Sub Main()
CombinedFiles "wk1.xlsx", "wk2.xlsx", "wk3.xlsx", "wk4.xlsx"
End Sub
Sub CombinedFiles(ParamArray sourceFileNameWithExtension() As Variant)
Dim masterWorkbookPath As String
masterWorkbookPath = ThisWorkbook.Path & "\"
Dim i As Long
ReDim ResultWorkbook(UBound(sourceFileNameWithExtension)) As Workbook
For i = LBound(sourceFileNameWithExtension) To UBound(sourceFileNameWithExtension)
Set ResultWorkbook(i) = Workbooks.Open(masterWorkbookPath & sourceFileNameWithExtension(i))
Debug.Print sourceFileNameWithExtension(i)
Next i
End Sub
关于excel - 通过 ParamArray 打开多个工作簿时将工作簿分配给数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59360246/
我是一名优秀的程序员,十分优秀!