作者热门文章
- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章VBS遍历Excel工作表的实现代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
核心代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
'******************************************
'拖拽文件,获取文件路径
'******************************************
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遍历所有工作簿中所有工作表执行宏 。
1
2
3
4
5
6
7
|
Sub
test()
n = Worksheets.Count
For
i = 1
To
n
Worksheets(i).Activate
Macro1
Next
End
Sub
|
Macro1是宏的名称 。
使用VBS遍历EXCEL 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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自动关闭.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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
|
为了方便学习特将代码打包提供下载 下载地址 。
最后此篇关于VBS遍历Excel工作表的实现代码的文章就讲到这里了,如果你想了解更多关于VBS遍历Excel工作表的实现代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我是一名优秀的程序员,十分优秀!