2017-03-01 82 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