문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top