如何使用 For 语句将列表框中选择的项目分配到每个单元格
How to assign items selected in listbox into each cell with For statement
我将使用列表框来允许用户进行多选。但是,我不知道将它们选择的项目放入列中的每个单元格中。这是我的代码。我陷入了 For 语句,因为我知道它 运行 通过内循环,然后通过外循环。任何人都可以提供帮助...谢谢
Private Sub lbaspectc_AfterUpdate()
For Each selected In Me.listbox.ItemsSelected
For Each c3 In Worksheets("maintenance").Range(Cells(2, 7), Cells(, 7)) 'Here i would like to start filling in from cell G2. I am not sure if the syntax is correct'
c3.Value = selected.Value
Next
Next
End Sub
你是这个意思吗?您需要重新分配给正确的控件。
Private Sub CommandButton1_Click()
Dim i As Long, j As Long
j = 2
With Me.ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
Worksheets("maintenance").Cells(j, 7).Value = .List(i)
j = j + 1
End If
Next i
End With
End Sub
我将使用列表框来允许用户进行多选。但是,我不知道将它们选择的项目放入列中的每个单元格中。这是我的代码。我陷入了 For 语句,因为我知道它 运行 通过内循环,然后通过外循环。任何人都可以提供帮助...谢谢
Private Sub lbaspectc_AfterUpdate()
For Each selected In Me.listbox.ItemsSelected
For Each c3 In Worksheets("maintenance").Range(Cells(2, 7), Cells(, 7)) 'Here i would like to start filling in from cell G2. I am not sure if the syntax is correct'
c3.Value = selected.Value
Next
Next
End Sub
你是这个意思吗?您需要重新分配给正确的控件。
Private Sub CommandButton1_Click()
Dim i As Long, j As Long
j = 2
With Me.ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
Worksheets("maintenance").Cells(j, 7).Value = .List(i)
j = j + 1
End If
Next i
End With
End Sub