Cancel button inputbox

Jurg55

New Member
Joined
Jan 31, 2024
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I need a VBA code in excel for the following:
If I use “inputbox” and I want the text in a cell, there are two buttons. Ok and cancel.
If I fill in text and click ok. The text will be filled in in the cell. Thats good. If I leave the text empty and click ok, the cell will be empty. Thats good. Now I have a problem with the cancel button. If I click cancel, the cell will be set to empty even if there is already a value in this cell. Thats not good. I would like to have a code that if I click cancel then the inputbox doesnt need to do anything and it does not need to empty the cell cell. If there is already a value in this cell and I click cancel. The value still needs to be in that cell instead of emptying.

How can control the cancel button?
How can I make this happen with a vba code?

Thank you
Jurg
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
There's a 'trick' with StrPtr which tells whether the user clicked Cancel:

VBA Code:
Public Sub Enter_Cell_Text()

    Dim inputText As String
    
    inputText = InputBox("Enter text for cell, or click Cancel to leave cell unchanged")
    If StrPtr(inputText) <> 0 Then
        Range("A1").Value = inputText
    Else
        MsgBox "User clicked Cancel, cell unchanged"
    End If
        
End Sub
 
Upvote 0
There's a 'trick' with StrPtr which tells whether the user clicked Cancel:

VBA Code:
Public Sub Enter_Cell_Text()

    Dim inputText As String
   
    inputText = InputBox("Enter text for cell, or click Cancel to leave cell unchanged")
    If StrPtr(inputText) <> 0 Then
        Range("A1").Value = inputText
    Else
        MsgBox "User clicked Cancel, cell unchanged"
    End If
       
End Sub
It worked! Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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