Excel VBA导入多个工作表

Excel VBA导入多个工作表,excel,ms-access,vba,Excel,Ms Access,Vba,我知道这个问题有很多不同的主题,但没有一个对我有用。。。我有.xls工作簿,其中有3个工作表(表1、表2和表3) 每张图纸上有65536行(目前图纸3有25多行)。我在下面的链接上找到了一个代码,它应该做这项工作。。。但是没有。它将只导入2.5万行。此外,只有Sheet1具有标题,第1行上的Sheet2和Sheet3具有数据 VBA I仅从第一个选项卡导入Excel文件。有没有办法修改它,以便它可以导入所有三个工作表,其中只有第一个工作表有标题 Private Sub cmdButto

我知道这个问题有很多不同的主题,但没有一个对我有用。。。我有.xls工作簿,其中有3个工作表(表1、表2和表3)

每张图纸上有65536行(目前图纸3有25多行)。我在下面的链接上找到了一个代码,它应该做这项工作。。。但是没有。它将只导入2.5万行。此外,只有Sheet1具有标题,第1行上的Sheet2和Sheet3具有数据

VBA I仅从第一个选项卡导入Excel文件。有没有办法修改它,以便它可以导入所有三个工作表,其中只有第一个工作表有标题

    Private Sub cmdButton_Click()

Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean

blnHasFieldNames = True
strPath = "C:\Folder\"
strTable = "dbo_tblTest"
strFile = Dir(strPath & "*.xlsx")

If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
If Dir(strPath & "*.*") = "" Then
MsgBox "The folder doesn't contain (visible) files"
Else

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Once purged LOOP file import
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
        strTable, strPathFile, blnHasFieldNames


     strFile = Dir()
Loop

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' LOOP TO MOVE FILES IN ARCHIVE FOLDER
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Dim fso As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim FileExt As String

    FromPath = "S:\Folder"  '~~> Change
    ToPath = "S:\Folder\Archive"    '~~> Change
    FileExt = "*"

    '~~> You can use *.* for all files or *.doc for word files

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If
    Set fso = CreateObject("scripting.filesystemobject")
    If fso.FolderExists(FromPath) = False Then
        MsgBox FromPath & " doesn't exist"
        Exit Sub
    End If
    If fso.FolderExists(ToPath) = False Then
        MsgBox ToPath & " doesn't exist"
        Exit Sub
    End If
    fso.CopyFile Source:=FromPath & FileExt, Destination:=ToPath

    Kill "S:\Folder\*"
    MsgBox "Files Successfully Imported"

End If

End Sub
Private Sub-cmdButton\u Click()
将strPath文件设置为字符串、strFile设置为字符串、strPath设置为字符串
作为字符串的Dim strTable
作为布尔值的Dim blnHasFieldNames
blnHasFieldNames=True
strPath=“C:\Folder\”
strTable=“dbo\u tblTest”
strFile=Dir(strPath&“*.xlsx”)
如果正确(strPath,1)“\”则
strPath=strPath&“\”
如果结束
如果Dir(strPath&“**”)=“那么
MsgBox“文件夹不包含(可见)文件”
其他的
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'一旦清除循环文件导入
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
当Len(strFile)>0时执行
strPathFile=strPath&strFile
DoCmd.transfer电子表格导入,acSpreadsheetTypeExcel9_
strTable,strPathFile,blnHasFieldNames
strFile=Dir()
环
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'循环以移动存档文件夹中的文件
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
作为对象的Dim fso
将FromPath设置为字符串
作为字符串的Dim-ToPath
Dim FileExt作为字符串
FromPath=“S:\Folder”~~>更改
ToPath=“S:\Folder\Archive”~~>更改
FileExt=“*”
'~~>您可以对所有文件使用*.doc,对word文件使用*.doc
如果正确(FromPath,1)“\”则
FromPath=FromPath&“\”
如果结束
设置fso=CreateObject(“scripting.filesystemobject”)
如果fso.FolderExists(FromPath)=False,则
MsgBox FromPath&“不存在”
出口接头
如果结束
如果fso.FolderExists(ToPath)=False,则
MsgBox ToPath&“不存在”
出口接头
如果结束
fso.CopyFile源:=FromPath&FileExt,目标:=ToPath
杀死“S:\Folder\*”
MsgBox“已成功导入文件”
如果结束
端接头

