Excel VBA Video Training/ EXCEL DASHBOARD REPORTS

Ozgrid, Experts in Microsoft Excel Spreadsheets

Inputbox in Excel VBA

FREE EXCEL & EXCEL VBA TRAINING COURSES > Trading Add-ins For excel > Convert Databases

 

InputBox Function

As you may, or may not know, there are multiple types we can use. The code below will show you some of these types. We will kick off we the most common use of an INPUTBOX, collect text

Sub StandardInputBox()
Dim strName As String
    
    'InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])
    strName = InputBox("Enter you name.", "NAME COLLECTOR")
    'Exit sub if Cancel button used or no text entered
    If strName = vbNullString Then Exit Sub
    
    MsgBox "Hello " & strName

End Sub

Now we can look at how we can use Application.InputBox with different types.

Type Meaning
0 A formula
1 A number
2 Text (a string)
4 A logical value (True or False)
8 A cell reference, as a Range object
16 An error value, such as #N/A
64 An array of values
Sub NumbersOnly()
Dim dNum As Long

    'expression.InputBox(Prompt, Title, Default, Left, Top, HelpFile, HelpContextID, Type)
    dNum = Application.InputBox("Enter you age.", "AGE COLLECTOR", , , , , , 1)
    'Exit sub if Cancel button used
    If dNum = 0 Then Exit Sub
    
    'As we have used Type 1 Inputbox, Excel will handle any text entered
    MsgBox "You are " & dNum & " Years old."

End Sub


Sub RangeAsObject()
Dim rRange As Range
Dim lReply As Long

'We use On Error so we ignore run time errors
On Error Resume Next
  'expression.InputBox(Prompt, Title, Default, Left, Top, HelpFile, HelpContextID, Type)
Set rRange = Application.InputBox("With you mouse, select a range of cells.", _
    "RANGE COLLECTOR", , , , , , 8)
On Error GoTo 0

    'Check if range is valid
    If rRange Is Nothing Then
        'If range not valid, ask if they wish to retry
        rRange = MsgBox("Non valid range. Try again?", vbOKCancel + vbQuestion)
            If rRange = vbCancel Then 'No retry
                Exit Sub
            Else 'retry
                Run "RangeAsObject"
            End If
    Else
       MsgBox rRange.Address
    End If
End Sub

Now let's see how we can the INPUTBOX to evaluate a chosen range with an expression collected from a user.

Sub CollectExpressions()
Dim strExpression As String
Dim rRange As Range

    On Error Resume Next
    Set rRange = Application.InputBox("With you mouse, select a range of cells to evaluate.", _
        "RANGE COLLECTOR", , , , , , 8)
    On Error GoTo 0

        If rRange Is Nothing Then
            rRange = MsgBox("Non valid range. Try again?", vbOKCancel + vbQuestion)
                If rRange = vbCancel Then 'No retry
                    Exit Sub
                Else 'retry
                    Run "CollectExpressions"
                End If
        End If
        
    strExpression = InputBox("Enter you expession.E.g. >5 or <10", "EXPRESSION COLLECTOR")
    If strExpression = vbNullString Then Exit Sub

    MsgBox "Using your expression." & vbNewLine _
    & "COUNTIF = " & WorksheetFunction.CountIf(rRange, strExpression) & vbNewLine _
    & "Top left cell = " & Evaluate(rRange(1, 1) & strExpression) & vbNewLine
    
    
End Sub

Hopefully, you find some uses for different INPUTBOX types.

Excel Dashboard Reports & Excel Dashboard Charts 50% Off Become an ExcelUser Affiliate & Earn Money

Special! Free Choice of Complete Excel Training Course OR Excel Add-ins Collection on all purchases totaling over $64.00. ALL purchases totaling over $150.00 gets you BOTH! Purchases MUST be made via this site. Send payment proof to [email protected] 31 days after purchase date.



Instant Download and Money Back Guarantee on Most Software

Excel VBA Video Training/ EXCEL DASHBOARD REPORTS

Excel Trader Package Technical Analysis in Excel With $139.00 of FREE software!

Microsoft � and Microsoft Excel � are registered trademarks of Microsoft Corporation. OzGrid is in no way associated with Microsoft

Some of our more popular products are below...
Convert Excel Spreadsheets To Webpages | Trading In Excel | Construction Estimators | Finance Templates & Add-ins Bundle | Code-VBA | Smart-VBA | Print-VBA | Excel Data Manipulation & Analysis | Convert MS Office Applications To...... | Analyzer Excel | Downloader Excel | MSSQL Migration Toolkit | Monte Carlo Add-in | Excel Costing Templates