我正在开发一个小项目,我需要从另一个工作簿中的工作表的任何外部数据库中将工作表添加到当前打开的工作簿中。我创建了一个表单来导入当前打开(活动)工作簿中所需的任何工作表。
将从远程(在任何其他文件夹,但同一台计算机)工作簿中复制工作表。我正在使用以下代码,但由于未知原因,工作表不会被复制到我当前的工作簿。
Dim wb As Workbook
Dim activeWB As Workbook
Dim FilePath As String
Dim oWS As String
Set activeWB = Application.ActiveWorkbook
FilePath = "D:\General Required Docs\DATA.xlsm"
If optFirst.Value = True Then
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
oWS = cboMaterial.Value
Set wb = Application.Workbooks.Open(FilePath)
wb.Worksheets(oWS).Copy
After:=Application.ActiveWorkbook.Sheets(ThisWorkbook.Sheets.count)
activeWB.Activate
wb.Close False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
答案 0 :(得分:2)
更改
wb.Worksheets(oWS).Copy
After:=Application.ActiveWorkbook.Sheets(ThisWorkbook.Sheets.count)
到
wb.Worksheets(oWS).Copy
After:=activeWB.Sheets(activeWB.Sheets.count)
假设oWS是您要复制的工作表的索引。
答案 1 :(得分:0)
Sub Add_Bridge_1()
Dim wbk1 As Workbook, wbk2 As Workbook
'add your own file path
fileStr = "C:\Program Files\Microsoft Office\Office\HL DDS Templates.xlsx"
Set wbk1 = ActiveWorkbook
Set wbk2 = Workbooks.Add(fileStr)
'wbk2.Sheets("Bridge 1").Copy After:=Workbooks("WorkbookNameYouCopyCodeInto").Sheets(1)
wbk2.Sheets("Sheet Name").Copy After:=wbk1.Sheets(1)
wbk2.Saved = True
End Sub