Excel to close trailing window

huangapple go评论147阅读模式
英文:

Excel to close trailing window

问题

这是我用于打开文件的代码:

Public openwb As Workbook
Sub testOpenWBOneDrive()
   Dim wbFullName, objLogExcel As Object
   Set objLogExcel = CreateObject("Excel.Application")
   objLogExcel.Visible = True
   wbFullName = "https://mysmartplace-my.sharepoint.com/personal/Misc/data.xlsx"
   Set openwb = objLogExcel.Workbooks.Open(wbFullName)
End Sub

这是我用于实际复制粘贴的代码:

Sub get_data()
 Dim wb As Workbook
 Dim ws As Worksheet
 Dim wn As Worksheet
 Dim lastrow
 Dim supercopy As Range

Application.ScreenUpdating = False

' 打开其他工作簿
testOpenWBOneDrive
Set ws = openwb.Worksheets("data")
Set wn = ThisWorkbook.Worksheets("Helper Sheet")

With ws
    lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
    wn.Unprotect Password:=pass

    .Range(.Cells(1, 1), .Cells(lastrow, 5)).Copy
    wn.Activate
    wn.Range("A1").Select
    wn.Paste
    
    wn.Protect Password:=pass
End With

openwb.Save

' 关闭我们刚刚获取数据的工作簿,并确保不保存它(以防意外更改)
openwb.Activate
openwb.Close
ActiveWindow.Close ' 这应该关闭新窗口,但关闭整个Excel

Application.ScreenUpdating = True

End Sub

我的问题是,当我关闭辅助工作簿后,仍然会有一个残留的窗口保持打开。

我尝试在代码中激活它并关闭,如您所见(已注释),我的主工作簿的窗口关闭(以及工作簿),而不是残留的窗口(data.xlsx工作簿关闭,但不关闭其窗口)。

非常感谢您的帮助。

英文:

Hi I use a code to copy paste from a one drive excel sheet to another

This is my code to open the file:

Public openwb As Workbook
Sub testOpenWBOneDrive()
   Dim wbFullName, objLogExcel As Object
   Set objLogExcel = CreateObject("Excel.Application")
   objLogExcel.Visible = True
   wbFullName = "https://mysmartplace-my.sharepoint.com/personal/Misc/data.xlsx"
   Set openwb = objLogExcel.Workbooks.Open(wbFullName)
   
End Sub

And my code to run the actual copy paste:

Sub get_data()
 Dim wb As Workbook
 Dim ws As Worksheet
 Dim wn As Worksheet
 Dim lastrow
 Dim supercopy As Range

Application.ScreenUpdating = False


'Open the other workbook
testOpenWBOneDrive
Set ws = openwb.Worksheets("data")
Set wn = ThisWorkbook.Worksheets("Helper Sheet")

With ws
    
    lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
    wn.Unprotect Password:=pass

    .Range(.Cells(1, 1), .Cells(lastrow, 5)).Copy
    wn.Activate
    wn.Range("A1").Select
    wn.Paste
    
    

    wn.Protect Password:=pass

End With

openwb.Save

'Close the workbook from which we just got some data and make sure not to save it in case
'accidental changes were made to it.
openwb.Activate
openwb.Close
ActiveWindow.Close 'This should close the new window but closes Excel altogether

Application.ScreenUpdating = True

End Sub

My issue is after I close the helper workbook, there is a trailing window that keeps open.

I tried activating it and closing as you can see in my code(as commented) the window of my main workbook closes(and the workbook) instead of the lingering window (data.xlsx workbook does close but not its window)

Thanks for your help in advance

答案1

得分: 1

你的代码打开了一个新的Excel实例:

Set objLogExcel = CreateObject("Excel.Application")

在关闭工作簿之后,你也必须使用 Quit 来关闭实例,所以解决方案应该是:

objLogExcel.Quit

问题在于你在 testOpenWBOneDrive 中声明了 objLogExcel,所以你不能从子程序 get_data 中关闭它。

简单的解决方案就是将变量声明为公共变量:

Public objLogExcel As Object

但说实话,我认为这样更好:

Sub get_data()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim wn As Worksheet
    Dim lastrow As Long
    Dim supercopy As Range
    Dim wbFullName As String
    Dim objLogExcel As Object

    Application.ScreenUpdating = False

    '打开另一个工作簿
    Set objLogExcel = CreateObject("Excel.Application")
    objLogExcel.Visible = True
    wbFullName = "https://mysmartplace-my.sharepoint.com/personal/Misc/data.xlsx"
    Set openwb = objLogExcel.Workbooks.Open(wbFullName)

    Set ws = openwb.Worksheets("data")
    Set wn = ThisWorkbook.Worksheets("Helper Sheet")

    With ws
        lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
        wn.Unprotect Password:=pass

        .Range(.Cells(1, 1), .Cells(lastrow, 5)).Copy
        wn.Activate
        wn.Range("A1").Select
        wn.Paste
        wn.Protect Password:=pass
    End With

    openwb.Save

    '关闭刚刚获取数据的工作簿,并确保不保存它,以防意外更改。
    openwb.Activate
    openwb.Close
    ActiveWindow.Close '这应该关闭新窗口,但会关闭整个Excel

    '关闭Excel对象
    objLogExcel.Quit

    Application.ScreenUpdating = True
End Sub

请注意,这段代码将你的变量 openwb 视为先前在其他地方声明为公共的变量。

我强烈建议阅读了解范围和可见性

英文:

there is a trailing window that keeps open

Your code opens a new instance of Excel here:

Set objLogExcel = CreateObject("Excel.Application")

After closing the workbook, you must also close the instance with Quit so the solution would be:

objLogExcel.Quit

The problem is that you declare objLogExcel inside testOpenWBOneDrive so you cannot close it from your sub get_data.

Easy solution is just declaring the variable as Public:

Public objLogExcel As Object

But to be honest, I think it would be better like this:

Sub get_data()
Dim wb As Workbook
Dim ws As Worksheet
Dim wn As Worksheet
Dim lastrow As Long
Dim supercopy As Range
Dim wbFullName As String
Dim objLogExcel As Object


Application.ScreenUpdating = False

'Open the other workbook
Set objLogExcel = CreateObject("Excel.Application")
objLogExcel.Visible = True
wbFullName = "https://mysmartplace-my.sharepoint.com/personal/Misc/data.xlsx"
Set openwb = objLogExcel.Workbooks.Open(wbFullName)

Set ws = openwb.Worksheets("data")
Set wn = ThisWorkbook.Worksheets("Helper Sheet")

With ws
    lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
    wn.Unprotect Password:=pass

    .Range(.Cells(1, 1), .Cells(lastrow, 5)).Copy
    wn.Activate
    wn.Range("A1").Select
    wn.Paste
   wn.Protect Password:=pass
End With

openwb.Save

'Close the workbook from which we just got some data and make sure not to save it in case
'accidental changes were made to it.
openwb.Activate
openwb.Close
ActiveWindow.Close 'This should close the new window but closes Excel altogether

'close Excel object
objLogExcel.Quit

Application.ScreenUpdating = True

End Sub

Please, note this code is considering your variable openwb as declared previously somewhere else as Public.

I strongly suggest to read Understanding scope and visibility

huangapple
  • 本文由 发表于 2023年3月15日 19:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定