我知道你可以通过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的解决方案更加优秀,但这是一种替代方法。