在我正在处理的这段代码中,将打开一个定义的工作簿,该工作簿具有一个“ 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
我只需要它退回到父代码并继续执行。任何想法都很棒!预先谢谢你!
答案 0 :(得分: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
答案 1 :(得分: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
以下子项在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
答案 2 :(得分:0)
我认为仅在传输工作簿事件结束时添加一行
<uses-permission android:name="android.permission.INTERNET"/>
在打开传输工作簿后在父工作簿代码中添加以下三行将解决该问题
Workbooks("Parent.xlsm").Worksheets(1).Range("K1").Value = True
可以根据您的参数进行修改。