为了阅读工作簿中的所有工作表,您需要在“传输电子表格”命令中添加一个参数(“范围”参数),并使用工作表的名称对其进行完全限定:

'Put these with the rest of your variable declarations

 Dim objExcel As Object
 Dim wb As Object
 Dim ws As Object
 Dim strUsedRange As String

'Replace the current loop with the code starting from here

 Set objExcel = CreateObject("Excel.Application")

 Do While Len(strFile) > 0

     strPathFile = strPath & strFile
     Set wb = objExcel.Workbooks.Open(strPathFile)

     For Each ws In wb.Worksheets()
         'Loop over all the sheets in the workbook

          strUsedRange = ws.UsedRange.Address(0,0)
          DoCmdTransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames, ws.Name & "!" & strUsedRange
     Next ws

     wb.Close
     Set wb = Nothing

     strFile = Dir()

 Loop

 Set objExcel = Nothing

这样做的好处是,它将使用Excel的内置工作表集合自动处理工作表中的工作表名称和使用范围,循环只需对其进行迭代。

为了读取工作簿中的所有工作表,您需要在“传输电子表格”命令中再添加一个参数(“范围”参数),并使用工作表的名称对其进行完全限定:

'Put these with the rest of your variable declarations

 Dim objExcel As Object
 Dim wb As Object
 Dim ws As Object
 Dim strUsedRange As String

'Replace the current loop with the code starting from here

 Set objExcel = CreateObject("Excel.Application")

 Do While Len(strFile) > 0

     strPathFile = strPath & strFile
     Set wb = objExcel.Workbooks.Open(strPathFile)

     For Each ws In wb.Worksheets()
         'Loop over all the sheets in the workbook

          strUsedRange = ws.UsedRange.Address(0,0)
          DoCmdTransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strPathFile, blnHasFieldNames, ws.Name & "!" & strUsedRange
     Next ws

     wb.Close
     Set wb = Nothing

     strFile = Dir()

 Loop

 Set objExcel = Nothing

这样做的好处是,它将使用Excel的内置工作表集合自动处理工作表中的工作表名称和使用范围,循环只需对其进行迭代。

我怀疑这就是您想要的

Option Compare Database

Private Sub Command0_Click()

Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean
Dim lngCount As Long
Dim objExcel As Object, objWorkbook As Object
Dim colWorksheets As Collection
Dim strPathFile As String, strTable As String
Dim strPassword As String

' Establish an EXCEL application object
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
      Set objExcel = CreateObject("Excel.Application")
      blnEXCEL = True
End If
Err.Clear
On Error GoTo 0


' Replace C:\Filename.xls with the actual path and filename
strPathFile = "your_path_here\testit.xls"

' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"


blnReadOnly = True ' open EXCEL file in read-only mode

' Open the EXCEL file and read the worksheet names into a collection
Set colWorksheets = New Collection
Set objWorkbook = objExcel.Workbooks.Open(strPathFile, , blnReadOnly)
For lngCount = 1 To objWorkbook.Worksheets.Count
      colWorksheets.Add objWorkbook.Worksheets(lngCount).Name

    ' Import the data from each worksheet into the table
    If lngCount = 1 Then
          DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
          strTable, strPathFile, False, colWorksheets(lngCount) & "$"
    Else
          DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
          strTable, strPathFile, False, colWorksheets(lngCount) & "$"
    End If

Next lngCount

' Close the EXCEL file without saving the file, and clean up the EXCEL objects
objWorkbook.Close False
Set objWorkbook = Nothing
If blnEXCEL = True Then objExcel.Quit
Set objExcel = Nothing

' Delete the collection
Set colWorksheets = Nothing

