Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / September 2007

Tip: Looking for answers? Try searching our database.

Transfer multiple sheets into 1 table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mario - 11 Sep 2007 10:47 GMT
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
John W. Vinson - 11 Sep 2007 20:13 GMT
>Hi there ,
>
[quoted text clipped - 12 lines]
>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]
Mario - 12 Sep 2007 09:47 GMT
John,

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

Mario

>>Hi there ,
>>
[quoted text clipped - 20 lines]
>
>             John W. Vinson [MVP]
gokop - 11 Sep 2007 22:42 GMT
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

> Hi there ,
>
[quoted text clipped - 15 lines]
> Thanks for your help.
> Mario
Mario - 12 Sep 2007 09:49 GMT
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 Mario,
>
[quoted text clipped - 166 lines]
>> Thanks for your help.
>> Mario
Mario - 13 Sep 2007 11:50 GMT
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,
>
[quoted text clipped - 166 lines]
>> Thanks for your help.
>> Mario
gokop - 13 Sep 2007 12:34 GMT
Hi Mario,

Try from Next j:

Next j
   

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

> Hi there ,
>
[quoted text clipped - 176 lines]
> >> Thanks for your help.
> >> Mario
Mario - 13 Sep 2007 13:53 GMT
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

> Hi Mario,
>
[quoted text clipped - 196 lines]
>> >> Thanks for your help.
>> >> Mario
PatK - 13 Sep 2007 23:38 GMT
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

> Hi Mario,
>
[quoted text clipped - 159 lines]
> > Thanks for your help.
> > Mario
PatK - 13 Sep 2007 23:58 GMT
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.

> 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
[quoted text clipped - 170 lines]
> > > Thanks for your help.
> > > Mario
gokop - 14 Sep 2007 00:00 GMT
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

> 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
[quoted text clipped - 170 lines]
> > > Thanks for your help.
> > > Mario
PatK - 14 Sep 2007 00:06 GMT
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!

> Hi Patk,
>
[quoted text clipped - 179 lines]
> > > > Thanks for your help.
> > > > Mario
John W. Vinson - 14 Sep 2007 03:21 GMT
>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
[quoted text clipped - 4 lines]
>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]
PatK - 14 Sep 2007 16:46 GMT
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.

> >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
[quoted text clipped - 9 lines]
>
>              John W. Vinson [MVP]
Douglas J. Steele - 14 Sep 2007 16:53 GMT
Microsoft ADO Ext. 2.x for DDL and Security.

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> Can you tell me which library I need to permit the following object to
> function:
[quoted text clipped - 21 lines]
>>
>>              John W. Vinson [MVP]
PatK - 14 Sep 2007 18:04 GMT
That did the trick..thanks Douglas!

> Microsoft ADO Ext. 2.x for DDL and Security.
>
[quoted text clipped - 23 lines]
> >>
> >>              John W. Vinson [MVP]
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.