'复制以下脚本到Form1中,添加一个Command1 按钮 '执行前,请确保INI文件为"公式文件"(全路径),Section为"公式Section",Key为: "公式Key" '即类似: '[公式Section] '公式Key=A=A+1Option Explicit Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long Private 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)) = 0 End Function'This function is used to get a key value Private 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 If End 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
'执行前,请确保INI文件为"公式文件"(全路径),Section为"公式Section",Key为: "公式Key"
'即类似:
'[公式Section]
'公式Key=A=A+1Option Explicit
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long
Private 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)) = 0
End Function'This function is used to get a key value
Private 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 If
End 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
公式是这样的:
stra=123
stra1=456reg=Hex(Int((stra * 100 - 64) / 2 + 4 / 2 + 5)) & "-" & Hex(Int((stra1 * 100 - 64) / 3 + 4 / 2 + 1))请赐教!
ExecuteLine "Dim stra1 As Long"
ExecuteLine "Dim reg As String"
ExecuteLine "Dim reg1 As String"
ExecuteLine "Dim reg2 As String"ExecuteLine "stra = 123"
ExecuteLine "stra1 = 456"
ExecuteLine "reg1 = Hex(Int((stra * 100 - 64) / 2 + 4 / 2 + 5))"
ExecuteLine "reg1 = ""&H"" & reg1"
ExecuteLine "reg2 = Hex (Int((stra1 * 100 - 64) / 3 + 4 / 2 + 1))"
ExecuteLine "reg2 = ""&H"" & reg2"
ExecuteLine "reg = reg1 - reg2"
ExecuteLine "Debug.Print reg"
ExecuteLine "Dim stra1 As Long"
ExecuteLine "Dim reg As String"
ExecuteLine "Dim reg1 As String"
ExecuteLine "Dim reg2 As String"ExecuteLine "stra = 123"
ExecuteLine "stra1 = 456"
ExecuteLine "reg1 = Hex(Int((stra * 100 - 64) / 2 + 4 / 2 + 5))"
'ExecuteLine "reg1 = ""&H"" & reg1"
ExecuteLine "reg2 = Hex (Int((stra1 * 100 - 64) / 3 + 4 / 2 + 1))"
'ExecuteLine "reg2 = ""&H"" & reg2"
ExecuteLine "reg = reg1 & ""-"" & reg2"
ExecuteLine "Debug.Print reg"
reg = Hex(Int((stra * 100 - 64) / 2 + 4 / 2 + 5)) & "" - "" & Hex(Int((stra1 * 100 - 64) / 3 + 4 / 2 + 1))即:
ExecuteLine "reg = Hex(Int((stra * 100 - 64) / 2 + 4 / 2 + 5)) & "" - "" & Hex(Int((stra1 * 100 - 64) / 3 + 4 / 2 + 1))"