简体   繁体   English

Excel VBA列到列表框

[英]excel vba columns to listbox

I'm looking to populate an Excel VBA userform listbox with values from Sheet 1, Row 1 with a dynamic number of columns in the sheet (columns can range between 22 and 30 columns). 我正在寻找用表格1,第1行中的值填充表格中动态列数的Excel VBA用户窗体列表框(列的范围可以在22到30列之间)。

So far I have the following code, but it only populates the value of cell "A1" in the listbox. 到目前为止,我有以下代码,但是它仅填充列表框中单元格“ A1”的值。

Dim rngSource As Range
Dim lCol As Long

'Determine last column
lCol = Cells(1, Columns.Count).End(xlToLeft).Column

'Set range source data to be included in listbox
Set rngSource = Worksheets("Sheet1").Range(Cells(1, 1), Cells(1, lCol))

'Populate listbox with range source data
lstAllFields.List = rngSource.Cells.Value

Thanks for the assistance. 感谢您的协助。

Change your statement which says 更改您的陈述

ListBox1.List = rngSource.Cells.Value

to be 成为

ListBox1.List = Application.Transpose(rngSource.Cells.Value)

so that the cells are treated as if they were a column of values instead of a row of values. 以便将单元格视为值的一列而不是一行的值。


As pointed out in a comment by ASH, you also have unqualified properties (ie things like Cells where you don't specify which worksheet that the property is referring to, and therefore it defaults to the active sheet). 正如ASH的评论中指出的那样,您还具有不合格的属性(例如,像Cells这样的东西,您没有指定该属性所指向的工作表,因此默认为活动工作表)。 These can cause problems as soon as you start needing to use more than one worksheet in your macro, so it is better to get into the habit of fully qualifying things now. 一旦您开始需要在宏中使用多个工作表,这些就会引起问题,因此最好养成现在完全合格的习惯。

At the moment, your code (after my suggested correction above) is equivalent to: 目前,您的代码(经过我上述建议的更正后)等效于:

Dim rngSource As Range
Dim lCol As Long

'Determine last column
lCol = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column

'Set range source data to be included in listbox
Set rngSource = Worksheets("Sheet1").Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(1, lCol))

'Populate listbox with range source data
lstAllFields.List = Application.Transpose(rngSource.Cells.Value)

The use of Cells belonging to the ActiveSheet as the bounds for a Range on Worksheets("Sheet1") works fine while ActiveSheet and Worksheets("Sheet1") are the same thing, but will crash if they are different. ActiveSheetWorksheets("Sheet1")是相同的东西时,使用属于ActiveSheetCells作为Worksheets("Sheet1")上的Range的边界可以很好地Worksheets("Sheet1") ,但是如果它们不同,则会崩溃。

I would recommend the use of a With Worksheets("Sheet1") block, which just allows us to syntactically shortcut all occurrences of Worksheets("Sheet1") to simply . 我建议使用With Worksheets("Sheet1")块,该块仅允许我们在语法上将所有出现的Worksheets("Sheet1")简化为simple . . Your code would then look like: 您的代码将如下所示:

Dim rngSource As Range
Dim lCol As Long

With Worksheets("Sheet1")    
    'Determine last column
    lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

    'Set range source data to be included in listbox
    Set rngSource = .Range(.Cells(1, 1), .Cells(1, lCol))

    'Populate listbox with range source data
    lstAllFields.List = Application.Transpose(rngSource.Cells.Value)
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM