Você está na página 1de 3

Importa de uma só vez, todas as folhas de excel que estiverem em c:\ para a tabela

Private Sub SeuBotao_Click()


Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
blnHasFieldNames = True
strPath = "C:" ' drive onde se situa o seu documento excel
strTable = "tblExemplo" 'nome da tabela no seu banco
strFile = Dir(strPath & "temp.xls") 'nome do seu excel, se mudar para "*.xls" importa todas as folhas excel
_
que estiverem em C:\ para a tabela do banco.
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, strPathFile, blnHasFieldNames
strFile = Dir()
Loop
End Sub

.........................................................................................

Este codigo importa o Excel para o Access, mas cria tabelas separadas com o nome dos Sheets e
com todos os seus dados.

Private Sub SeuBotao_Click()


Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
Dim strValue As String
Set appExcel = CreateObject("Excel.Application")
Set wb = appExcel.Workbooks.Open("C:\temp.xls") 'nome do seu excel e seu diretorio
For Each sh In wb.Sheets
Debug.Print sh.Name
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_" & sh.Name, "C:\temp.xls", True,
sh.Name & "!"
Next
wb.Close
appExcel.Quit
On Error GoTo 0
Exit Sub
End Sub

.......................................................................................
Este importa todos os dados de todos os sheets para a mesma tabela:

Private Sub SeuBotao_Click()


Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
Dim strValue As String
Dim strTable As String
strTable = "tblExemplo" 'nome da tabela no seu banco
Set appExcel = CreateObject("Excel.Application")
Set wb = appExcel.Workbooks.Open("C:\temp.xls") 'nome do seu excel e seu diretorio
For Each sh In wb.Sheets
Debug.Print sh.Name
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, "C:\temp.xls", True,
sh.Name & "!"
Next
wb.Close
appExcel.Quit
On Error GoTo 0
Exit Sub
End Sub

........................................................................................

Ou este, especificando o Range:

Private Sub SeuBotao_Click()


Dim aaa As Excel.Workbook
Dim bbb As Excel.Worksheet
Dim xl As Excel.Application
Dim strTabela As String
strTabela = "tblExemplo" 'nome da tabela no seu banco
Set xl = New Excel.Application
Set aaa = xl.Workbooks.Open("C:\temp.xls") 'nome do seu excel e seu diretorio

xl.Calculation = xlCalculationManual
ActiveWorkbook.PrecisionAsDisplayed = False
xl.ErrorCheckingOptions.BackgroundChecking = False
With aaa
For Each bbb In .Worksheets
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTabela, "C:\temp.xls", True,
bbb.Name & "!A1:AQ500"
Next
End With
xl.Calculation = xlCalculationAutomatic
ActiveWorkbook.PrecisionAsDisplayed = True
xl.ErrorCheckingOptions.BackgroundChecking = True
aaa.Close False
End Sub
Tenha um bom final de semana.

Você também pode gostar