Erlandsen Data Consulting
Erlandsen Data Consulting
Erlandsen Data Consulting
1 de 3
http://www.erlandsendata.no/english/index.php?d=envbadacexportado
The macro example assumes that your VBA project has added a reference to
the ADO object library.
You can do this from within the VBE by selecting the menu Tools, References
and selecting Microsoft ActiveX Data Objects x.x Object Library.
Use ADO if you can choose between ADO and DAO for data import or export.
Below is an extended example that shows how you can export data
from multiple workbooks:
Sub ExportMultipleFiles()
Dim fn As Variant, f As Integer
Dim cn As ADODB.Connection
' select one or more files
fn = Application.GetOpenFilename("Excel-files,*.xls", _
1, "Select One Or More Files To Open", , True)
If TypeName(fn) = "Boolean" Then Exit Sub
' connect to the Access database
Set cn = New ADODB.Connection
On Error GoTo DisplayErrorMessage
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\FolderName\DataBaseName.mdb;"
On Error GoTo 0
If cn.State <> adStateOpen Then
Exit Sub
End If
Application.ScreenUpdating = False
' repeat for each selected file
For f = LBound(fn) To UBound(fn)
Debug.Print "Selected file #" & f & ": " & fn(f)
Application.StatusBar = "Exporting data from " & fn(f) & "..."
ExportFromExcelToAccess cn, CStr(fn(f))
Application.StatusBar = False
Next f
Application.ScreenUpdating = True
' close the database connection
cn.Close
Set cn = Nothing
2 de 3
http://www.erlandsendata.no/english/index.php?d=envbadacexportado
cn.Close
Set cn = Nothing
MsgBox "The data export has finished!", vbInformation, ThisWorkbook.Name
Exit Sub
DisplayErrorMessage:
MsgBox Err.Description, vbExclamation, ThisWorkbook.Name
Resume Next
End Sub
Sub ExportFromExcelToAccess(cn As ADODB.Connection, strFullFileName As String)
' exports data from a workbook to a table in an Access database
' this procedure must be edited before use
Dim wb As Workbook, rs As ADODB.Recordset, r As Long, f As Integer
If cn Is Nothing Then Exit Sub
If cn.State <> adStateOpen Then Exit Sub
' open the source workbook
On Error GoTo DisplayErrorMessage
Set wb = Workbooks.Open(strFullFileName, True, True)
On Error GoTo 0
If wb Is Nothing Then Exit Sub ' failed to open the workbook
' activate the proper data source worksheet
wb.Worksheets(1).Activate
' create a new recordset
Set rs = New ADODB.Recordset
' open a recordset, all records in a table
On Error GoTo DisplayErrorMessage
rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' or open an empty recordset using a sql query that returns no records
'rs.Open "select * from TableName where SomeFieldName = -1", _
'
cn, adOpenKeyset, adLockOptimistic, adCmdText
On Error GoTo 0
If rs.State = adStateOpen Then ' successfully opened the recordset
r = 2 ' the first row containing data in the worksheet
Do While Len(Range("A" & r).Formula) > 0
' repeat until the first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
For f = 1 To .Fields.Count
.Fields(f - 1).Value = Cells(r, f).Value
Next f
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
End If
Set rs = Nothing
' close the source workbook without saving any changes
wb.Close False
Exit Sub
DisplayErrorMessage:
MsgBox Err.Description, vbExclamation, ThisWorkbook.Name
Resume Next
End Sub
3 de 3
http://www.erlandsendata.no/english/index.php?d=envbadacexportado
Wow, this is great.
This adds the data to an existing table, what if I want to replace the content of the table each
time I run the Macro (In other word, delette all the data in the table before I run this macro).
Thank you.
Ole P. from Norway wrote (2006-09-10 22:41:09 CET):
Big up guys
This a great site
Mahesh from Bangalore, India wrote (2006-06-02 15:41:18 CET):
Wonderfull code
This is really a wonderfull site
Shep from Lincoln, UK wrote (2006-05-23 11:41:40 CET):
Brilliant!
This worked perfectly. Thanks for your help.
Ole P. from Norway wrote (2006-02-16 21:09:32 CET):