【问题标题】:How to have an input box pop up in VBA only if certain criteria is met?仅当满足某些条件时,如何在 VBA 中弹出输入框?
【发布时间】:2019-08-14 16:30:40
【问题描述】:

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

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,都会弹出输入框。另外,如何定义用户答案的​​输入位置?

【问题讨论】:

    标签: vba if-statement inputbox


    【解决方案1】:

    您应该能够使用 Do While 循环来完成此操作,该循环将一直持续到用户输入值或按下取消。此外,您可以使用 .Value 属性将他们的答案放在您需要的地方。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多