A
Add
Hi,
Please see the code below to read data from closed and protected workbooks
in a directory.
I am able to read data from most of the workbook, however from some workbook
it reads couple of cells correctly and for other cells it gives #NA (i.e.
error 2042). If I open the workbook in which it reads few cells correctly and
few cells gives error, then it gets all the cells data correctly of open
workbook.
This #NA error appears only when the workbook is closed and that too only
for couple of cells of that workbook and not for all other cells.
Can someone help me to rectify this error.
------------------------Code------------------------
Option Explicit
Sub ExtractData()
Dim FSO, Fld, Fil
Dim NewSht As Worksheet
Dim I As Integer, V As Integer
Dim Myrange As Range, C As Range
Dim MainFolderName As String
Dim fName As String, sName As String
Set FSO = CreateObject("Scripting.FileSystemObject")
MainFolderName = ThisWorkbook.path
Set Fld = FSO.GetFolder(MainFolderName)
Set NewSht = ThisWorkbook.Sheets.Add
I = 1
Cells(1, 1) = Now()
For Each Fil In Fld.Files
V = 0
'Skip this workbook
If Fil.Name <> ThisWorkbook.Name And Fil.Type = "Microsoft Office
Worksheet" Then
I = I + 1
fName = Fil.Name
' Change this sheet name
sName = "My Sheet"
' change these cell refs to grab the cells you want
Set Myrange = Range("C9,F9,I9,C11,F11,I11,C13,F13,I13")
Cells(I, 1) = fName
For Each C In Myrange
V = V + 1
Cells(I, 1 + V) = GetValue(MainFolderName, fName, sName,
C.Address)
Next
Else
End If
Next
Columns("A:A").AutoFit
Set FSO = Nothing
End Sub
Private Function GetValue(path, file, sheet, ref)
' Retrieves a value from a closed workbook
Dim arg As String
' Make sure the file exists
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetValue = "File Not Found"
Exit Function
End If
' Create the argument
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function
--------------------------------End of Code-----------------------
Thanks
Please see the code below to read data from closed and protected workbooks
in a directory.
I am able to read data from most of the workbook, however from some workbook
it reads couple of cells correctly and for other cells it gives #NA (i.e.
error 2042). If I open the workbook in which it reads few cells correctly and
few cells gives error, then it gets all the cells data correctly of open
workbook.
This #NA error appears only when the workbook is closed and that too only
for couple of cells of that workbook and not for all other cells.
Can someone help me to rectify this error.
------------------------Code------------------------
Option Explicit
Sub ExtractData()
Dim FSO, Fld, Fil
Dim NewSht As Worksheet
Dim I As Integer, V As Integer
Dim Myrange As Range, C As Range
Dim MainFolderName As String
Dim fName As String, sName As String
Set FSO = CreateObject("Scripting.FileSystemObject")
MainFolderName = ThisWorkbook.path
Set Fld = FSO.GetFolder(MainFolderName)
Set NewSht = ThisWorkbook.Sheets.Add
I = 1
Cells(1, 1) = Now()
For Each Fil In Fld.Files
V = 0
'Skip this workbook
If Fil.Name <> ThisWorkbook.Name And Fil.Type = "Microsoft Office
Worksheet" Then
I = I + 1
fName = Fil.Name
' Change this sheet name
sName = "My Sheet"
' change these cell refs to grab the cells you want
Set Myrange = Range("C9,F9,I9,C11,F11,I11,C13,F13,I13")
Cells(I, 1) = fName
For Each C In Myrange
V = V + 1
Cells(I, 1 + V) = GetValue(MainFolderName, fName, sName,
C.Address)
Next
Else
End If
Next
Columns("A:A").AutoFit
Set FSO = Nothing
End Sub
Private Function GetValue(path, file, sheet, ref)
' Retrieves a value from a closed workbook
Dim arg As String
' Make sure the file exists
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetValue = "File Not Found"
Exit Function
End If
' Create the argument
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function
--------------------------------End of Code-----------------------
Thanks