如何从列表框中将所选值(一次一个)添加到特定的Excel列

我正在使用包含文件夹名称的列表框。 我需要从列表框中select一个名字(一次一个,保持select的顺序)并将其添加到excel列A1,这样每次添加到列A的下一个空单元格。我对vb并需要帮助。 以下是我尝试的方法。

方法1)

Sub AddRecord_Click() With Sheet1.ListBox1 For intIndex = 0 To .ListCount - 1 With ActiveSheet LastRow = .Cells(.Rows.Count, "F").End(xlUp).Row End With If .Selected(intIndex) Then Sheet1.Cells(LastRow, "A") = Sheet1.ListBox1.Value NextRow = LastRow + 1 End If Next End With End Sub 

方法2)

 Sub AddRecord_Click() intRecord = (CInt(Range("A1").End(xlDown).Row) + 1) Sheet1.Cells(intRecord, "A") = Sheet1.ListBox1.Value intRecord = intRecord + 1 End Sub 

试试这可能对你有所帮助

  Sub ListBox7_Change() Dim i As Long With ActiveSheet.ListBoxes("List Box 7") For i = 1 To .ListCount If .Selected(i) Then Range("A" & Rows.count).End(xlUp).offset(1).Value = .List(i) End If Next i End With End Sub 

首先从Excel工作表中获取最后一次使用的行,最后递增最后一行并将下一列值插入到Excel中。

 Dim last as Excel.Range = xlWorkSheet.Cells.SpecialCells (Excel.XlCellType.xlcellTypeLastCell,Type.Missing) dim lastUsedRow As Integer = last.Row lastUsedRow += 1 xlWorksheet.RangeA("A"+ lastUserRow).value = ListBox1.Value