Page 1 of 1

Executing string as command in VBA/Excel

Posted: Tue, 02 Aug 2005 6:35 am
by VBA inquirer
What is the command to convert a string into a command?
Preferably in the excel's cell. Thanks in advance.

Re: Executing string as command in VBA/Excel

Posted: Thu, 04 Aug 2005 10:42 am
by Yutenji
VBA inquirer wrote:What is the command to convert a string into a command? Preferably in the excel's cell.
Hi,

Cut and paste the following code into an Excel VBA module.
Put the command you want to run into Cell B2.
Run the subroutine, and voila!

Code: Select all

Option Explicit

Private Declare Function ShellExecute Lib _
     "shell32.dll" Alias "ShellExecuteA" _
     (ByVal hwnd As Long, ByVal lpOperation _
     As String, ByVal lpFile As String, ByVal _
     lpParameters As String, ByVal lpDirectory _
     As String, ByVal nShowCmd As Long) As Long
     
Const SW_HIDE = 0
Const SW_SHOWNORMAL = 1
Const SW_NORMAL = 1
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMAXIMIZED = 3
Const SW_MAXIMIZE = 3
Const SW_SHOWNOACTIVATE = 4
Const SW_SHOW = 5
Const SW_MINIMIZE = 6
Const SW_SHOWMINNOACTIVE = 7
Const SW_SHOWNA = 8
Const SW_RESTORE = 9
Const SW_SHOWDEFAULT = 10
Const SW_MAX = 10

Sub RunACommand()
Const CellContainingTheCommand = "B2"
Dim TheCommandToRun As String

    Range(CellContainingTheCommand).Select
    TheCommandToRun = ActiveCell.Text
    ShellExecute 0&, vbNullString, TheCommandToRun, _
                       vbNullString, vbNullString, SW_SHOWDEFAULT

End Sub
Good luck