Frage

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.

War es hilfreich?

Lösung

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

Andere Tipps

Try this:

Dim a As Variant
a = Application.InputBox(Prompt:="Please enter name", Title:="Sample name")
If a <> False Then
    ActiveCell.Value = a
End If
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top