如何从一个系列的代码移动,在另一模块上执行一个不同的,并且在步骤背面

问题描述 投票:0回答:3

在这段代码我工作的它会打开一个工作簿定义它有一个“开放式”的事件,将处理传输大量的数据,然后保存为。所有这一切都很好,这是我在哪里输了...

转送位后,需要将焦点设置回父书,易,但其需要后退一步到父代码来处理的最后一个步骤,并关闭转印书...

现在,它莫属,因为该工作簿已经是一个沉重的档案我我分开的代码之前,我想没有压倒它,也记住我的客户端的计算机通常不是我的一样好,所以受困下来真的停滞不前它他们的计算机上。

我已经戒了周围不断的事件,但我不知道如何将焦点重新设置到该行的代码。

Sub TransferMe()
    'Runs the script to start the transfer manager

    answer = MsgBox("This will transfer then clear all data for new period, are you sure you want to continue?", vbYesNo, Title:="New Period")
    If answer = vbYes Then
        MsgBox ("Please be patient this may take a few minuets."), Title:="Please Wait..."

        Application.Cursor = xlWait

        'open the transfer manager
        Workbooks.Open Filename:="C:\Users\dlroy\Dropbox\SSS\WORKING OR COMPLETE\Ian McDermid - Pump Bar\Prime Cost Suite\TransManager.xlsm"

        'this is where the transfer workbook opens which has an "on open" event
        'that will handle transferring all of my data
        'it then needs to set focus back on the original worksheet and restart the code

        'Ending code will handle closing the transfer workbook with out 
        'saving as it will already save as
        'and then complete the last couple of steps and end the macro.

        Application.Cursor = xlDefault
    Else
        MsgBox ("Goodbye."), Title:="Exit"
        Exit Sub     
    End If             
End Sub

我只需要它退一步到父代码并继续执行。任何想法将真棒!先感谢您!

excel vba multithreading continue
3个回答
0
投票

您可以通过Application.OnTime定时器处理这个问题。

通过打开第二个工作簿,启动定时器:

Option Explicit

Private TimerStart As Double
Private Const SecondsToWait As Double = 10
Private OtherWorkbook As Workbook

Private Sub StartOtherWorkbookAndTimer()
    TimerStart = Timer
    Application.OnTime Now + TimeValue("00:00:01"), "CheckIfOtherWorkbookFinished"
    Workbooks.Open (Application.DefaultFilePath & "\NameOfOtherWorkbook.xlsm")
End Sub

下面的子支票Ë期间每一秒。 G。 10秒,如果其他工作簿仍处于打开状态。如果其他工作簿做它的工作,并在此期间关闭本身,你可以做剩下的工作。

如果其他工作簿不关闭本身,你需要通过电子邮件认识的其他工作簿的任务的完成。 G。在它的值是第一个单元格。这也是本次处理:

Private Sub CheckIfOtherWorkbookFinished()
    Dim secondsElapsed As Double
    secondsElapsed = Timer - TimerStart
    On Error Resume Next
    Set OtherWorkbook = Workbooks("NameOfOtherWorkbook.xlsm")
    On Error GoTo 0
    If OtherWorkbook Is Nothing Then
        MsgBox "Other workbook is closed. Now I do the remaining work ..."
        ' do the remaining work here, if other workbook is closed within 10 seconds
    ElseIf OtherWorkbook.Worksheets(1).Range("A1").Value = "ready" Then
        MsgBox "Other workbook is ready. Now I do the remaining work ..."
        ' do the remaining work here, if other workbook said "ready" in it's first cell
        OtherWorkbook.Close
    ElseIf Int(SecondsToWait - secondsElapsed) > 0 Then
        Application.OnTime Now + TimeValue("00:00:01"), "CheckIfOtherWorkbookFinished"
    Else
        MsgBox SecondsToWait & " seconds elapsed, but other workbook still open?!"
    End If
End Sub

0
投票

当打开工作簿时,将其设置为对象变量的引用,那么你可以参考它很容易和做的东西吧。

我个人不建议设置“焦点”做的东西,但在你需要它时,看到代码波纹管。

' Declare object variables
Dim mainWorkbook As Excel.Workbook

'open the transfer manager
Set mainWorkbook = Workbooks.Open(Filename:="C:\Users\dlroy\Dropbox\SSS\WORKING OR COMPLETE\Ian McDermid - Pump Bar\Prime Cost Suite\TransManager.xlsm")

' Refer to a sheet
debug.print mainworkbook.Worksheets(1).Name

' Set focus
mainWorkbook.Activate

' Close it
mainworkbook.Close

0
投票

我认为只有添加一行在转移工作簿事件结束

Workbooks("Parent.xlsm").Worksheets(1).Range("K1").Value = True

和打开传递工作簿就能解决问题之后加入以下在父工作簿代码三行

ThisWorkbook.Worksheets(1).Range("K1").Value = False
    Do While ThisWorkbook.Worksheets(1).Range("K1").Value = False
    DoEvents
    Loop

可以根据自己的参数修改。

© www.soinside.com 2019 - 2024. All rights reserved.