【发布时间】:2016-04-06 23:54:35
【问题描述】:
我有一个 Access DB,我试图将单个文件夹中的多个工作表复制到一个主 excel 文件中
我首先为工作簿构建一个名为filedetails 的文件位置数组,然后打开每个文件位置并将内容粘贴到主文件中。因为我不希望文件相互粘贴。主工作簿上的起始位置始终是与上一个粘贴位置的偏移量 1。所有工作簿都在同一个工作目录中,因此设置代码以阻止将主工作簿复制到自身中
代码在xlSht2.Range(Selection, Selection.End(xlToRight)).Select 失败并显示错误消息
需要运行时错误“424”对象
Set xlApp = CreateObject("Excel.Application")
'## Open Working File
Set xlBook_A = xlApp.Workbooks.Open(strWF)
Set xlSht = xlBook_A.Worksheets(1)
' Open Each Sheet and Copy it into the Workbook (Except Worksheet into itself)
For intRecord = 1 To UBound(filedetails)
If (filedetails(1, intRecord)) <> strWF Then
Set xlBook_B = xlApp.Workbooks.Open(filedetails(1, intRecord))
Set xlSht2 = xlBook_B.Worksheets(1)
' After the rows have been pasted, a new starting point not "A2" will need to be set
' This offset will be done after each copy and paste giving an Append operation to MS Excel
' So Sheet A wont overwrite Sheet B
xlSht2.Range(Selection, Selection.End(xlToRight)).Select
xlSht2.Range(Selection, Selection.End(xlDown)).Select
xlSht2.Selection.Copy Destination:=xlSht.Range("A1").End(xlDown).Offset(1, 0)
End If
Next intRecord
谁能看到我哪里出错了?
【问题讨论】:
-
我不太确定 Selection 会指向什么
-
这基本上是脚本从 A2 中选择然后 shift+ctrl 和向左箭头然后向下箭头以突出显示所有需要复制的记录的地方。如果你想象 excel 表有 3 列,每列 150 行
-
我不确定
Selection是否会指向某个xlSht2单元格。也许它指向托管应用程序(MSAccess?)当前选择。最好使用全方位资格,例如xlSht2.Range(xlSht2.range("A1"), xlSht2.range("A1").End(xlToRight).End(xlDown)).Copy Destination:=xlSht.Range("A1").End(xlDown).Offset(1, 0)
标签: excel vba ms-access-2010