【问题标题】:exporting multicolumn listbox to specific range将多列列表框导出到特定范围
【发布时间】:2020-08-11 14:06:50
【问题描述】:

背景信息:我有一个用户表单,其中包含一个 2 列列表框和一个命令按钮

我想通过按钮将列表框中的所有项目(从两列)导出到工作表中。我正在考虑使用循环从列表框中获取所有信息,但不知何故它不起作用。运行时,它只给我列表框中的第一行,而不是其他行。

Private Sub CommandButton1_Click()

Dim i, t As Double
Dim inputrow As Long
Dim sh As Worksheet
Dim lastrow As Long

Set sh = Sheets("sheet1")

lastrow = sh.Range("A" & Application.Rows.Count).End(xlUp).Row + 1

i = ListBox2.ListCount

For inputrow = lastrow To lastrow + i

    For t = 0 To i
    
        sh.Range("A" & inputrow).Value = ListBox2.List(t, 0)
        sh.Range("B" & inputrow).Value = ListBox2.List(t, 1)
            
    Exit For
     
    Next t

Exit For

Next inputrow

end sub

【问题讨论】:

    标签: excel vba listbox


    【解决方案1】:

    尝试以下子。

    Private Sub CommandButton1_Click()
    Dim lRow As Long, i As Long
    Dim sh As Worksheet
    Dim Rng As Range
    
    Set sh = Sheets("Sheet1")
    lRow = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row
    
        Set Rng = Range("A" & lRow)
        For i = 0 To Me.ListBox1.ListCount - 1
            Rng.Offset(i + 1, 0) = Me.ListBox1.List(i, 0)
            'sh.Range("A" & lRow + i + 1) = Me.ListBox1.List(i, 0)
            
            Rng.Offset(i + 1, 1) = Me.ListBox1.List(i, 1)
            'sh.Range("B" & lRow + i + 1) = Me.ListBox1.List(i, 0)
        Next i
    
    Set Rng = Nothing
    Set sh = Nothing
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多