> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Check if a User Clicked [Cancel] on an InputBox in VBA
- URL: https://nolongerset.com/user-clicked-cancel-on-an-inputbox/
- Published: 2021-08-31T01:17:16.000Z
- Updated: 2026-05-08T13:02:39.000Z
- Description: It's possible to distinguish between a user clicking [OK] on an empty box or clicking [Cancel]. You just need to use this simple trick.
- Author: Mike Wolfe
- Tags: Hidden Features, #Import 2026-05-20 02:59

Did you know that it's possible to distinguish between a user clicking "OK" on an [InputBox](https://docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/inputbox-function) versus "Cancel", even if they did not enter any text?

Consider the following sample code:

```vba
Sub InputBoxTest()
    Dim Result As String
    
    Result = InputBox("Leave this box blank")
    If StrPtr(Result) = 0 Then
        Debug.Print "User clicked [Cancel]"
    ElseIf Len(Result) = 0 Then
        Debug.Print "User clicked [OK]"
    Else
        Debug.Print "User can't follow instructions"
    End If
    
End Sub
```

Here is the test code in action:

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/08/InputBox.gif)

If you're interested in why this works, I recommend the following question on stackoverflow: [What are the benefits and risks of using StrPtr in VBA?](https://stackoverflow.com/q/42015700/154439) Both top-voted answers (from users [Comintern](https://stackoverflow.com/users/4088852/comintern) and [GSerg](https://stackoverflow.com/users/11683/gserg)) are worth reading for background knowledge. 

---

### External references

[InputBox function (Visual Basic for Applications)Microsoft Docso365devx](https://docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/inputbox-function)

[What are the benefits and risks of using the StrPtr function in VBA?While looking for a way to test when a user cancels an InputBox, I stumbled across the StrPtr function. I believe it checks if a variable was ever assigned a value and returns zero if it was never![](https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a)Stack OverflowChrisB![](https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon@2.png?v=73d79a89bded)](https://stackoverflow.com/q/42015700/154439)

*Image by [Ron van den Berg](https://pixabay.com/users/ronberg-3029970/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1593406) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1593406)*