Sub DataInputBox()
Dim Id As Integer
Dim Name As String
Dim gender As String
Id = InputBox("Enter in your id")
Name = InputBox("Enter in your Name")
gender = InputBox("Enter in your gender")
Sheets(2).Range("a1").End(xlDown).Offset(1, 0).Value = Id
Sheets(2).Range("a1").End(xlDown).Offset(1, 1).Value = Name
Sheets(2).Range("a1").End(xlDown).Offset(1, 2).Value = gender
End Sub
在excel中执行时,上面的代码出现运行时错误。
错误: - 运行时错误'1004' 应用程序定义或对象定义的错误
答案 0 :(得分:3)
试一试,
Sheets(2).Range("a" & rows.count).End(xlUP).Offset(1, 0).Value = Id
Sheets(2).Range("a" & rows.count).End(xlUP).Offset(0, 1).Value = Name
Sheets(2).Range("a" & rows.count).End(xlUP).Offset(0, 2).Value = gender
答案 1 :(得分:1)
答案 2 :(得分:0)
尝试以下
Sheets(2).Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1).Value = ID
Sheets(2).Range("B" & Range("A" & Rows.Count).End(xlUp).Row).Value = Name
Sheets(2).Range("C" & Range("A" & Rows.Count).End(xlUp).Row).Value = gender
答案 3 :(得分:0)
只是为了获得乐趣的第四个解决方案:
Sub DataInputBox()
Dim Id As Long
Dim xName As String
Dim gender As String
Id = InputBox("Enter in your id")
xName = InputBox("Enter in your Name")
gender = InputBox("Enter in your gender")
Sheets(2).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, 3).Value = Array(ID, xName, gender)
End Sub
但是,最好不要将Name
声明为变量,否则VBA可能会搞砸,因为每个对象都将其作为属性。 ;)