VBScript例子程序

来源:互联网 发布:js两数求和 编辑:程序博客网 时间:2024/05/16 17:24

VBS基本语法介绍

W3CSchool

工作中遇到的问题,项目组长给了一个Excel,让分析其中包含的字符含义,由于每页条数太多,导致看不出来要分析的字符在一长串字符中的位置。于是想把其中要注意的字符标注颜色,网上找资料,发现Excel中本身没有这个功能,于是下了下面的代码进行处理,顺便学习下VBS的基本语法格式。
未执行脚本前三个sheet的样子如下:

sheet1
sheet2
sheet3

执行脚本后三个sheet样子如下:

sheet1_f
sheet2_f
sheet3_f

最后上脚本代码如下:

Sub Macro1()Dim loopNum As IntegerFor Each wsh In Worksheets'找出sheet名称并提取相应的字符串findStringArray = Split(wsh.Name, "_")findString = findStringArray(0)LengthString = Len(findString)'遍历所有sheet中的所有的表格TotalRows = wsh.UsedRange.Rows.Count    For loopNum = 1 To TotalRows    vlaueInCell = wsh.Cells(loopNum, 1).Value    posstart = InStr(LCase(vlaueInCell), LCase(findString))    If posstart > 0 Then            With wsh.Cells(loopNum, 1).Characters(Start:=posstart, Length:=LengthString).Font            .Color = -16777024            '.Color = 0            End With        End If    NextNextEnd Sub
0 0
原创粉丝点击