VBS遍历Excel工作表的实现代码
导读:收集整理的这篇文章主要介绍了VBS遍历Excel工作表的实现代码,觉得挺不错的,现在分享给大家,也给大家做个参考。 核心代码'******************************...
收集整理的这篇文章主要介绍了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")="成功" nextExcel遍历所有工作簿中所有工作表执行宏
Sub test()n = Worksheets.CountFor i = 1 To nWorksheets(i).ActivateMacro1NextEnd Sub
macro1是宏的名称
使用vbS遍历EXCEL
Dim xlApp,xlSheet,xlWorkBookDim irowcount,iLoop,jLoop,jColumnCount,numAddSet xlApp=CreateObject("Excel.Application")xlApp.Visible=TrueSet xlWorkBook=xlApp.Workbooks.Open("C:\data.xls")Set xlSheet=xlWorkBook.Sheets("Sheet1")iRowCount=xlSheet.UsedRange.Rows.CountjColumnCount=xlSheet.UsedRange.Columns.CountFor iLoop=1 To iRowCount For jLoop=1 To jColumnCount MsgBox(xlSheet.cells(iLoop,jLoop).value) NextNextxlWorkBook.SavexlWorkBook.ClosexlApp.QuITVBScript 编写 自动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 iDim a(150)For i = 5 To 145 '145-5+1 = 141 a(i-5) = oSheet.Range("B"&
i).ValuePRint "data=",a(i-5)nextSet oSheet = Nothing oExcel.Workbooks.CloseoExcel.Quit '关闭excel.exe'Function Dir()Set WshShell = CreateObject("Wscript.Shell")Dir = WshShell.currentDirectory End FunctionFunction print (prompt,title)Set WshShell = CreateObject("Wscript.Shell")WshShell.Popup prompt &
title,1,""End Function为了方便学习特将代码打包提供下载 下载地址
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: VBS遍历Excel工作表的实现代码
本文地址: https://pptw.com/jishu/610175.html
