首页 文章

VBA Ecel UserForm多选,动态项目列表

提问于
浏览
0

我用这个指令做了userForm:http://www.excel-easy.com/vba/examples/multiple-list-box-selections.html

我试着根据表做动态项目列表,

With ListBox_1
.RowSource = 
 Worksheets("warianty").ListObjects("in_").ListColumns(1).DataBodyRange
.ColumnHeads = True
.ColumnCount = 3
End With

它显示为空白,没有可供选择的列表 . 我使用对象表来获取动态范围 . 当然我可以使用其他解决方案,但这个似乎是可以实现的 .

然后我想将每个选定的项目复制到从第一行开始的其他表格 . 这是我尝试根据教程中的其他按钮修改的代码 .

Private Sub button_save_Click()
 Dim counter As Integer
 counter = 0
 For i = 0 To ListBox_2.ListCount - 1
 If ListBox_2.Selected(i - counter) Then
 ListBox_2.copy (i - counter) 'it gives error here
Worksheets("dane wejściowe").ListObjects("Tabela41").DataBodyRange(1 + i, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False,         Transpose:=True
 counter = counter + 1
 End If
 End Sub

除了改进代码之外,SB能告诉我为什么我不能只是'复制'列表ListBox.value或者某事?

1 回答

  • 1

    请尝试以下方式:

    Code

    Private Sub UserForm_Initialize()
      With ListBox_1
    
            .RowSource = Worksheets("warianty").ListObjects("in_1").ListColumns(1).DataBodyRange.Address
            .ColumnHeads = True
            .ColumnCount = 3
    
            End With
    End Sub
    
    
            'end this:
    
            Private Sub button_save_Click()
    
            For i = 0 To ListBox_2.ListCount - 1
             If ListBox_2.Selected(i) Then
    
           Worksheets("dane wejsciowe").Select
    
    
            Dim new_row As ListRow
            Set new_row = Worksheets("danewejsciowe").ListObjects("Tabela41").ListRows.Add(AlwaysInsert:=True)
    
    
    
    
            new_row.Range.Cells(1, 1).Value = ListBox_2.List(i)
    
    
             End If
    
    
            Next
            End Sub
    

相关问题