简体   繁体   English

将工作表从另一个工作簿(#2)导入到当前工作簿(#1)

[英]Import a worksheet from another workbook (#2) to current workbook (#1)

I wrote a code that is opening a window in which I can select a the excel workbook (#2) I want to copy and import the worksheet from. 我编写了一个代码,该代码正在打开一个窗口,从中可以选择要从中复制和导入工作表的excel工作簿(#2)。 The Code is then checking whether the wanted worksheet (named "Guidance") exists in the opened workbook (#2).If so it should be copied and pasted into the current workbook (#1). 然后,代码检查正在打开的工作簿(#2)中是否存在所需的工作表(名为“指导”)。如果是,则应将其复制并粘贴到当前工作簿中(#1)。 After pasting the worksheet the workbook (#2) should be closed again. 粘贴工作表后,应再次关闭工作簿(#2)。

So far the code does what I want it to do, as it opens the window and lets me select the wanted worksheet (named "Guidance") but I have the bug (not sure if the translation is correct) 到目前为止,代码已经完成了我想要的工作,因为它打开了窗口,让我选择了想要的工作表(名为“ Guidance”),但是我遇到了错误(不确定翻译是否正确)。

"Runtime error '9': index out of range" “运行时错误'9':索引超出范围”

where the worksheet is supposed to be copied and pasted. 应该将工作表复制并粘贴到的位置。

Any help on that would be very much appreciated! 任何帮助,将不胜感激! Thanks in advance. 提前致谢。

 Private Function SheetExists(sWSName As String, Optional InWorkbook As Workbook) As Boolean

 If InWorkbook Is Nothing Then
    Set InWorkbook = ThisWorkbook
 End If

 Dim ws As Worksheet
 On Error Resume Next
 Set ws = Worksheets(sWSName)
 If Not ws Is Nothing Then SheetExists = True

 On Error GoTo 0

 End Function


 Sub GuidanceImportieren()


 Dim sImportFile As String, sFile As String
 Dim sThisWB As Workbook
 Dim vFilename As Variant

 Application.ScreenUpdating = False
 Application.DisplayAlerts = False

 Set sThisWB = ActiveWorkbook
 sImportFile = Application.GetOpenFilename("Microsoft Excel Workbooks, 
 *xls; *xlsx; *xlsm")

 If sImportFile = "False" Then
 MsgBox ("No File Selected")
 Exit Sub

 Else
 vFilename = Split(sImportFile, "|")
 sFile = vFilename(UBound(vFilename))
 Application.Workbooks.Open (sImportFile)

 Set wbWB = Workbooks("sImportFile")
 With wbWB
 If SheetExists("Guidance") Then
 Set wsSht = .Sheets("Guidance")
 wsSht.Copy Before:=sThisWB.Sheets("Guidance")
 Else
 MsgBox ("No worksheet named Guidance")
 End If

 wbWB.Close SaveChanges:=False
 End With
 End If

 Application.ScreenUpdating = True
 Application.DisplayAlerts = True

 End Sub

The issue is here 问题在这里

Set wbWB = Worksheets("Guidance") '<-- this should be a workbook not a worksheet?
With wbWB '<-- this with is useless until …
    If SheetExists("Guidance") Then
        Set wsSht = .Sheets("Guidance") '<-- … until Sheets here starts with a dot
        wsSht.Copy Before:=sThisWB.Sheets("Guidance") 'if the error is here then there is no sheet "Guidance" in sThisWB
    Else
        MsgBox ("No worksheet named Guidance")
    End If
    wbWB.Close SaveChanges:=False
End With

Also note that SheetExists("Guidance") does not check in a specific workbook (which may fail). 另请注意, SheetExists("Guidance")不会签入特定的工作簿(这可能会失败)。 I recommend to extend the function to: 我建议将功能扩展到:

Private Function SheetExists(WorksheetName As String, Optional InWorkbook As Workbook) As Boolean
    If InWorkbook Is Nothing Then
        Set InWorkbook = ThisWorkbook 'fallback if not set
    End If

    Dim ws As Worksheet
    On Error Resume Next
    Set ws = InWorkbook.Worksheets(WorksheetName)
    SheetExists = Not ws Is Nothing
    On Error Goto 0 'necessary because the Err.Number will not be cleared on End Function
End Function

So you can test if a worksheet exists in a specific workbook like 因此,您可以测试特定工作簿中是否存在工作表,例如

SheetExists("Guidance", sThisWB)
SheetExists("Guidance", wbWB)

Sub GuidanceImportieren()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim sImportFile As String
    sImportFile = Application.GetOpenFilename("Microsoft Excel Workbooks, *xls; *xlsx; *xlsm")

    If sImportFile = False Then 'false should not be "false"
        MsgBox "No File Selected"
        Exit Sub
    Else
        Dim vFilename As Variant
        vFilename = Split(sImportFile, "|")

        Dim sFile As String
        sFile = vFilename(UBound(vFilename))

        Dim ImportWorkbook As Workbook
        Set ImportWorkbook = Application.Workbooks.Open(sImportFile)

        If SheetExists("Guidance", ImportWorkbook) Then
            ImportWorkbook.Sheets("Guidance").Copy Before:=ThisWorkbook.Sheets("Guidance")
            'you might need to change it into something like this:
        Else
            MsgBox "No worksheet named Guidance"
        End If

        ImportWorkbook.Close SaveChanges:=False
    End If

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA 将工作表(带变量名)从另一个工作簿复制到当前工作簿并在当前工作簿中重命名 - VBA Copy worksheet (with variable name) from another workbook to the current workbook and rename it in the current workbook 将工作表从一个工作簿复制到另一工作簿 - Copy worksheet from one Workbook to another Workbook 根据当前工作簿上单元格中的数据,确定要从另一个工作簿复制什么工作表 - Determin what worksheet to copy from another workbook based on data in a cell on the current workbook 将复选框从另一个工作簿复制到当前工作簿? - Copy checkbox from another workbook to current workbook? 将工作表添加到当前工作簿 - Add Worksheet To Current Workbook 将 XML 导入工作表而不是工作簿 - Import XML into worksheet not workbook 将工作表从一个Excel工作簿粘贴到另一工作簿时出错 - Error pasting a worksheet from one excel workbook to another workbook 将工作表从一个Excel工作簿复制到另一工作簿 - copy a worksheet from one excel workbook to another workbook 从另一个工作簿复制工作表后获取创建的工作簿名称 - Getting the created workbook name after copying worksheet from another workbook 想要使用 openpyxl 将工作表从一个工作簿添加到另一个工作簿 - want to add worksheet from one workbook in to another workbook using openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM