使用VBA在excel中动态打开输入框

时间:2017-07-07 06:31:37

标签: excel vba excel-vba

我有一张excel表和一个特定区域。这个区域可以 范围(" A1:A5")例如只需接受 0和1.它必须自动打开一个Inputbox / em>当用户输入0时(仅点击按钮或类似的东西)。当然在那一刻。 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address.Value = 0 Then 'open InputBox End If End Sub

有这个,但这不起作用。

谢谢大家。

1 个答案:

答案 0 :(得分:0)

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim valueEnter As Long, lastRow As Long
    Set rng = Range("A1:A5") 'specify your range here
    lastRow = Cells(Rows.Count, "B").End(xlUp).Row
    If Intersect(Target, rng) Is Nothing Or Target.Value <> 0 Then Exit Sub
    valueEnter = InputBox("Gebe die ein:", "Eingabe")
    If valueEnter >= 0 Then
        Range("B" & lastRow + 1) = valueEnter
    End If
End Sub

修改:___________________________________________________________

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Dim rng As Range
    Dim valueEnter As Variant, lastRow As Long
    Set rng = Range("A1:A5") 'specify your range here
    lastRow = Cells(Rows.Count, "B").End(xlUp).Row
    If Intersect(Target, rng) Is Nothing Or Target.Value <> 0 Then GoTo Jump
    valueEnter = InputBox("Gebe die ein:", "Eingabe", 1)
    If IsNumeric(valueEnter) Then
        If valueEnter > 0 Then
            Range("B" & lastRow + 1) = CInt(valueEnter)
            Target.Value = ""
        End If
    Else
        Target.Value = ""
    End If
Jump:
    Application.EnableEvents = True
End Sub