Finding tricks to make a Standard InputBox/MsgBox Modeless.

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,589
Office Version
  1. 2016
Platform
  1. Windows
By modeless , I mean two things:

1-Allowing to work with excel while the InputBox/Msgbox are displayed and
2-Allowing execution of subsequent code

In other words just like a Modeless UserForm.

Any ideas are welcome.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Following is an example of a Standard InputBox that allows a cell to flash while the InputBox is on display.

The actual code runs inside an API timer procedure which runs asynchroneously.

I have wrapped the modeless InputBox in the function: AsynInputBox.
This custom AsynInputBox function has the same signature as that of the standard vba InputBox for easy use.

Plug this into a standard module , run the Test Macro and see the flashing of Cell A1:

Code:
Option Explicit

Private Declare Function SetTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
 
Private Declare Function KillTimer Lib "user32" ( _
ByVal hwnd As Long, _
ByVal uIDEvent As Long) As Long


Private Function AsynInputBox _
(Prompt As String, _
Optional Title As String, _
Optional Default As String, _
Optional XPos As Variant, _
Optional YPos As Variant, _
Optional HelpFile As String, _
Optional Context As Long) As String

    SetTimer Application.hwnd, 0, 1000, _
    AddressOf AsynChroneousProcedure
    
    AsynInputBox = InputBox _
    (Prompt, Title, Default, XPos, YPos, HelpFile, Context)
    
    KillTimer Application.hwnd, 0

End Function


Private Sub AsynChroneousProcedure( _
ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long)

    With Range("a1").Interior
        If Int(lpTimerFunc / 1000) Mod 2 = 0 Then
            .ColorIndex = 3 'red
        Else
            .ColorIndex = xlColorIndexNone
        End If
    End With

End Sub

Sub Test()

    Range("a1") = AsynInputBox("Enter a value in the flashing Cell  'A1'", _
    "AsynChronous Standard InputBox Demo.")
    
    Range("a1").Interior.ColorIndex = xlColorIndexNone
    
End Sub
the code inside the AsynChroneousProcedure Procedure could be changed to do anything not just flashing a cell. (carefull editing is required though as any unhadled error could crash the application !)

Now,if only we could make it possible to work with the worksheet while the InputBox is displayed it would be great.
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,284
Members
448,885
Latest member
LokiSonic

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top