简体   繁体   English

Excel VBA打开工作簿并切换到其他工作表

[英]Excel VBA to open a workbook and switch to a different worksheet

Good Afternoon. 下午好。 I am currently very novice a VBA and am trying to create a simple code that will open a workbook, flip to a specific worksheet (Transaction Analysis) and run two macros. 我目前是VBA的新手,正在尝试创建一个简单的代码来打开工作簿,翻转到特定的工作表(事务分析)并运行两个宏。 Then is should flip to worksheet "Fund Trend" and run those same two macros, then save and close. 然后应转到工作表“资金趋势”并运行相同的两个宏,然后保存并关闭。 I found this code online and have slightly modified to try to open a specific sheet within the workbook with no luck. 我在网上找到了此代码,并稍加修改以尝试在工作簿中打开特定工作表而没有运气。

If I exclude the line regarding "ActiveWorkbook.Sheets("Transaction Analysis") then the script works correctly for the open worksheet. It is when I try to add anything referencing a specific worksheet within the workbook that I run into issues. The the script shown below, the workbook opens and that's it. The script stops there and does not run the macros. 如果我排除有关“ ActiveWorkbook.Sheets(“交易分析”)的行,则该脚本对于打开的工作表而言是正确的。这是当我尝试添加引用工作表中特定工作表的任何内容时遇到的问题。如下所示,将打开工作簿,仅此而已,脚本在那里停止并且不运行宏。

I would really appreciate any help or suggestions! 我将不胜感激任何帮助或建议! The idea is to schedule schedule the execution of the final vbscript with task scheduler. 这个想法是使用任务计划程序来计划最终vbscript的执行。 Thank You! 谢谢!

Dim args, objExcel

Set args = wscript.Arguments
set objExcel = CreateObject("Excel.Application")

objExcel.workbooks.open args(0)
objExcel.Visible = True

ActiveWorkbook.Sheets("Transaction Analysis")

objExcel.Run "Clear"

objExcel.Run "SecurityDistribution"

objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close(0)
objExcel.Quit

Are you saying to want to run a Macro in another Workbook other than the one that has focus? 您是否要在另一个具有焦点的工作簿中运行宏? If that's all it is, try this one-liner. 如果仅此而已,请尝试使用这种单线。

Application.Run "MacroBook!MacroName" 运行“ MacroBook!MacroName”

Notice, you don't need to select a specific Worksheet to run a Macro. 注意,您不需要选择特定的工作表即可运行宏。 You do, however, need to select the right Workbook! 但是,您确实需要选择正确的工作簿!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM