NEW UPDATE-Using a Collection to Map Destinations of Userform Controls
Using a collection to reference userform controls and the location to input
I created a collection, as shown below to reference the position to print the userform controls, to the sheet. I tested this on a sheet "Sheet1" with the controls being "subTaskID" and "TextBoxsubtask".
I get a
"ByRef argument type mismatch"
on the ctlMap variable. I tried to follow PeterT's explanation below.
'' COLLECTION START DELETE FROM HERE IF WRONG
Sub Userform_Intialize()
Set ctlMap = New Collection
With ctlMap
.Add item = "Sheet1!B1", key:="SubTaskID"
.Add item = "Sheet!B2", key:="TextBoxsubtask"
End With
End Sub
'Macro to populate sheet
Private Sub CommandButton1_Click()
Dim ctl As Variant
For Each ctl In Me.Controls
If ControlExists(ctlMap, ctl.Name) Then
Range(ctlMap(ctl.Name)) = ctl.Value
End If
Next ctl
End Sub
Original QUESTION
I have a userform to populate an Excel file regarding project tasks.
I created code linking each control to the correct column and cell number. I would like to be more efficient in my code to speed things up. I do not want labels, and certain textbox values to be inputted and also if a control is blank to skip to the next.
I can't order the loop through the controls correctly as it does not print in the desired cells.
How do I rearrange this order? Also how to I prevent it from printing false for an empty value?
I created an array with the required control names, however I get an error
same object required when running.
lastrowST = subtaskws.range("A" & Rows.Count).End(xlUp).row
Dim Ctrl As Control, col As Long, range As range, userformorder As Variant
With AddTask
userformorder = Array(.SubTaskID, .TextBoxsubtask, .ComboBoxDeliverableFormat, .TextBoxcheckedcomplete, .TextBoxformat, .TextBoxacceptancecriteria, .BudgetWorkloadTextBox, .AWLTextBox, .ComboBoxOwner, .TextBoxTDSNumber, .TextBoxMilestone, .TextBoxTargetDeliveryDate, .ComboBoxW, .ComboBoxI, .ComboBoxe, .TextBoxP, .TextBoxLevel, .TextBoxInputQuality, .TextBoxNewInput, .TextBoxDelay, .TextBoxInternalVV, .TextBoxReviewer, .TextBoxDelivered, .ComboBoxNumIterations, .ComboBoxAcceptance, .ComboBoxProgress, .ComboBoxStatus, .ComboBoxFlowChart, .TextBoxActivitySheet, .TextBoxEvidenceofDelivery, .TextBoxComments)
For Each range In subtaskws.range("A" & lastrowST + 1 & ":AE" & lastrowST + 1 & "")
For Each Ctrl.Value In userformorder
If Ctrl.Value <> "" Then
range.Value = Ctrl.Value
Else
End If
Next Ctrl
Next range
End With