Home > Net >  How to speed up firing a shell command in Excel VBA?
How to speed up firing a shell command in Excel VBA?

Time:10-12

It takes more than 2 seconds to fire an external program in Excel 2021. I meant there is a big delay between the cell selection and executing the program. When I select a cell I have to wait for 2 seconds to see the program actually being run.

Sub Worksheet_SelectionChange(ByVal Target As Range)
  If InStr(1, CStr(Target.Address), ":") < 1 Then
    If Selection.Cells.Count = 1 Then
      If Selection.Column = 5 Then
        Set fso = CreateObject("Scripting.FileSystemObject")
        Path = ThisWorkbook.Path
        ChDir Path
        ChDrive fso.GetDriveName(Path)
        ProgramFilesPath = Environ("ProgramFiles")
        cmd = ProgramFilesPath & "\AutoHotkey\AutoHotkey.exe keys.ahk "
        Shell cmd, vbMinimizedNoFocus
      End If
    End If
  End If
End Sub

CodePudding user response:

Open Another Application (Shell) on Selection Change

  • The application will run on each selection of only one cell in column 5 ("E").
Option Explicit

Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Const EnvironName As String = "ProgramFiles"
    Const RightPath As String = "\AutoHotkey\AutoHotkey.exe keys.ahk"
    
    If Target.Cells.CountLarge > 1 Then Exit Sub ' multiple cells not allowed
    If Target.Column <> 5 Then Exit Sub ' only 5th column allowed
    
    Dim ShellPath As String: ShellPath = Environ(EnvironName) & RightPath
    Shell ShellPath, vbMinimizedNoFocus

End Sub

CodePudding user response:

Shell creates and loads a copy of the Windows Shell. You can make Windows faster to load by using a fast disk and making sure that you have no shell extensions loading.

To make a program load faster, load just the program, rather than loading a Windows Shell. Of course, if you just load the program, you won't have a shell to handle hotkeys: you'll have to do the I/O yourself.

You can declare and load executables in Excel the same way you declare and use DLL's

  • Related