|
Option Explicit
Sub CheckClosedFile()
Dim strPath As String, strFile As String
Dim strSheet As String, strResult As String
Dim a As String
Dim r As Integer
Dim c As Integer
Dim i As Integer
strPath = ThisWorkbook.Path & "\" & "传票练习"
strFile = Dir(strPath & "/" & "*.xls")
strSheet = "成绩"
i = 1
Do While Len(strFile) > 0
On Error Resume Next
For r = 2 To 4
For c = 1 To 12
a = Cells(r, c).Address
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
strResult = GetCellValue(strPath, strFile, strSheet, a)
ThisWorkbook.Sheets("sheet1").Cells(r + i, c) = strResult
Next c
Next r
i = i + 1
strFile = Dir
Loop
End Sub
Public Function GetCellValue(strPath, strFile, strSheet, strA1)
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
If Dir(strPath & strFile) = "" Then '判断文件是否存在
'文件不存在时产生运行时错误,此错误会传递给调用此函数的过程
Err.Raise 12345, "GetCellValue", "No found file"
Exit Function
End If
'调用XLM4.0宏表函数读取指定区域的内容
'如果指定工作表不存在,返回错误
GetCellValue = ExecuteExcel4Macro("'" & strPath & "[" & strFile & "]" _
& strSheet & "'!" & Range(strA1).Address(, , xlR1C1))
End Function
请各位大侠看看,为什么我只能读取一个工作薄的内容,而不能把相应文件夹下的所有文件都读取
|
|