溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在VBS中遍歷Excel工作表

發(fā)布時(shí)間:2021-04-17 16:11:36 來源:億速云 閱讀:229 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹怎么在VBS中遍歷Excel工作表,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

核心代碼

'******************************************
'拖拽文件,獲取文件路徑
'******************************************
If wscript.Arguments.count=0 then 
		msgbox "拖拽文件到本圖標(biāo)",0,"提示"
End if 
 
 
for a=0 to wscript.Arguments.count-1
 
	strPath=wscript.Arguments(a)	
	
next
'******************************************
'定義Excle對(duì)象、工作薄對(duì)象、工作表對(duì)象
'******************************************
dim oExcel,oWb,oSheet
 
set ws=WScript.createobject("wscript.shell")
Set oExcel=CreateObject("Excel.Application")
'打開指定的工作簿
Set oWb=oExcel.Workbooks.Open(strPath)
'顯示打開的Excel工作簿
oExcel.visible=true
'******************************************
'遍歷工作簿的所有工作表
'******************************************
for j= 1 to oWb.sheets.count
	set oSheet=oWb.Sheets(j)
	'選中并激活工作表
	oSheet.Activate
	oSheet.Range("A1")="成功"
 
next

 Excel遍歷所有工作簿中所有工作表執(zhí)行宏

Sub test()
n = Worksheets.Count
For i = 1 To n
Worksheets(i).Activate
Macro1
Next
End Sub

 Macro1是宏的名稱

 使用VBS遍歷EXCEL

Dim xlApp,xlSheet,xlWorkBookDim iRowCount,iLoop,jLoop,jColumnCount,numAdd
Set xlApp=CreateObject("Excel.Application")
xlApp.Visible=True
Set xlWorkBook=xlApp.Workbooks.Open("C:\data.xls")
Set xlSheet=xlWorkBook.Sheets("Sheet1")
iRowCount=xlSheet.UsedRange.Rows.Count
jColumnCount=xlSheet.UsedRange.Columns.Count
For iLoop=1 To iRowCount
 For jLoop=1 To jColumnCount
 MsgBox(xlSheet.cells(iLoop,jLoop).value)
 Next
Next

xlWorkBook.Save
xlWorkBook.Close
xlApp.Quit

VBScript 編寫 自動(dòng)Excel文件內(nèi)容到數(shù)組并提示輸出

解壓到任意目錄,點(diǎn)擊VBS文件執(zhí)行,程序自動(dòng)讀取文件所在目錄的Excel文件到數(shù)組中,并通過提示框逐個(gè)輸出,提示框1s自動(dòng)關(guān)閉。

Dim oExcel,oWb,oSheet 
Set oExcel= CreateObject("Excel.Application") 
Set oWb = oExcel.Workbooks.Open(dir&"\DataReport.xls") 
Set oSheet = oWb.Sheets("HistoryData")      

Dim i
Dim a(150)

For i = 5 To 145 '145-5+1 = 141 
a(i-5) = oSheet.Range("B"&i).Value
print "data=",a(i-5)
next

Set oSheet = Nothing 

oExcel.Workbooks.Close


oExcel.Quit '關(guān)閉excel.exe'


Function Dir()

Set WshShell = CreateObject("Wscript.Shell")

Dir = WshShell.CurrentDirectory
	
End Function

Function print (prompt,title)
Set WshShell = CreateObject("Wscript.Shell")
WshShell.Popup prompt &title,1,""
End Function

關(guān)于怎么在VBS中遍歷Excel工作表就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI