Singapore Expats

Executing string as command in VBA/Excel

Discuss about computers & Internet. Including mobile phones, home appliances & other gadgets. Read about Windows security risks or virus updates.
Post Reply
VBA inquirer

Executing string as command in VBA/Excel

Post by VBA inquirer » Tue, 02 Aug 2005 6:35 am

What is the command to convert a string into a command?
Preferably in the excel's cell. Thanks in advance.

User avatar
Yutenji
Chatter
Chatter
Posts: 180
Joined: Tue, 15 Jun 2004 5:50 pm
Location: Singapore

Re: Executing string as command in VBA/Excel

Post by Yutenji » Thu, 04 Aug 2005 10:42 am

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

Post Reply

Return to “Computer, Internet, Phone & Electronics”

Who is online

Users browsing this forum: No registered users and 6 guests