资源描述:
《在vba中使用javascript和vbscript》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、在VBA中使用JAVASCRIPT和VBSCRIPT(1)javascript有许多函数和功能可以弥补VBA不足,如正则,数组,类,等等1)以数组为例,用JAVASCRIPT排序Subfig8()Setx=CreateObject("msscriptcontrol.scriptcontrol")x.Language="javascript"arr=Array("aa","cc","bb","1a")kk=Join(arr,",")x.addcode"functionaa(bb){x=bb.split(',');x.sort();returnx;}"cc=x.eval("aa('"&kk&"'
2、)")MsgBoxccEndSub2)1)以数组为例,用JAVASCRIPT倒序Subfig8()Setx=CreateObject("msscriptcontrol.scriptcontrol")x.Language="javascript"arr=Array("aa","cc","bb","1a")kk=Join(arr,",")x.addcode"functionaa(bb){x=bb.split(',');x.reverse();returnx;}"cc=x.eval("aa('"&kk&"')")MsgBoxccEndSub用VBSCRIPT的简单例子Subfig8()Setx=C
3、reateObject("msscriptcontrol.scriptcontrol")x.Language="vbscript"x.addcode"subaa():msgbox""hello.."":endsub"x.Run"aa"EndSub以前需要分开好几个模块,函数,现在可以统统放在一起了。。。。。Subfig8()Setx=CreateObject("msscriptcontrol.scriptcontrol")x.Language="vbscript"x.addcode"subaa():msgbox""hello.."":endsub:subbb:msgbox3:endsub:s
4、ubcc:msgbox""cc"":endsub"x.Run"aa"x.Run"bb"x.Run"cc"EndSub自定义函数的用法Subfig8()Setx=CreateObject("msscriptcontrol.scriptcontrol")x.Language="vbscript"x.addcode"functionsum(x,y):sum=x+y:endfunction"bb=x.Run("sum",2,3)MsgBoxbbEndSub动态改变窗口,文本框,单元格,range属性,'本例改[A1:z888]单元格为红色Subfig88()SetX=CreateObject("ms
5、scriptcontrol.scriptcontrol")X.Language="vbscript"X.addcode"SUBAA:XX.INTERIOR.COLORINDEX=3:ENDSUB"X.ADDOBJECT"XX",[A1:z888]X.Run"AA"EndSub设置和调用全局变量Subfigvb()Setx=CreateObject("msscriptcontrol.scriptcontrol")x.Language="vbscript"x.addcode"publicx:subaa(bb):x=bb*100:endsub"x.Run"aa",3b=x.codeobject.x
6、MsgBoxbEndSub代码放在单元格里不再是笑话:)Subfigvbs()Setx=CreateObject("msscriptcontrol.scriptcontrol")x.Language="vbscript"[a1]="a1=3"[a2]="b1=4"[a3]="msgboxa1+b1"Fori=1To3x.executestatementCells(i,1)NextEndSub新建类可以不再需要类模块Subfigvbs()SetX=CreateObject("msscriptcontrol.scriptcontrol")X.Language="vbscript"X.ADDCOD
7、E"CLASSAA:PUBLICSUBTEST():MSGBOX""类模块"":ENDSUB:ENDCLASS"X.ADDCODE"SETYY=NEWAA"SetRR=X.EVAL("YY")RR.TESTEndSub表达式可以直接拿来运算Subaa()SetX=CreateObject("msscriptcontrol.scriptcontrol")X.Language="vbscript"DimARR(2