我知道你可以从win cmd行打开一个Excel文件。 但是,如何使用win cmd在该文件中打开特定的电子表格?
ExcelSheet2.vbs
strFileName = "c:\temp\testa.xlsx"
更改为所需的Excel文件路径 该代码具有错误处理的情况下,文件路径是错误的,或第二个工作表不存在。
[ 更新:进一步的错误处理,以测试被隐藏的第二张表]
Const xlVisible = -1 Dim objExcel Dim objWb Dim objws Dim strFileName strFileName = "c:\temp\test.xlsx" On Error Resume Next Set objExcel = CreateObject("excel.application") Set objWb = objExcel.Workbooks.Open(strFileName) Set objws = objWb.Sheets(2) On Error GoTo 0 If Not IsEmpty(objws) Then If objws.Visible = xlVisible Then objExcel.Goto objws.Range("a1") Else wscript.echo "the 2nd sheet is present but is hidden" End If objExcel.Visible = True Else objExcel.Quit Set objExcel = Nothing If IsEmpty(objWb) Then wscript.echo strFileName & " not found" Else wscript.echo "sheet2 not found" End If End If
或者,您可以从命令行打开工作簿并将以下代码添加到工作簿以激活“Sheet2”
Private Sub Workbook_Open() ThisWorkbook.Sheets("Sheet2").Activate End Sub
您需要确保工作簿处于可信位置,并且安全设置允许宏运行。 @ brettdj的解决方案要优越得多,但这是另一种选择。