Transfer multiple sheets into 1 table

  • Thread starter Thread starter Mario
  • Start date Start date
M

Mario

Hi there ,

Can anyone help me in the right direction.
In a dirtectory i have multiple excelfiles with the same number of sheets ,
but the data is different.
I need to transfer data from (for instance sheet4) of multiple excelbooks
into 1 table.
The headings in sheet4 are always the same, so it should be easy to import.

I presume i need to do the following in the code :

- point out the directory
- loop through the files and extract the data from sheet4

Is there any code already available that does this kind of thing , or where
can i find more info on this issue. ?

Thanks for your help.
Mario
 
Hi there ,

Can anyone help me in the right direction.
In a dirtectory i have multiple excelfiles with the same number of sheets ,
but the data is different.
I need to transfer data from (for instance sheet4) of multiple excelbooks
into 1 table.
The headings in sheet4 are always the same, so it should be easy to import.

I presume i need to do the following in the code :

- point out the directory
- loop through the files and extract the data from sheet4

Is there any code already available that does this kind of thing , or where
can i find more info on this issue. ?

Take a look at the VBA help for "TransferSpreadsheet". It does exactly this.

John W. Vinson [MVP]
 
Hi Mario,

Use the transferspreadsheet method of DoCmd. See a code that opens
multipliple workbooks with multiple worksheets and load them into one table
and adds 4 extra columns, then fill the 4 extra columns with the names of the
workbooks and worksheets.

Private Sub Command2_Click()
'Dim strSheet() As String
'xlsSheetLoop (strSheet())
Dim xlApp As Excel.Application
Dim xlWS As Excel.Worksheet
Dim xlWB As Excel.Workbook
Dim i As Integer
Dim strFileName As String
Dim strOpenFile As String
Dim strNameOnly As String
Dim intTableExistTest As Integer


Dim wkShName As String
Dim objFSO As Object
Dim objFile As Object
Dim strFolderPath As String
Dim strPath As String
Dim strPathBrowser As String
Dim bookName As String
Dim intCellValue As Integer

Dim fieldValue As Field
Dim rangeValue As Range
Dim strFileNameValue As String
Dim workBookName As Names
Dim strFullPath As String
Dim j As Integer
Dim strActiveBook As Object
Dim strDefaultPath As String
Dim strTableNames As String
Dim dtDataTable As DataTable
Dim tbl As ADOX.Table

Dim strSQL As String

Set xlApp = New Excel.Application
On Error Resume Next
''Folder browser function
strDefaultPath = BrowseFolder("Select Folder") & "\"
strPath = strDefaultPath
strFileName = Dir(strPath & "*.xls")
strFullPath = strPath & strFileName

Do While Len(strFileName) > 0


strFullPath = strPath & strFileName
strFileNameValue = strFileName
strNameOnly = Left(strFileName, Len(strFileName) - 4)


Set xlWB = xlApp.Workbooks.Open(strFullPath, , , , "dulan")




For j = 1 To xlApp.Worksheets.Count

Set xlWS = xlApp.ActiveWorkbook.Worksheets(j)
xlWS.Unprotect ("dulan")
wkShName = xlWS.Name
strBudgetCat = Left(wkShName, Len(wkShName) - 6)
strSubCat = Left(wkShName, Len(wkShName) - 5)

DoCmd.TransferSpreadsheet acImport, , "NonVolSen_Table", strFullPath, -1,
wkShName & "!A13:Q33"



DoCmd.RunSQL "ALTER TABLE NonVolSen_Table ADD COLUMN CostCentreCode CHAR,
GLCode CHAR, BudCat CHAR, SubCat CHAR", -1
strSQL = "UPDATE NonVolSen_Table SET CostCentreCode = '" & strNameOnly & "',
GLCode = '" & wkShName & "', BudCat = '" & strBudgetCat & "', SubCat = '" &
strSubCat & "' WHERE CostCentreCode IS NULL"
CurrentDb.Execute strSQL, dbFailOnError
Next j




strFileName = Dir()

Set xlWB = Nothing

Loop



End Sub


The BrowseFolder is a Function. Put it in a separate Module and called it.
See the codes of the BrowseFolder:


Option Explicit
'************** Code Start **************
'This code was originally written by Terry Kreft.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code courtesy of
'Terry Kreft

Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long

Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) _
As Long

Private Const BIF_RETURNONLYFSDIRS = &H1
Public Function BrowseFolder(szDialogTitle As String) As String
Dim X As Long, bi As BROWSEINFO, dwIList As Long
Dim szPath As String, wPos As Integer

With bi
.hOwner = hWndAccessApp
.lpszTitle = szDialogTitle
.ulFlags = BIF_RETURNONLYFSDIRS
End With

dwIList = SHBrowseForFolder(bi)
szPath = Space$(512)
X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)

If X Then
wPos = InStr(szPath, Chr(0))
BrowseFolder = Left$(szPath, wPos - 1)
Else
BrowseFolder = vbNullString
End If
End Function
'*********** Code End *****************

Good luck
 
John,

Thanks for the info. I already knew about transferspreadsheet.
The code in the next post should do the trick i need.

Mario
 
Hello 'gokop' ( don't find your name )

WOW , what a nice peace of code this is.
I'm trying it right now , but i'll need to do some modifications.
I'll let you know through this newsgroup if everything works fine.
Thanks again ,great help.
Mario
 
Hi there ,

I encounter a small problem when running this code.
Every Excelbook is opened during the execution of the code.
This does not need to happen.
How can i correct this.



Krgds
Mario
 
Hi Mario,

Try from Next j:

Next j


xlApp.ActiveWorkbook.Save
xlApp.ActiveWorkbook.Close
strFileName = Dir()
Loop
End Sub
 
Hi ,

Works great , but i just left out the save command. :
xlApp.ActiveWorkbook.Save
The workbooks do not need saving.

Thanks again for your great help.
Mario
 
Sorry to jump into the middle of this, but this looks like an answer for a
problem I have. However, my success is less so. I get a runtime error right
off the bat when I get to:

Dim xlApp As Excel.Application

Do I have to, somehow, have some sort of library linked to access to make
that type of declaration? I presume I will have the problem on most dim
statements, thereafter. Anyway...sorry to interrupt and thanks for any
guidance....Pat
 
One additional Note: I have tried to go to Tools, References inside the VBA
editor, but "references" is greyed out...I have no idea why.
 
Hi Patk,

Go to the Tool menu when you are in the code mode of Access and choose
Reference. Check Microsoft Excel (You will see MS Excel somehow, check it) to
activate Excel Package. You may need to check other things like DAO also.

Good luck
Gokop
 
That helped once I was able to access the reference page (I had to delete the
function I had already started, using this code, to be able to add the
reference library). That got me past that error, but now am hung up at:

Dim tbl As ADOX.Table

so there is still another library, or something, I need to add. Am gonna
search for something like ....ado....something or other. I will let you know
if I figure it out.

Thanks!
 
Sorry to jump into the middle of this, but this looks like an answer for a
problem I have. However, my success is less so. I get a runtime error right
off the bat when I get to:

Dim xlApp As Excel.Application

Do I have to, somehow, have some sort of library linked to access to make
that type of declaration?

Yes. Open the VBA editor and select Tools... References from the menu. Scroll
down and find the Excel library and check it.

John W. Vinson [MVP]
 
Can you tell me which library I need to permit the following object to
function:

Dim tbl As ADOX.Table

This is still erroring out on me. I see a number of Activex items that I
thought might work, but so far, have not been able to find the right one.
 
Microsoft ADO Ext. 2.x for DDL and Security.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)
 
Back
Top