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”)遇到错误。请选择有人可以帮助我更正
答案 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以下评论的检查