0

我有一个代码,它在用户表单列表框中显示范围(A1:E10)的非连续范围

Private Sub UserForm_Initialize()
Dim rng As Range

Set rng = Sheet1.Range("A1:E10")

With ListBox1
  .ColumnCount = 3
  .ColumnWidths = "100;100,100"
  ' load 1st, 3rd and 5th columns of range (A,C and E) into listbox
  .List = Application.Index(rng, Evaluate("ROW(1:" & rng.Rows.Count & ")"), Array(1, 3,5))
End With
End Sub

现在我需要一个单击按钮代码来将列表框中的任何选定行检索到另一个范围(G1:I1)

4

1 回答 1

1

将以下宏分配给该按钮。我将您的列表框视为表单控件。在 activeX 控件的情况下,将.ListBoxes("ListBox1")替换为.ListBox1并从0开始循环到.ListCount-1

 Sub Test()

        Dim lngLoop         As Long
        Dim strValue        As String

        With ThisWorkbook.Worksheets("Sheet1")
            With .ListBoxes("ListBox1")
                For lngLoop = 1 To .ListCount
                    If .Selected(lngLoop) Then
                        strValue = strValue & "|" & .List(lngLoop)
                    End If
                Next
           End With
           .Range("G1:I1") = Split(Mid(strValue, 2), "|")
        End With

    End Sub
于 2013-06-13T09:46:09.200 回答