Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ezhov-da/888c4ae413728345b3991f5494f4cb81 to your computer and use it in GitHub Desktop.
Save ezhov-da/888c4ae413728345b3991f5494f4cb81 to your computer and use it in GitHub Desktop.
vba выполнение кода из строки
'Надо в VBA с помощью меню Tools/References подключить библиотеку "Microsoft Script Control 1.0", а затем сделать следующее:
Sub ScripCode()
Dim first As Integer
Dim second As Integer
first = 12
second = 17
Dim resultUsual As Integer
Dim resultFromCode As Integer
resultUsual = (first + first / second * second - first * first) / second
Debug.Print resultUsual
Dim sс As New MSScriptControl.ScriptControl
sс.Language = "VBScript" ' Инициализация
Dim text As String
text = "(:first + :first / :second * :second - :first * :first) / :second"
text = Replace(text, ":first", first)
text = Replace(text, ":second", second)
resultFromCode = sс.Eval(text)
Set sс = Nothing ' Деинициализация
Debug.Print resultFromCode
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment