阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

VBS遍历Excel工作表的方法

58次阅读
没有评论

共计 1966 个字符,预计需要花费 5 分钟才能阅读完成。

导读 这篇文章主要介绍了 VBS 遍历 Excel 工作表的实现代码, 需要的朋友可以参考下

核心代码

'******************************************' 拖拽文件,获取文件路径
'******************************************
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

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计1966字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中