你好,谢谢你的帮助。我正在整理一个看似简单的Access数据管理解决方案,以便在我们的办公室使用,而且我遇到了麻烦,因为我在vba方面的背景最多也是最小的。
我这里有两个相关但断开连接的Access 2007应用程序,我需要一个系统让用户能够轻松地导入和导出这些信息。我现在有一个脚本可以将应用程序中的所有表导出到一个excel文件中,每个表作为一个不同的工作表,问题是当我导入它时,它似乎只找到了要导入的第一个表。
我希望找到一个方法来迭代每个工作表,获取工作表名称,然后根据工作表名称将数据合并到表中。
澄清:
谢谢您能提供的任何帮助,非常感谢。
编辑
工作脚本(非常感谢grahamj42 42的帮助):
Private Sub Command101_Click()
'Dim excelapp As New Excel.Application
Dim excelApp As Object
Set excelApp = CreateObject("Excel.Application")
'Dim excelbook As New Excel.Workbook
Dim excelbook As Object
Set excelbook = excelApp.Workbooks.Add
'Dim excelsheet As New Excel.Worksheet
'Dim excelsheet As Object
'Set excelsheet = excelbook.Sheets
Dim intNoOfSheets As Integer, intCounter As Integer
Dim strFilePath As String, strLastDataColumn As String
Dim strLastDataRow As String, strLastDataCell As String
strFilePath = "C:\Users\UserName\Documents\Export\DatabaseExport03-28-2013.xlsx"
Set excelbook = excelApp.Workbooks.Open(strFilePath)
intNoOfSheets = excelbook.worksheets.Count
Dim CurrSheetName As String
For intCounter = 1 To intNoOfSheets
excelbook.worksheets(intCounter).Activate
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, ActiveSheet.Name, _
strFilePath, True, _
excelbook.Worksheets(intCounter).Name & "!" & _
Replace(excelbook.Worksheets(intCounter).UsedRange.Address, "$", "")
Next
excelbook.Close
excelApp.Quit
Set excelApp = Nothing
End Sub请注意,在DoCmd.TransferSpreadsheet命令中,这里有一个设置为“True”的HasFieldNames属性,因为我的电子表格导出时将字段名作为列标题。
发布于 2013-03-28 19:28:41
在没有选择任何内容的情况下使用Selection时,将引用工作表中选定的单元格。虽然我不明白,在您的情况下,为什么这应该在表之外,您可以这样做,而不用选择任何使用Worksheet.UsedRange
For intCounter = 1 To intNoOfSheets
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, Activesheet.Name, _
strFilePath, True, _
excelbook.Worksheets(intCounter).Name & "!" & _
Replace(excelbook.Worksheets(intcounter).UsedRange.Address, "$", "")
Nexthttps://stackoverflow.com/questions/15685974
复制相似问题