Search code examples
vbaexcelinputbox

Skip to next step if cancel is pressed in InputBox


I am currently running a macro with the InputBox code shown below:

ActiveCell.Value = InputBox(Prompt:="Please enter the sample name", Title:="Sample name")

It works great for when I don't have any text in the ActiveCell already, but if there is text in there already, it clears the text if I hit cancel.

How can I change this so that if I hit cancel at this step, then it will just skip to the next step and not change what is in the cell already? I don't want it to end the sub, I just want it to skip to the next line in the code.

Thank you for your help.


Solution

  • This should do the job:

    Dim s as String
    s = InputBox(Prompt:="Please enter the sample name", Title:="Sample name")
    If s <>"" Then ActiveCell.Value = s