接受用户输入

时间:2020-07-08 09:20:04

标签: excel vba

    Private Sub CommandButton1_Click()

a = InputBox("Enter the Row", "Excel VBA Says", "Please enter data")
    Rows("a").Select
    Selection.Copy
    Selection.Insert Shift:=xlDown
    Range("L8").Select
    Application.CutCopyMode = False
    Selection.ClearContents
    Rows("a" + 1).Select
    Selection.Rows.Group
    ActiveSheet.Outline.ShowLevels RowLevels:=1
    
End Sub

我在Rows(“ a”)遇到错误。请选择有人可以帮助我更正

1 个答案:

答案 0 :(得分:0)

参考已删除的答案,您应该始终将InputBox的变量声明为String。然后,您可以执行验证。就您而言,您只需执行以下操作即可:

  Dim a As String
  a = InputBox("Enter the Row", "Excel VBA Says", "Please enter data")
  If Not IsNumeric(a) Or Val(a) <= 0 Then
    MsgBox "Please enter a valid row number"
  Else
    Rows(a).Select
        Selection.Copy
        Selection.Insert Shift:=xlDown
        Range("L" & a).ClearContents
        Rows(a + 1).Select
        Selection.Rows.Group
        ActiveSheet.Outline.ShowLevels RowLevels:=1
  End If

编辑:添加了对a = 0以下评论的检查

相关问题