以下是引用wangtx218在2008-7-15 20:58:01的发言:好,最好再给一个实用例子! 看以下实例,可以执行VBA语句的函数,可以在工作表中写入VBA代码,用函数调用执行.
J42fDRRG.rar
(10.57 KB, 下载次数: 3193)
模块中的代码 Public myc, mycc Function pengs(a) On Error GoTo ren pengs = "运行完毕" mycc = "sub xi()" & Chr(10) arr = a.Resize(a.Rows.Count, 2) For i = 1 To UBound(arr) If arr(i, 1) Like "range*" Or arr(i, 1) Like "Range*" Then arr(i, 1) = "ActiveSheet." & arr(i, 1) mycc = mycc & arr(i, 1) & Chr(10) Next i mycc = mycc & "end sub" Set myc = New css Set myc.sht = ActiveSheet Exit Function ren: mycc = "sub xi()" & Chr(10) & a & Chr(10) & "end sub" Set myc = New css Set myc.sht = ActiveSheet End Function 类模块css代码 Public WithEvents sht As Worksheet Private Sub sht_Change(ByVal Target As Range) On Error GoTo ren Set myc.sht = Nothing Set myc = Nothing Set s = CreateObject("MSScriptControl.ScriptControl") s.Language = "VBScript" s.AddObject "ActiveWorkbook", ActiveWorkbook s.AddObject "Application", Application s.AddObject "Activesheet", ActiveSheet s.AddObject "sheets", Sheets s.AddObject "cells", Cells s.addcode mycc s.Run "xi" s.Reset ren: End Sub |