【问题标题】:Copy worksheet from active workbook to another workbook in a different location将工作表从活动工作簿复制到不同位置的另一个工作簿
【发布时间】:2018-07-30 20:14:13
【问题描述】:

从一个大脚本中处理我当前活动工作簿中的数据,然后将数据复制到一个新工作表中。我引入了一个按钮,可以将同一张工作表从当前工作簿复制到另一个工作簿。

这里 D6 包含目标文件的实际路径。当我尝试使用 Windows + 运行选项打开链接时,它是正确且打开的

J12 包含新工作表的名称。

“Total”表示目标工作簿中的第一个工作表,在该工作表之后应该插入新工作簿。

Private Sub CopyToDest_Click()

        Dim destWbk As Workbook
        Dim sourceWbk As Workbook
        Dim shName As Variant
        Dim filePath As Variant

        Application.ScreenUpdating = False
        Application.DisplayAlerts = False

    'Set source and dest sheets
        Set sourceWbk = ThisWorkbook
        filePath = Sheet29.Range("D6").Value
        Set destWbk = Workbooks.Open(filePath)
        shName = Sheet29.Range("J12").Value

    'Copy sheet from source file to destination sheet
        sourceWbk.Sheets(shName).Copy After:=destWbk.Sheets("Total")
        destWbk.Save
        destWbk.Close

     'Delete copied sheet from source file
        Sheets(shName).Select
        ActiveWindow.SelectedSheets.Delete

        Application.ScreenUpdating = True
        Application.DisplayAlerts = True

        MsgBox "Sheet Copied Successfully", vbInformation, "Success"

End Sub

sourceWbk.Sheets(shName).Copy After:=destWbk.Sheets("Total") - 此代码有时会给我“运行时错误'9':下标超出范围错误,而有时它显示成功 msgbox。真是令人困惑的场景。有人可以帮忙吗?

提前致谢。

【问题讨论】:

  • 为什么不用“移动”而不是复制——那你只需要保存而不是删除...
  • 即使我使用 move 也会给我同样的错误。

标签: vba excel templates


【解决方案1】:

最后正常的宏录制帮助解决了这个问题。我刚刚录制了一个宏,并将静态值替换为动态值。甚至为用户添加了一个额外的选项,可以在特定工作表之后移动目标文件中的文件。

Private Sub CopyToDest_Click()

        Dim destWbk As Workbook
        Dim wbkName As String
        Dim shName As String, _
            dstShName As String
        Dim filePath As Variant, _
            dstFileName As Variant


        Application.ScreenUpdating = False
        Application.DisplayAlerts = False

        'check neccessary cells have values
        If Sheet29.Range("R12").Value = "" Then
                MsgBox "Destination worksheet name has to be selected in Cell R12"
                Exit Sub
            End If

    'Set source and dest sheets
        wbkName = ThisWorkbook.Name ' Storing name of macro workbook
        filePath = Sheet29.Range("D6").Value 'D6 holds the file location of destination sheet
        dstFileName = Sheet29.Range("D7").Value 'D7 holds the file name extracted from D6
        Set destWbk = Workbooks.Open(filePath) 'Open destination sheet
        shName = Sheet29.Range("J12").Value 'Contains sheetname in macro workbook which has to be moved
        dstShName = Sheet29.Range("R12").Value ' Contains sheetname of destination workbook after which the sheet has to be placed.

    'Copy sheet from macro file to destination sheet          
            Windows(wbkName).Activate
            Sheets(shName).Select
            Sheets(shName).Move After:=Workbooks(dstFileName).Sheets(dstShName)
            destWbk.Save
            destWbk.Close
            Windows(wbkName).Activate
            ActiveWorkbook.Save

        Application.ScreenUpdating = True
        Application.DisplayAlerts = True

        MsgBox "Sheet Copied Successfully", vbInformation, "Success"

End Sub

【讨论】:

  • 感谢您的跟进。您可以通过单击绿色复选标记关闭此线程,将您自己的答案标记为解决方案。
【解决方案2】:

该错误意味着该名称的工作表不存在。 “Total”或 shName 中的任何内容均无效。请记住,此处的名称是 Excel 中选项卡的名称,而不是 VBA 中工作表对象的名称。一个工作表有 2 个名称。

【讨论】:

  • 两者都是有效的,因为分配给 shName 的单元格值之前用于在宏工作簿中创建新工作表。这部分代码将创建的工作表移动到新工作簿。即使工作表名称是目标工作簿也是有效的,因为即使我手动将工作簿名称从目标工作簿复制粘贴到代码中,我也会遇到相同的错误。如何从工作表名称中获取工作表编号?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
相关资源
最近更新 更多