2017-03-01 78 views
0

有人可以幫助我更正我的代碼,因爲我無法在用戶表單工作中創建我的更新按鈕。下面是我的代碼:如何使用vba excel更新數據

Private Sub CommandButton_update_Click() 
Dim cNum As Integer 
Dim x As Integer 
Dim nextrow As Range 

cNum = 7 
Set nextrow = Sheet1.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) 
For x = 1 To cNum 
nextrow = Me.Controls("TextBox" & x).Value 
Set nextrow = nextrow.Offset(0, 1) 
Next 
MsgBox "Sent" 
'Clear Control 
cNum = 7 
For x = 1 To cNum 
For x = 1 To cNum 
nextrow = Me.Controls("TextBox" & x).Value = "" 
Set nextrow = nextrow.Offset(0, 1) 
Next 

End Sub 
+0

你的代碼是** **不可編譯的,你有2個'對於x = 1至cNum'只有1' Next' –

+0

爲了澄清,您有一個帶有7個文本框的用戶窗體,您希望將其放入Sheet1列C中,那麼您希望在不卸載用戶窗體的情況下清除這些文本框? –

回答

0

可能是你在這之後

Private Sub CommandButton_update_Click()  
    Dim cNum As Integer 
    Dim x As Integer 
    Dim nextrow As Long '<--| have nextrow store a row index, i.e. a Long value 

    cNum = 7 
    nextrow = Sheet1.cells(Sheet1.Rows.Count, 3).End(xlUp).Offset(1, 0).Row '<--| store row index of column "C" first empty cell after last not empty one 
    For x = 1 To cNum 
     Me.Controls("TextBox" & x).Value = nextrow '<--| set "current "TextBox value to nextrow 
     nextrow = nextrow + 1 '<--| update nextrow 
    Next 

    MsgBox "Sent" 

    'Clear Control 
    cNum = 7 
    For x = 1 To cNum 
     Me.Controls("TextBox" & x).Value = "" '<--| clear "current" TextBox 
    Next  
End Sub 
+0

@Janice,你通過了嗎? – user3598756

+0

@Janice,有沒有機會從你那裏得到反饋? – user3598756