我一直在运行此UDF和随附的宏,以从关闭的工作簿中检索某些单元格值。
它总是工作得很好,因为单元格内容被拉的范围最多总是一两个字。
现在,我正在尝试提取一个包含更多文本(〜800个字符)的单元格,它正在显示#VALUE!
错误作为输出。
我将错误转换为在MsgBox中显示为字符串,结果:“错误2015”。
有人知道解决方法吗?基本上,我在“ D10”范围内的文本太长,因此没有使用下面的代码将其提取。
这是我正在使用的代码:
Option Explicit
Public MyObj As New FileSystemObject
Function GetValueFromClosedWorkbook(path, file, sheet, ref)
Dim arg As String, xFolder As String
If Right(path, 1) <> "\" Then path = path & "\" ' check whether the file exists
If Dir(path & file) = "" Then
GetValueFromClosedWorkbook = "File not found."
Exit Function
End If
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Address(, , xlR1C1) 'create the argument
GetValueFromClosedWorkbook = ExecuteExcel4Macro(arg)
End Function
Sub GetTextsFromClosedWorkbooks()
Dim xPath As Object, xFolder As String
Dim xSheet As String, xName As String, xExplanation As Variant, xTestNo As String, xFile As String, xCompleted As String, z As Long, y As Long, xColumnNo As Long
Dim file As Variant
Sheets("Test").Range("A8:M100").ClearContents
xFolder = "\\generic path" 'path of files
xSheet = "Test1"
xName = "B7"
xExplanation = "D10"
z = 7 'number of rows used as headers
Set xPath = MyObj.GetFolder(xFolder)
For Each file In xPath.Files
z = z + 1
xFile = file.Name
With Sheets("Test")
MsgBox CStr(GetValueFromClosedWorkbook(xFolder, xFile, xSheet, xExplanation)) ''this is what displays Error 2015
.Cells(z, 10) = GetValueFromClosedWorkbook(xFolder, xFile, xSheet, xExplanation)
End With
Next file
z = 7 'number of rows used as headers
End Sub