【发布时间】:2020-01-07 18:03:53
【问题描述】:
新更新 - 使用集合映射用户窗体控件的目标
使用集合来引用用户窗体控件和输入位置
我创建了一个集合,如下所示,以将打印用户窗体控件的位置引用到工作表。我在工作表“Sheet1”上对此进行了测试,控件为“subTaskID”和“TextBoxsubtask”。
我得到一个
"ByRef 参数类型不匹配"
在 ctlMap 变量上。我试图按照下面 PeterT 的解释进行操作。
'' 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
原始问题
我有一个用户表单来填充有关项目任务的 Excel 文件。
我创建了将每个控件链接到正确列和单元格编号的代码。我想提高我的代码效率以加快速度。我不希望输入标签和某些文本框值,并且如果控件为空白则跳到下一个。
我无法通过控件正确排序循环,因为它没有打印在所需的单元格中。
如何重新排列此订单?还有如何防止它为空值打印 false?
我创建了一个包含所需控件名称的数组,但出现错误
运行时需要相同的对象。
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
【问题讨论】:
-
看起来你的循环是错误的。
col应该做什么? -
col 只是列计数器,所以移动列。现在是多余的
-
我已经更新了 wuestion
-
您的数组是否包含控件名称?
-
对,就是数组中的名字