关于公式调用的调用

    技术2022-05-11  173

    程序中如何实现读取ini文件中的公式,然后进行计算!

    例如A=A+1

    我把A+1存储到ini文件中!然后程序执行时到INI文件中读取公式然后按公式进行计算!——————————————————————————————

    '复制以下脚本到Form1中,添加一个Command1 按钮'执行前,请确保INI文件为"公式文件"(全路径),Section为"公式Section",Key为: "公式Key"'即类似:'[公式Section]'公式Key=A=A+1

    Option ExplicitPrivate Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As LongPrivate Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

    Public Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean    ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0End Function

    'This function is used to get a key valuePrivate Function GetItemString(ByVal sectionname As String, ByVal KeyName As String, ByVal strIniFile As String) As String    Dim IniString As String    Dim retval, Status, nullpos As Integer    IniString = String(255, " ")    retval = GetPrivateProfileString(sectionname, KeyName, "Not Used", IniString, Len(IniString), strIniFile)    IniString = Trim(IniString)    'delete the null char at the end of string    nullpos = InStr(IniString, Chr$(0))    If nullpos > 0 Then IniString = Left$(IniString, nullpos - 1)    If retval And Left(IniString, 8) <> "Not Used" Then       GetItemString = IniString    End IfEnd Function

    Private Sub Command1_Click()    Dim strFormula As String    'Get formula from Ini file    strFormula = GetItemString("公式Section", "公式Key", "公式文件")    ExecuteLine "Dim A As Long"  '定义变量    ExecuteLine strFormula  '执行公式,比如A=A+1等End Sub

    当然,执行脚本除了使用VBA.dll中的EbExecuteLine 外,你还可使用Microsoft Script Control(msscript.ocx)控件来处理,效果是相同的。不过使用前者只能在IDE环境运行...


    最新回复(0)