Hi all
I need a macro to do the following
1.Get list of .xls files from specified folder.
2.Append the files in new workbook in same folder.
Ex:
D:/users/excel/Sample_sheet1.xls
D:/users/excel/Sample_sheet2.xls
D:/users/excel/Sample_sheet3.xls
I want to get the list of Sample_*.xls and
create Sample.xls master file which adds the above three .xls as sheets in it.
Actually my macro as below
Sub GetExcelFileData()
Dim strFilePath As String, strFilename As String, strFullPath As String
Dim lngCounter As Long
Dim oConn As Object, oRS As Object, oFSObj As Object
Dim fileItem As Object
Application.ScreenUpdating = False
'This gives us a full path name e.g. C:tempfolderfile.txt
'We need to split this into path and file name
Set oFSObj = CreateObject("SCRIPTING.FILESYSTEMOBJECT")
Set srcFolder = oFSObj.GetFolder(ThisWorkbook.Path)
'Open an ADO connection to the folder specified
Set oConn = CreateObject("ADODB.CONNECTION")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.Path & ";" & _
"Extended Properties='Excel 8.0;HDR=Yes';"
Set oRS = CreateObject("ADODB.RECORDSET")
lngCounter = 1
'Now actually open the excel file and import into Excel
For Each fileItem In srcFolder.Files
strFile = fileItem.Name
If Right(fileItem.Name, 4) = ".xls" And InStr(fileItem.Name, Left(strFile, InStr(strFile, ".") - 1)) = 1 Then
oRS.Open "SELECT * FROM " & strFile, oConn, 3, 1, 1
While Not oRS.EOF
If lngCounter > 1 Then
Sheets.Add After:=Worksheets(Worksheets.Count)
Else
lngCounter = 2
End If
ActiveSheet.Range("A1").CopyFromRecordset (oRS)
Wend
oRS.Close
Columns("A:IV").AutoFit
End If
Next
oConn.Close
ActiveWorkbook.Saved = True
End Sub
The main .xls has created and while im trying to open its throwing the below warning.
Run-time error '-2147467259 (80004005)':
The Microsoft Jet Database engine cannot open the file
\\testsystem\testdomain\exceloutput\. It is already oppened
exlusively by another user , or you need permission to view its data.
Could you please anyone help me on this....
Bookmarks