2013-03-13 83 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