当前位置: 移动技术网 > IT编程>脚本编程>VBScript > VBS遍历Excel工作表的实现代码

VBS遍历Excel工作表的实现代码

2020年03月09日  | 移动技术网IT编程  | 我要评论

梧州网站建设,正月十五组合星光大道,4008-197-197

核心代码

'******************************************
'拖拽文件,获取文件路径
'******************************************
if wscript.arguments.count=0 then 
		msgbox "拖拽文件到本图标",0,"提示"
end if 
 
 
for a=0 to wscript.arguments.count-1
 
	strpath=wscript.arguments(a)	
	
next
'******************************************
'定义excle对象、工作薄对象、工作表对象
'******************************************
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遍历所有工作簿中所有工作表执行宏

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 编写 自动excel文件内容到数组并提示输出

解压到任意目录,点击vbs文件执行,程序自动读取文件所在目录的excel文件到数组中,并通过提示框逐个输出,提示框1s自动关闭。

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 '关闭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

为了方便学习特将代码打包提供下载 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网