在excel中增加单元格行

时间:2013-03-13 02:02:45

标签: excel vba excel-vba userform

我有一个UserForm会将字段中的数据提交到A,B和C列,但每次用户点击提交时我需要向下移动并填写下一个空行。

这是我到目前为止所做的,我不知道我会把它做什么,所以它会从A2 / B2 / C2转到A3 / B3 / C3等。

Private Sub Submit_Click()
   Dim LastRow As Object

   Set LastRow = Sheet1.Range("a65536").End(xlUp)

   Sheet1.Range("A2").Value = MatchNum.Text
   Sheet1.Range("B2").Value = TeamNum.Text
   Sheet1.Range("C2").Value = AllianceTeamNum.Text

   MsgBox "One record written to Sheet1"

End Sub

我是Visual Basic的初学者(大约1小时的经验),如果解决方案尽可能简单,那就太好了。任何帮助将非常感激!

2 个答案:

答案 0 :(得分:3)

尝试以下代码:

Private Sub Submit_Click()
    With Sheet1
        .Select

        Dim LastRow As Long
        LastRow = .Range("A65536").End(xlUp).Row + 1

        .Range("A" & LastRow).Value = MatchNum.Text
        .Range("B" & LastRow).Value = TeamNum.Text
        .Range("C" & LastRow).Value = AllianceTeamNum.Text

    End With
    MsgBox "One record written to Sheet1"

End Sub

答案 1 :(得分:-1)

试试这个:

Dim start As Integer

Private Sub CommandButton1_Click()

   Dim LastRow As Object

   Set LastRow = Sheet1.Range("a65536").End(xlUp)

   Sheet1.Range("A" & start).Value = "a"
   Sheet1.Range("B" & start).Value = "b"
   Sheet1.Range("C" & start).Value = "c"

   MsgBox "One record written to Sheet1"

   start = start + 1

End Sub

Private Sub UserForm_Initialize()
   start = 2
End Sub