在VBA Excel中循环使用特定列的使用范围

我想获得特定列中的所有单元格的值,并将它们添加到列表中这是我所尝试的

Dim rng As Range 'here ColumnName will be like "A", "B" etc... 'and SheetName will be like "Sheet001", "Sheett002" etc... rng = ThisWorkbook.Sheets(SheetName).Columns(ColumnName).UsedRange For Each cell In rng.Cells If Not cell.Value = "" Then ListBox1.AddItem (cell.Value) End If Next 

但是我无法确定如何根据名称来获得已使用的列的范围。 我想要阅读的数据将在单独的表格中显示

在这里输入图像说明

UsedRange将引用表单的已用范围。 随着UsedRange你可以select所需的列。

我修改了一下你的代码。 见下文:

 Sub SomeSub() Dim MySht As Worksheet Dim MyRng As Range Set MySht = ThisWorkbook.Sheets("Sheet1") Set MyRng = MySht.UsedRange.Columns("A") For Each cell In MyRng.Cells If Not cell = "" Then 'If your List Box is in "Sheet1" Sheets("Sheet1").ListBox1.AddItem (cell) 'If your List Box is in "UserForm1" UserForm1.ListBox1.AddItem (cell) End If Next 'To clear ListBox1 data ' Sheets("Sheet1").ListBox1.Clear ' UserForm1.ListBox1.Clear End Sub