如何使用vba excel更新数据

时间:2017-03-01 08:02:49

标签: excel vba insert-update

有人可以帮我纠正我的代码,因为我无法在userform工作中创建更新按钮。以下是我的代码:

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

1 个答案:

答案 0 :(得分: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