Dear Klatuu,
Thanks very much. It works find.
I also added a query that adds two columns. It works find. I want the first
column to be filled with the Workbook name, that is the file name of the
excel and the second column to be filled with the worksheet name. I am using
INSERT INTO-- VALUES statement. It does not work. Please any idea? See the
code below:
Private Sub Command7_Click()
Dim xlApp As Excel.Application
Dim xlWS As Excel.Worksheet
Dim xlWB As Excel.Workbook
Dim i As Integer
Dim strFileName As String
Dim wkShName As String
Dim strFolderPath As String
Dim strPath As String
Dim strPathBrowser As String
Dim bookName As String
Dim strFileNameValue As String
Dim strFullPath As String
Dim j As Integer
Set xlApp = New Excel.Application
On Error Resume Next
strPath = "C:\Documents and Settings\a99858\My Documents\"
strFileName = Dir(strPath & "*.xls")
strFullPath = strPath & strFileName
Do While Len(strFileName) > 0
strFullPath = strPath & strFileName
strFileNameValue = strFileName
xlApp.Workbooks.Open (strFullPath)
For j = 1 To xlApp.Worksheets.count
Set xlWS = xlApp.ActiveWorkbook.Worksheets(j)
wkShName = xlWS.Name
DoCmd.TransferSpreadsheet acImport, , "MultiSheet_Example", strFullPath, -1,
wkShName & "!A1:F8"
DoCmd.RunSQL "ALTER TABLE MultiSheet_Example ADD COLUMN CCCode CHAR, GCode
CHAR", -1
DoCmd.RunSQL "INSERT INTO MultiSheet_Example (CCCode ,GCode) VALUES (&
strFileNameValue, & wkShName)"
Next j
strFileName = Dir()
Loop
End Sub
Please any idea on how to insert the filename without the .xls extension in
the first column of the access table and the worksheet name in the second
column.
Let me explain a little bit. I am working on an cost accounting document of
a company. The workbook is named after the cost center name eg Sales and the
Worksheet is named after the general ledger code (GLCode eg EE00875). After
trasfering all worksheets of all workbooks into one access table, the only
distinguishing value is the Workbookname and worksheet names. I will use this
to query general ledgers and create Profit and Lost Account for each cost
centre.
Sorry for the lengnthy explanation.
Any help is appreciated
Gokop