2013-03-13 82 views
0

我有一个用户窗体将从字段中提交数据到列A,B和C,但我需要它向下移动并在用户每次提交时填充下一个空行。在excel中增加单元格行

这是我到目前为止,我不知道我会投入使它所以它会从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小时的经验),如果解决方案尽可能简单,它会很好。任何帮助将非常感激!

+0

涉及改变最少当前代码的解决方案似乎以某种方式利用的是好看的'LastRow'对象;目前除了用于存储内存外,还没有其他用途。你有没有尝试将'LastRow'合并到三个范围内? – bernie 2013-03-13 02:14:34

+0

如果您不介意记录的顺序相反,则可以将记录插入顶部,将所有记录向下移动。保存制定什么行也写。可能不如将它追加到底部。 – NickSlash 2013-03-13 03:01:46

回答

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

试试这个:

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 
+0

这是如何解决问题的“空行”部分的? – 2013-03-13 04:05:10