简体   繁体   中英

vba excel userform move to next empty row

I recently created a VBA userform with various ActiveX controls, and I'm having trouble with the following:

  1. Saving data from the userform to a worksheet
  2. Entering data to the next available row in the worksheet (creating multiple records)
  3. Resetting the userform for new data entry

I have a command button that uses (unsuccessfully) the following code:

Private Sub cmdSubmit_Click()
    Dim ws As Worksheet
    Dim addme As Range
    Set ws = Sheet1
    Set addme = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

    With ws
        addme.Offset.Value = Me.txtNeedsAnalysSum
        addme.Offset.Value = Me.txtSummaryOfTask
        addme.Offset.Value = Me.txtIntroduction
        addme.Offset.Value = Me.chkInRes
        addme.Offset.Value = Me.chkOnline
        addme.Offset.Value = Me.chk24Hr
        addme.Offset.Value = Me.chk3days
        addme.Offset.Value = Me.chkDurOther
        addme.Offset.Value = Me.cmbPrereqReq
        addme.Offset.Value = Me.cmbPrereqRec
    End With
End Sub

Any assistance is appreciated!

-Joe

I think you want do something like:

Private Sub cmdSubmit_Click()
    Dim ws As Worksheet
    Dim addme As Range
    Set ws = Sheet1
    Set addme = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)


        addme.Offset(,1).Value = Me.txtNeedsAnalysSum
        addme.Offset(,2).Value = Me.txtSummaryOfTask
        addme.Offset(,3).Value = Me.txtIntroduction
        addme.Offset(,4).Value = Me.chkInRes
        addme.Offset(,5).Value = Me.chkOnline
        addme.Offset(,6).Value = Me.chk24Hr
        addme.Offset(,7).Value = Me.chk3days
        addme.Offset(,8).Value = Me.chkDurOther
        addme.Offset(,9).Value = Me.cmbPrereqReq
        addme.Offset(,10).Value = Me.cmbPrereqRec

End Sub

You could, potentially, loop through the controls in your form and use a variable to track which column you are writing to:

Private Sub cmdSubmit_Click()
    Dim ws As Worksheet
    Dim addme As Range
    Set ws = Sheet1
    Set addme = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

    Dim cntrl As control
    Dim intCol as integer
    intCol = 0
    For Each cntrl in Me.Controls
        addme.offset(, intCol) = cntrl
        intCol = intCol + 1
    Next cntrl        
End Sub

That will also pick up labels and submit buttons and what have you, so YMMV.

Something like the following should do it:

Private Sub cmdSubmit_Click()
    Dim ws As Worksheet
    Dim addme As Long
    Set ws = Sheet1
    addme = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

    With ws
        ws.Cells(addme, 1).Value = Me.txtNeedsAnalysSum 'the number 1 here represents the Column A
        ws.Cells(addme, 2).Value = Me.txtSummaryOfTask 'the number 2 represents Column B
        ws.Cells(addme, 3).Value = Me.txtIntroduction
        ws.Cells(addme, 4).Value = Me.chkInRes
        ws.Cells(addme, 5).Value = Me.chkOnline
        ws.Cells(addme, 6).Value = Me.chk24Hr
        ws.Cells(addme, 7).Value = Me.chk3days
        ws.Cells(addme, 8).Value = Me.chkDurOther
        ws.Cells(addme, 9).Value = Me.cmbPrereqReq
        ws.Cells(addme, 10).Value = Me.cmbPrereqRec
    End With
        Me.txtNeedsAnalysSum = vbNullString 're-set your textboxes
        Me.txtSummaryOfTask = vbNullString
        Me.txtIntroduction = vbNullString
        Me.chkInRes = False
        Me.chkOnline = False
        Me.chk24Hr = False
        Me.chk3days = False
        Me.chkDurOther = False
        Me.cmbPrereqReq = ""
        Me.cmbPrereqRec = ""
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM