我正在使用此代码将列表框中的所选项目添加到工作表中。它将选定的项目添加到工作表中,但它将其放置在列表框中的位置的工作表行中。我希望它从A1开始,然后堆叠选定的项目A2,A3等,无论列表框中的位置如何。
Private Sub CommandButton2_Click()
Dim rRange As Range
Dim lCount As Long 'Counter
On Error GoTo ErrorHandle
Set rRange = Range("A1")
With ListBox1
For lCount = 0 To .ListCount - 1
If .Selected(lCount) = True Then
rRange.Offset(lCount, 0).Value = .List(lCount)
End If
Next
End With
BeforeExit:
Set rRange = Nothing
'Unload Me
Exit Sub
ErrorHandle:
MsgBox Err.Description
Resume BeforeExit
End Sub
答案 0 :(得分:1)
未经测试的代码:
'Set rRange = Range("A1") 'Not necessary
lMyRow = 1 'Dim as long
With ListBox1
For lCount = 0 To .ListCount - 1
If .Selected(lCount) = True Then
Worksheets("NameOfYourSheet").Cells(lMyRow, 1).Value = .List(lCount)
lMyRow = lMyRow + 1
End If
Next
End With