简体   繁体   English

Excel VBA:列表框

[英]Excel VBA: Listbox

on a separate Excel 2007 VBA question, how do i use the listbox under "Developer" > "Form Control" ? 在单独的Excel 2007 VBA问题上,如何使用“开发人员”>“表单控件”下的列表框?

i would like to display some data from another worksheet (say wsData), however i would like to display the data specific to a user (say wsInterface, cell A1), and NOT use the "format control" > "control" > "input range" 我想显示另一个工作表中的某些数据(例如wsData),但是我想显示特定于用户的数据(例如wsInterface,单元格A1),而不要使用“格式控件”>“控件”>“输入”范围”

so i will have a form that pops up to prompt the user for a name to be keyed into worksheet wsInterface cell A1, and i would like the data relevant to the user to be displayed in the listbox. 因此,我将弹出一个窗体,提示用户输入要输入到工作表wsInterface单元格A1中的名称,并且我希望与用户相关的数据显示在列表框中。 Thanks 谢谢

wsData: wsData:

name  |   date   | remarks
Mr A     22/2/11   blah blah blah 1.
Mr B     22/2/11   blah blah blah 2.
Mr A     23/2/11   blah blah blah 3.

wsInterface: wsInterface:

Cell A1: Mr A

ListBox (2 columns)
22/2/11   blah blah blah 1.
23/2/11   blah blah blah 3.

here are my codes in one of the macro: 这是我在其中一个宏中的代码:

Sub CustRemarkListBox_Change()
    Dim lbtarget As MSForms.ListBox
    Dim rngSource As Range
    Dim rw As Range
    Dim i As Long

    Set rngSource = Range("Remarks")

Set lbtarget = CustRemarkListBox 设置lbtarget = CustRemarkListBox

    With lbtarget
        .ColumnCount = 2
        .ColumnWidths = "50;200"
        For Each rw In rngSource.Rows
            If rw.Cells(1, 1) = Worksheets("ExistingCustomer").Range("C4") Then
                .AddItem ""
                For i = 1 To .ColumnCount
                    .List(.ListCount - 1, i - 1) = rw.Cells(1, i)
                Next
            End If
        Next
    End With
End Sub

The one in bold is my problem, it says "Object Required" meaning it did not detect my listbox in my Excel Worksheet that was named "CustRemarkListBox" 粗体字是我的问题,它说“需要对象”,这意味着它没有在我的Excel工作表中检测到名为“ CustRemarkListBox”的列表框

So if I understand you correctly, the user has to enter a name into a cell, and you want the listbox to get populated based on the name entered? 因此,如果我对您的理解正确,那么用户必须在单元格中输入一个名称,并且希望基于输入的名称填充列表框?

You could use something like the Worksheet_Range event to run some code to populate the listbox when you enter a name it can match. 您可以使用诸如Worksheet_Range事件之类的东西来运行一些代码,以在输入可以匹配的名称时填充列表框。

How familiar are you with VBA? 您对VBA有多熟悉? You could use something like the following in the codpage for the relevant sheet: 您可以在相关表的鳕鱼页中使用以下内容:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)  
If Target.Address = "A1" Then  
'put your code here  
End If  
End Sub

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

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