如何从一系列代码中移出,在另一个模块中执行不同的代码,然后返回
how to move from one series of code, execute a different on in another module, and step back in
在我正在处理的这段代码中,它将打开一个定义的工作簿,其中有一个 "on open" 事件,该事件将处理传输大量数据,然后另存为。一切都很好,这就是我迷路的地方...
传输位后需要将焦点设置回父书,很简单,但需要返回父代码来处理最后的步骤并关闭传输书...
现在,在开始之前,我要分离代码,因为这个工作簿已经是一个很重的文件,我尽量不压倒它,还要记住,我的客户计算机通常不如我的好,所以陷入困境它确实使他们的计算机陷入困境。
我已经启动了一个 continue 事件,但我不确定如何将焦点重新设置到该行代码上。
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
我只需要它退回到父代码并继续执行。任何想法都会很棒!提前致谢!
打开工作簿时,将其设置为对对象变量的引用,然后您可以轻松地引用它并使用它进行操作。
我个人不建议设置 "focus" 来做一些事情,但如果您需要它,请参阅下面的代码。
' 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
您可以通过 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
以下子程序在 e 期间每秒检查一次。 G。 10 秒,如果另一个工作簿仍处于打开状态。如果其他工作簿已完成它的工作并同时自行关闭,则您可以完成剩余的工作。
如果另一个工作簿没有自行关闭,您需要通过 e 确认另一个工作簿的任务已完成。 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
我认为只在传输工作簿事件的末尾添加一行
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
可以根据您的参数修改。
在我正在处理的这段代码中,它将打开一个定义的工作簿,其中有一个 "on open" 事件,该事件将处理传输大量数据,然后另存为。一切都很好,这就是我迷路的地方...
传输位后需要将焦点设置回父书,很简单,但需要返回父代码来处理最后的步骤并关闭传输书...
现在,在开始之前,我要分离代码,因为这个工作簿已经是一个很重的文件,我尽量不压倒它,还要记住,我的客户计算机通常不如我的好,所以陷入困境它确实使他们的计算机陷入困境。
我已经启动了一个 continue 事件,但我不确定如何将焦点重新设置到该行代码上。
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
我只需要它退回到父代码并继续执行。任何想法都会很棒!提前致谢!
打开工作簿时,将其设置为对对象变量的引用,然后您可以轻松地引用它并使用它进行操作。
我个人不建议设置 "focus" 来做一些事情,但如果您需要它,请参阅下面的代码。
' 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
您可以通过 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
以下子程序在 e 期间每秒检查一次。 G。 10 秒,如果另一个工作簿仍处于打开状态。如果其他工作簿已完成它的工作并同时自行关闭,则您可以完成剩余的工作。
如果另一个工作簿没有自行关闭,您需要通过 e 确认另一个工作簿的任务已完成。 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
我认为只在传输工作簿事件的末尾添加一行
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
可以根据您的参数修改。