激活最后一个 Excel 工作簿在多次运行后。

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

Activate last Excel Workbook after multiple runs

问题

以下是您提供的代码部分的翻译:

'设置一个新的Excel实例,或者如果Excel没有运行,则启动它
On Error Resume Next
Set Xl = GetObject(, "Excel.application")
If Err <> 0 Then
    On Error GoTo 0
    Set Xl = CreateObject("Excel.Application")
    If Err <> 0 Then
        MsgBox "此工作站上不可用Excel应用程序" _
            & vbCr & "安装Excel或检查网络连接", vbCritical, _
            "项目数据导出-严重错误"
        FilterApply Name:="所有任务"
        Set Xl = Nothing
        On Error GoTo 0     '清除错误函数
        Exit Sub
    End If
End If
On Error GoTo 0
'创建一个具有两个工作表的工作簿
Xl.Workbooks.Add
Xl.ActiveWorkbook.Worksheets(1).Name = "资源信息"
Set WS1 = Xl.ActiveWorkbook.Worksheets(1)
Xl.ActiveWorkbook.Worksheets(2).Name = "月度数据"
Set WS2 = Xl.ActiveWorkbook.Worksheets(2)

'在电子表格完成之前将Excel保持在后台和最小化(加快传输速度)
Xl.Visible = False
Xl.ScreenUpdating = True
Xl.DisplayAlerts = False

然后,宏继续执行导出。完成后,意图是在Excel中进行一些最终格式化并显示结果。对于第一次运行,一切都很好。问题发生在执行多次运行时。当将Excel对象设置为可见时,第一个创建的工作簿显示为活动可见工作簿。因此,我认为,好的,我将确保最后一个创建的工作簿(当前运行)是活动焦点,以便可以进行格式化。为此,我添加了以下一组代码。当逐步执行代码时,它可以正常工作,但当代码“自由”运行时,当它尝试选择 WS1.Rows(2).Select 时,会发生运行时错误,因为活动工作簿是Workbook1,而不是最新的工作簿。

'格式化已完成的工作表
Xl.Visible = True
'确保最新的工作簿是焦点
Xl.Workbooks(Xl.Workbooks.Count).Activate
Set WS1 = Xl.Workbooks(Xl.Workbooks.Count).Worksheets(1)
Set WS2 = Xl.Workbooks(Xl.Workbooks.Count).Worksheets(2)
WS1.Columns("A:F").AutoFit
WS1.Rows(2).Select
Xl.ActiveWindow.FreezePanes = True
WS1.Range("A1").Select
WS2.Activate
WS2.UsedRange.Columns.AutoFit
WS2.Range("C3").Select
Xl.ActiveWindow.FreezePanes = True
TotTim = Timer - TimSt
'暂时隐藏Excel以显示结果
Xl.Visible = False

MsgBox "导出完成" & vbCr & _
    "   导出时间: " & TotTim & " 秒", vbInformation
Xl.Visible = True
Xl.Workbooks(Xl.Workbooks.Count).Activate
Set Xl = Nothing
End Sub

有何建议?我期望最新创建的工作簿是活动工作簿,以便可以执行基于选择的格式化。

英文:

I have a macro running in MS Project that exports timescaled data to a pre-formatted Excel Workbook. The export works fine. The issue is doing the final formatting in Excel. This is the code snippet that sets up the instance of Excel and creates a new Workbook.

&#39;set up an new instance of Excel, or if Excel is not running, start it
On Error Resume Next
Set Xl = GetObject(, &quot;Excel.application&quot;)
If Err &lt;&gt; 0 Then
    On Error GoTo 0
    Set Xl = CreateObject(&quot;Excel.Application&quot;)
    If Err &lt;&gt; 0 Then
        MsgBox &quot;Excel application is not available on this workstation&quot; _
            &amp; vbCr &amp; &quot;Install Excel or check network connection&quot;, vbCritical, _
            &quot;Project Data Export - Fatal Error&quot;
        FilterApply Name:=&quot;all tasks&quot;
        Set Xl = Nothing
        On Error GoTo 0     &#39;clear error function
        Exit Sub
    End If
End If
On Error GoTo 0
&#39;create a workbook with two worksheets
Xl.Workbooks.Add
Xl.ActiveWorkbook.Worksheets(1).Name = &quot;Resource Information&quot;
Set WS1 = Xl.ActiveWorkbook.Worksheets(1)
Xl.ActiveWorkbook.Worksheets(2).Name = &quot;Monthly Data&quot;
Set WS2 = Xl.ActiveWorkbook.Worksheets(2)

&#39;Keep Excel in the background and minimized until spreadsheet is done (speeds transfer)
Xl.Visible = False
Xl.ScreenUpdating = True
Xl.DisplayAlerts = False

Then the macro proceeds to execute the export. When finished the intent is to do some final formatting and display the result in Excel. For the first run, all is great. The problem occurs when multiple runs are performed. When the Excel object is set back to visible, the first Workbook created shows as the active visible workbook. So I thought, okay, I'll just insure the last Workbook created (current run) is the active focus so it can be formatted. To do that I added this set of code. When the code is stepped through, it works fine but when the code runs "free" a runtime error occurs when it comes to selecting, WS1.Rows(2).Select, because the active Workbook is, you guessed it, Workbook1, not the latest Workbook.

&#39;format completed worksheets
Xl.Visible = True
&#39;make sure latest workbook is focus
Xl.Workbooks(Xl.Workbooks.Count).Activate
Set WS1 = Xl.Workbooks(Xl.Workbooks.Count).Worksheets(1)
Set WS2 = Xl.Workbooks(Xl.Workbooks.Count).Worksheets(2)
WS1.Columns(&quot;A:F&quot;).AutoFit
WS1.Rows(2).Select
Xl.ActiveWindow.FreezePanes = True
WS1.Range(&quot;A1&quot;).Select
WS2.Activate
WS2.UsedRange.Columns.AutoFit
WS2.Range(&quot;C3&quot;).Select
Xl.ActiveWindow.FreezePanes = True
TotTim = Timer - TimSt
&#39;temporarily hide Excel to display result
Xl.Visible = False

MsgBox &quot;Export is complete&quot; &amp; vbCr &amp; _
    &quot;   Export time: &quot; &amp; TotTim &amp; &quot; sec&quot;, vbInformation
Xl.Visible = True
Xl.Workbooks(Xl.Workbooks.Count).Activate
Set Xl = Nothing
End Sub

Any suggestions?

I expected the latest Workbook created to be the active Workbook so the selection based formatting can be accomplished

答案1

得分: 0

创建工作簿时,将新工作簿保存在一个变量中以备将来参考:

'创建包含两个工作表的工作簿
Dim wbk As Workbook
Set wbk = Xl.Workbooks.Add

然后稍后:

'确保最新的工作簿处于焦点状态
wbk.Activate
Set WS1 = wbk.Worksheets(1)
Set WS2 = wbk.Worksheets(2)
WS1.Columns("A:F").AutoFit
Ws1.Activate
WS1.Rows(2).Select
英文:

When creating the workbook, capture the new workbook in a variable for future reference:

&#39;create a workbook with two worksheets
Dim wbk As Workbook
Set wbk = Xl.Workbooks.Add

Then later:

&#39;make sure latest workbook is focus
wbk.Activate
Set WS1 = wbk.Worksheets(1)
Set WS2 = wbk.Worksheets(2)
WS1.Columns(&quot;A:F&quot;).AutoFit
Ws1.Activate
WS1.Rows(2).Select

huangapple
  • 本文由 发表于 2023年6月19日 03:57:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502327.html
匿名

发表评论

匿名网友

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

确定