excel VBA error: The object invoked has disconnected from its clients

前端 未结 1 434
一生所求
一生所求 2021-01-25 04:24

It seems a normal question and I have searched and tried many suggestions here but the error persists. I want to copy a \"case1\" sheet from current workbook to an existing work

相关标签:
1条回答
  • 2021-01-25 05:19

    Always set references to sheets and workbooks, then there is no chance of confusing which is which. This code does the same thing and should not give you any errors.

    Sub CopySheet()
        Dim wb1 As Workbook, ws1 As Worksheet 'Source
        Dim wb2 As Workbook, ws2 As Worksheet   'Target
    
        Set wb1 = ThisWorkbook
        Set ws1 = wb1.Sheets("case1")
        Set wb2 = Workbooks.Open("c:\workbook2.xlsx")
        Set ws2 = wb2.Sheets("case2")
    
        ws1.Copy Before:=ws2
        wb2.Close True
    End Sub
    
    0 讨论(0)
提交回复
热议问题