End Sub
选项比较数据库
专用子命令0_单击()
Dim blnHasFieldNames为布尔型,blnEXCEL为布尔型,BlnRadonly为布尔型
暗计数等于长
对象为Dim objExcel,对象为OBJ工作簿
暗淡的Colworksheet作为集合
Dim strPathFile作为字符串,strTable作为字符串
将strPassword设置为字符串
'建立EXCEL应用程序对象
出错时继续下一步
Set objExcel=GetObject(,“Excel.Application”)
如果错误号为0,则
设置objExcel=CreateObject(“Excel.Application”)
blnEXCEL=True
如果结束
呃,明白了
错误转到0
'将C:\Filename.xls替换为实际路径和文件名
strPathFile=“此处为您的路径\u\testit.xls”
'将tablename替换为要插入的表的真实名称
“数据将被导入
strTable=“tablename”
blnReadOnly=True“以只读模式打开EXCEL文件
'打开EXCEL文件并将工作表名称读入集合
Set colWorksheets=新集合
设置objWorkbook=objExcel.Workbooks.Open(strPathFile,blnReadOnly)
对于lngCount=1到OBJWORKING.Worksheets.Count
colWorksheets.Add obj工作簿.Worksheets(lngCount).Name
'将每个工作表中的数据导入表中
如果lngCount=1,则
DoCmd.transfer电子表格导入,acSpreadsheetTypeExcel9_
strTable、strPathFile、False、colWorksheets(lngCount)和“$”
其他的
DoCmd.transfer电子表格导入,acSpreadsheetTypeExcel9_
strTable、strPathFile、False、colWorksheets(lngCount)和“$”
如果结束
下一个lngCount
'关闭EXCEL文件而不保存该文件,并清理EXCEL对象
objWorkbook.Close False
设置objWorkbook=Nothing
如果blnEXCEL=True,则objExcel.Quit
设置objExcel=Nothing
'删除集合
设置colWorksheets=无
端接头

我怀疑这就是你想要的

Option Compare Database

Private Sub Command0_Click()

Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean
Dim lngCount As Long
Dim objExcel As Object, objWorkbook As Object
Dim colWorksheets As Collection
Dim strPathFile As String, strTable As String
Dim strPassword As String

' Establish an EXCEL application object
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
      Set objExcel = CreateObject("Excel.Application")
      blnEXCEL = True
End If
Err.Clear
On Error GoTo 0


' Replace C:\Filename.xls with the actual path and filename
strPathFile = "your_path_here\testit.xls"

' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"


blnReadOnly = True ' open EXCEL file in read-only mode

' Open the EXCEL file and read the worksheet names into a collection
Set colWorksheets = New Collection
Set objWorkbook = objExcel.Workbooks.Open(strPathFile, , blnReadOnly)
For lngCount = 1 To objWorkbook.Worksheets.Count
      colWorksheets.Add objWorkbook.Worksheets(lngCount).Name

    ' Import the data from each worksheet into the table
    If lngCount = 1 Then
          DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
          strTable, strPathFile, False, colWorksheets(lngCount) & "$"
    Else
          DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
          strTable, strPathFile, False, colWorksheets(lngCount) & "$"
    End If

Next lngCount

' Close the EXCEL file without saving the file, and clean up the EXCEL objects
objWorkbook.Close False
Set objWorkbook = Nothing
If blnEXCEL = True Then objExcel.Quit
Set objExcel = Nothing

' Delete the collection
Set colWorksheets = Nothing

End Sub
选项比较数据库
专用子命令0_单击()
Dim blnHasFieldNames为布尔型,blnEXCEL为布尔型,BlnRadonly为布尔型
暗计数等于长
对象为Dim objExcel,对象为OBJ工作簿
暗淡的Colworksheet作为集合
Dim strPathFile作为字符串,strTable作为字符串
将strPassword设置为字符串
'建立EXCEL应用程序对象
出错时继续下一步
Set objExcel=GetObject(,“Excel.Application”)
如果错误号为0,则
设置objExcel=CreateObject(“Excel.Application”)
blnEXCEL=True
如果结束
呃,明白了
错误转到0
'将C:\Filename.xls替换为实际路径和文件名
strPathFile=“此处为您的路径\u\testit.xls”
'将tablename替换为要插入的表的真实名称
“数据将被导入
strTable=“tablename”
blnReadOnly=True“以只读模式打开EXCEL文件