|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Using VBA to select/copy xls data into Access
Hello all,
I've been trying numerous methods of getting an Excel worksheet into Access 2003 via VBA to no avail. Transferspreadsheet doesn't work (Excel data problems) but manually copying the same Excel data then Paste Appending into the Access table works fine. So, what I'd like to do in VBA is to select and copy all the data in the first worksheet then Paste Append into a named table. I'm ok with establishing a connection to Excel and selecting the necessary xls but am stumped as to how you select and copy all data in the first worksheet. Also, no doubt it's possible, but how do you get the name of the worksheet via VBA? Thanks in advance. Chris |
|
#2
|
|||
|
|||
|
Quote:
What about going row-by-row? Open the Excel sheet, copy the cell contents into a variable and insert it into the Access table? Is all of the data formatted nicely for merging into Access? I have Access VBA modules that takes data from Excel (one is nicely formatted, the other is a printable spreadsheet) and pull it into Access. I think I do it row-by-row. |
|
#3
|
||||
|
||||
|
Here is code I use to import excel spreadsheet into a table in Access. Once you have it in a table, then the easy thing is to append it to another table that you desire to accumulate data to.
Code:
Private Sub Command16_Click()
'---------------------------------------------------------------------------------------
' Procedure : ImportXLSheetsAsTables
' Author : jed
' Date : 1/16/2009
' Purpose : To import all worksheets in a specified workbook into
' individual tables in Access.
' Tables get names: Tbl_ + name of the worksheet
'
' NOTE: Must have a reference to the Microsoft Excel Object Library
'---------------------------------------------------------------------------------------
'
Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
Dim strValue As String
On Error GoTo ImportXLSheetsAsTables_Error
Set appExcel = CreateObject("Excel.Application")
Set wb = appExcel.Workbooks.Open("C:\Documents and Settings\" & Environ("UserName") & "\Desktop\GDOCS.xls")
For Each sh In wb.Sheets
Debug.Print sh.Name
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "GDOCS", "C:\Documents and Settings\" & Environ("UserName") & "\Desktop\GDOCS.xls", True, sh.Name & "!"
Next
wb.Close
appExcel.Quit
MsgBox "SpreadSheets Imported. Continue"
On Error GoTo 0
Exit Sub
ImportXLSheetsAsTables_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ImportXLSheetsAsTables of Module Module9"
End Sub
__________________
Alan Sidman __________ If I helped you, I'd like to know. Click on the icon next to the thread number and tell me. Last edited by AlanSidman : September 23rd, 2009 at 04:16 PM. |
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft Access Development > Using VBA to select/copy xls data into Access |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|