下面我在userform中有一个命令按钮的代码。我有两个列表框(lb1和lb2)和几个文本框(t1,t2,t3,t4)。列表框中的项目是工作簿中每个工作表的名称。如果在lb1中选择了项目,则项目将转移到lb2。
然后按钮应该放置一个输出(t3)并使用t4中给出的单元格范围和lb2中选择的工作表名称来循环选择的工作表并放置输出。很抱歉,如果这令人困惑,但代码有望澄清。
Private Sub c4_Click()
Dim CellName As Variant
Dim n As Integer
Dim i As Integer
Dim lb1 As ListBox
Dim sht As Worksheet
CellName = t4.Value
'## If this item is selected, then give it a new value name
For n = 0 To Me.lb1.ListCount - 1
If Me.lb1.Selected(n) = True Then
Me.lb2.AddItem Me.lb1.Selected(n)
End If
Next n
For i = 0 To lb2.ListCount - 1
sht(Me.lb2.ListIndex).Range("(CellName)").Value = t3.Value
Next i
'Loop through selected listbox sheets to add output
End Sub