首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在VBA中只有在满足特定条件时才弹出输入框?

如何在VBA中只有在满足特定条件时才弹出输入框?
EN

Stack Overflow用户
提问于 2019-08-15 00:30:41
回答 1查看 199关注 0票数 0

我正在尝试设置一个输入框,如果某个单元格(I4)中的值大于0,就会弹出输入框。然后我想这样做,这样如果没有在单元格中输入任何内容,它将提示用户输入信息。如果用户按下cancel,则宏应该停止。

代码语言:javascript
复制
Sub testing123()

Re_Enter_FileImport:
FileImport = InputBox("File Not Yet Imported, Please Provide Reasoning")

If Range("I4") <> 0 Then
    Answer = FileImport

 'Code if user pressed cancel

If StrPtr(FileImport) = 0 Then
    Exit Sub

ElseIf FileImport = "" Then
    MsgBox "Please Provide Reasoning"
    GoTo Re_Enter_FileImport:
Else
End If

End Sub

当我尝试此代码时,无论单元格I4中的信息是否为0,都会弹出输入框。另外,我如何定义输入用户答案的位置?

EN

回答 1

Stack Overflow用户

发布于 2019-08-15 05:15:02

您应该能够通过Do While循环来实现这一点,该循环将一直运行,直到用户输入值或按下cancel。此外,您还可以使用.Value属性将它们的答案放在需要的地方。

代码语言:javascript
复制
Sub testing123()

Dim FileImport As String
Dim AnsBool As Boolean

AnsBool = True

If Range("A4") > 0 Then

    FileImport = InputBox("File Not Yet Imported, Please Provide Reasoning")

    Do While AnsBool
        If StrPtr(FileImport) = 0 Then Exit Sub
        If FileImport <> "" Then Exit Do
        MsgBox "Please Provide Reasoning"
        FileImport = InputBox("File Not Yet Imported, Please Provide Reasoning")
    Loop
End If

'You can change this to the range needed
Range("A1").Value = FileImport
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57498694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档