Reputation: 3
I recently created a VBA userform with various ActiveX controls, and I'm having trouble with the following:
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
Upvotes: 0
Views: 2543
Reputation: 7735
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
Upvotes: 0
Reputation: 50064
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.
Upvotes: 1