I have a listbox that is supposed to display entries in initialize when a button is clicked from excel sheet.
我有一个列表框,当从Excel表格中单击按钮时,它应该显示初始化中的条目。
The button is calling a module to show the form.
该按钮正在调用一个模块以显示该表单。
Module code:
模块代码:
Option Explicit
Sub Show_Form()
UserForm1.Show
End Sub
The form shows but the listbox display is blank.I have been setting the property of listbox to different multiselect but still displays blank when the green button above is clicked.
表单显示,但列表框显示为空。我一直在将列表框的属性设置为不同的多选,但当单击上面的绿色按钮时,它仍然显示为空。
But when I click the run button in VBA Developer:
但当我在VBA Developer中单击Run按钮时:
Sometimes it displays, sometimes it does not.
有时它会显示,有时它不会。
Is this a bug?
这是个窃听器吗?
This is the code of my simple form. The code below is for saving entries and displaying the last 10 entries. No problem with that. Just the listbox not displaying but sometimes displaying.
这是我的简单表单的代码。下面的代码用于保存条目并显示最后10个条目。这没问题。只是列表框不显示,但有时会显示。
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("ExcelEntryDB")
Dim n As Long
n = sh.Range("C" & Application.Rows.Count).End(xlUp).Row
sh.Range("C" & n + 1).Value = Format(Date, "mm/dd/yyyy")
sh.Range("D" & n + 1).Value = Format(Time, "hh:nn:ss AM/PM")
sh.Range("E" & n + 1).Value = Me.txtColor.Value
sh.Range("F" & n + 1).Value = Me.txtName.Value
sh.Range("G" & n + 1).Value = Me.txtShape.Value
Me.txtName.Value = ""
Me.txtColor.Value = ""
Me.txtShape.Value = ""
showListBoxEntries
End Sub
Private Sub UserForm_Initialize()
showListBoxEntries
End Sub
Sub showListBoxEntries()
'showing 10 entries only in listbox
Dim arr(10, 5), lastRow As Long, i As Integer 'get last column number using index
With ActiveSheet
arr(0, 0) = .Cells(1, 3)
arr(0, 1) = .Cells(1, 4)
arr(0, 2) = .Cells(1, 5)
arr(0, 4) = .Cells(1, 7)
arr(0, 5) = .Cells(1, 8)
lastRow = .Cells(Rows.Count, 3).End(xlUp).Row
If lastRow > 10 Then
For i = 1 To 10
arr(i, 0) = .Cells(lastRow - 10 + i, 3).Text
arr(i, 1) = .Cells(lastRow - 10 + i, 4).Text
arr(i, 2) = .Cells(lastRow - 10 + i, 5).Text
arr(i, 3) = .Cells(lastRow - 10 + i, 6).Text
arr(i, 4) = .Cells(lastRow - 10 + i, 7).Text
Next
Else
For i = 1 To lastRow - 1
arr(i, 0) = .Cells(i + 1, 3).Text
arr(i, 1) = .Cells(i + 1, 4).Text
arr(i, 2) = .Cells(i + 1, 5).Text
arr(i, 3) = .Cells(i + 1, 6).Text
arr(i, 4) = .Cells(lastRow - 10 + i, 7).Text
Next
End If
End With
With Me.ListBox1
.ColumnHeads = True
.ColumnCount = 6
.ColumnWidths = "75,75,75,75,75,75"
.List = arr()
End With
End Sub
更多回答
Try this: Instead ActiveSheet define the name of the sheet. With Worksheets("name_of_sheet")
.
尝试这样做:而不是ActiveSheet定义工作表的名称。工作表(“name_of_Sheet”)。
To show the headers you need to use the RowSource property. To use the RowSource property, you need a range with the headers. See this as an alternative.
要显示标头,您需要使用RowSource属性。若要使用RowSource属性,您需要具有标头的范围。把这视为另一种选择。
@VBasic2008 Yes, thanks. I am still trying to apply your code in that link in my previous question. I am almost there.
@VBasic2008是的,谢谢。我仍在尝试将您的代码应用于我上一个问题中的那个链接。我就快到了。
@Blackcat okay let me try
@Blackcat,好的,让我试试
Never noticed it was your question. This is VBA so you probably should have gone one-based to simplify. Have you checked if the array contains the correct values? As I stated in my previous answer, to get the headers, you need to write the array to a range e.g. A1:F11
and then use the RowSource property with e.g. lbx.RowSource = ActiveSheet.Range("A2:F11").Address(External:=True)
. Note A2
excluding the headers, not A1
.
没注意到这是你的问题。这是VBA,所以您可能应该使用基于一的方法来简化。您检查数组是否包含正确的值了吗?正如我在前面的回答中所述,要获得标头,您需要将数组写入一个范围,例如a1:f11,然后使用RowSource属性,例如lbx.RowSource=ActiveSheet.Range(“A2:F11”).Address(External:=True).注A2,不包括标题,而不是A1。
我是一名优秀的程序员,十分优秀!