WSH VBScript切换workSHEET已经打开了Excel workBOOK

时间:2013-06-13 09:32:39

标签: excel vbscript wsh

是否可以在WSH VBScript(由名称引用)中切换工作簿中的工作表,如果是,我该怎么做?

这将在已打开Excel文件的脚本中使用。我想访问名为“版本控制”的工作表。

以下是我打开工作簿的方式:

xlsFile = path & "\xml-sitemap.xls"
Set objExcel = WScript.CreateObject("Excel.Application")
objExcel.Workbooks.open(xlsFile)

3 个答案:

答案 0 :(得分:2)

这样做(根据Ekkehard.Horner的评论修改)

Dim ws
Dim wb

xlsFile = path & "\xml-sitemap.xls"
Set objExcel = WScript.CreateObject("Excel.Application")
objExcel.Workbooks.open(xlsFile)
Set wb = objExcel.ActiveWorkbook 'the ActiveWorkbook will refer to the opened workbook
Set ws = wb.Worksheets("Version Control") 'ws is a Worksheet object

答案 1 :(得分:1)

从VBScript自动化Excel时,Excel中提供的VBA代码的全局对象将丢失。因此,最小的“工作表”脚本应如下所示:

Option Explicit

Dim sFSpec : sFSpec     = "P:\athto\your.xls"
Dim oExcel : Set oExcel = CreateObject("Excel.Application")
Dim oWB    : Set oWB    = oExcel.Workbooks.Open(sFSpec)
Dim oWS, sWSName

Set oWS = oWB.Worksheets(1)
sWSName = oWS.Name
WScript.Echo 0, sWSName

Set oWS = oWB.Worksheets(2)
WScript.Echo 1, oWS.Name

Set oWS = oWB.Worksheets(sWSName)
WScript.Echo 2, oWS.Name

oExcel.Quit

答案 2 :(得分:0)

使用VBScript,您可以返回带有“.name”扩展名的Excel电子表格选项卡的名称。

Dim excelPath, objExcel, oData, Excel_tab

excelPath = "C:\Users\user.name\Desktop\file.xls"
Set objExcel = CreateObject("Excel.Application")
Set oData = objExcel.Workbooks.Open(excelPath)
Excel_tab = objExcel.ActiveWorkbook.Worksheets(1).name
msgbox(Excel_tab)

objExcel.quit