1

我正在做一个小项目,我需要从另一个工作簿中的任何外部工作表数据库将工作表添加到当前打开的工作簿中。我制作了一个表格来导入当前打开(活动)工作簿中所需的任何工作表。

工作表将从远程(在同一计算机以外的任何其他文件夹中)工作簿中复制。我正在使用以下代码,但由于未知原因,这些工作表没有被复制到我当前的工作簿中。

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
4

2 回答 2

2

改变

wb.Worksheets(oWS).Copy      
   After:=Application.ActiveWorkbook.Sheets(ThisWorkbook.Sheets.count)

wb.Worksheets(oWS).Copy      
   After:=activeWB.Sheets(activeWB.Sheets.count)

假设 oWS 是您要复制的工作表的索引。

于 2013-05-15T12:50:38.587 回答
2
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
于 2016-03-29T21:27:32.257 回答