【问题标题】:How to populate a ListBox in a UserForm with a variable set of Rows from Last Row?如何使用最后一行中的一组可变行填充用户窗体中的列表框?
【发布时间】:2019-05-05 14:22:45
【问题描述】:

我有一个工作表“Sheet1”,数据范围为“A:R”,在输入新条目时行不断添加。每个条目的行范围都不同,因此“A 列”中的行会重复,这样它就可以很好地显示在数据透视表中。现在要检查最后一个条目是否实际上已粘贴,一种检查方法是按“Cltr+Down”键。我有一个用户窗体中的列表框来填充最后输入的条目。但它只显示 1 行,即最后一行。如何显示该特定条目是否假设有 5 行?

我有这段代码,它显示输入的最后一行。我知道缺少定义范围的主要块。我只是不知道该怎么做。请帮忙。 这是我需要在用户窗体中填充列表框的数据库的屏幕截图。

这是我单击“CommandButton1”时带有 ListBox 的用户窗体的屏幕截图

Screenshot of Userform when 'CommandButton1' is clicked

Private Sub CommandButton1_Click()
Dim Rng As Range
Dim lr As Long
Dim x(1 To 2, 1 To 18), y As Long
    With Worksheets("Sheet1")
         lr = .Cells(.Rows.Count, 1).End(xlUp).Row
        UserForm1.ListBox1.ColumnCount = 18
        For y = 1 To 18
            x(1, y) = .Cells(1, y)
            x(2, y) = .Cells(lr, y)
        Next y
              UserForm1.ListBox1.List = x
    End With
UserForm1.Show
End Sub

我希望 025 - GRACE PHILLIPS(来自数据库)的记录显示在 ListBox 上,稍后添加新条目时,它应该显示输入的新条目

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    让我们使用 ADODB.recordset 尝试另一种方法

      Option Explicit
    
    Private Sub CommandButton1_Click()
    
        Dim criteria As String, fldName As String, shtName As String
        Dim con As Object  ' ADODB.Connection
        Dim rcd As Object  ' ADODB.Recordset
        Dim colCount As Long
    
        ' Assuming the list has a header
        Set con = GetExcelConnection(ThisWorkbook.FullName, True)
    
        Set rcd = CreateObject("ADODB.Recordset")
        'Set rcd = New ADODB.Recordset
    
        ' Assuming the list is on the first sheet and
        shtName = ThisWorkbook.Sheets(1).Name
    
        ' Reading the list into a recordset
        With rcd
            .ActiveConnection = con
            .CursorType = 2  ' adOpenDynamic
            .LockType = 3    ' adLockOptimistic
            .Source = "SELECT * FROM [" & shtName & "$]"
            .Open
        End With
    
        colCount = rcd.Fields.Count + 1
        ' Move to the last record in the list
        rcd.MoveLast
    
        ' Trying to find the row where S.L.No. does contain avalue
        Do While IsNull(rcd.Fields(0).Value)
            rcd.MovePrevious
            If rcd.BOF Then
                MsgBox "No S.L.No found", vbOKOnly + vbCritical, "Error - Exit"
                Exit Sub
            End If
        Loop
    
        ' get the value and the fieldname for the filter
        criteria = rcd.Fields(0).Value
        fldName = rcd.Fields(0).Name
    
    
        ' Filter the list by the value in the first column and last row
        ' This assumes you do not have different areas with the same value
        rcd.Filter = fldName & " = '" & criteria & "'"
    
        ' Prpearing everything for the Userform
        Dim frm As New UserForm1
        Load frm
        With frm
            .ListBox1.ColumnCount = colCount
            .ListBox1.Column = rcd.GetRows   ' This transfers the filtered records into the lsitbox
            .Show
        End With
    
    End Sub
    
    Function GetExcelConnection(ByVal Path As String, _
        Optional ByVal Headers As Boolean = True) As Object
        Dim strConn As String
        Dim objConn As Object ' ADODB.Connection
        Set objConn = CreateObject("ADODB.Connection")
        strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=" & Path & ";" & _
            "Extended Properties=""Excel 8.0;HDR=" & _
            IIf(Headers, "Yes", "No") & """"
        objConn.Open strConn
        Set GetExcelConnection = objConn
    End Function
    

    【讨论】:

    • criteria = rcd.Fields(0).Value 此行给出“无效使用 Null”错误。
    • 据我了解,有一个代码可用,它在用户窗体中有一个输入框和一个列表框。 ListBox 由您在输入框中输入的内容填充。类似于 Google 搜索框的东西。现在该代码适用于类似的过程,对吗?它只显示那些包含某个单词的行(在这种情况下是在文本框中输入的单词。现在我们如何用我在第一篇文章中发布的 LastRow 单元格替换“文本框”。我们确实得到了最后一行,对吗?从那我们如何用匹配的单元格值填充列表框。
    • 我更新了答案。我不明白你的最后评论。您以前从未提到过输入框。我的代码不像谷歌搜索框那样工作。但你是对的,我通过列表最后一行和第一列中的 S.L.NO 的值过滤列表。根据您的反馈,该程序还会识别属于列表的行,这些行在 S.L.NO 列中没有值。我试图通过添加一些代码检查来解决这个问题。
    • 别在意之前的评论,我只是想在别处解释另一个例子......回到这个......在你更新这个之后,它就像一个魅力!!!!美丽的 。太感谢了。这就是我想要的。我希望当我添加新记录时这也会起作用,对吗?我得检查一下。不过谢谢。最后一件事,如何也显示标题?现在它只缺少标题。
    • 你不能,除非你使用Rowsource porperty of the listbox 更新:对不起,我可能错了。请给我一分钟
    【解决方案2】:

    我会这样做

    选项 1 您可以使用 CurrentRegion 来获取范围。我不确定这是否适合你。

    Private Sub CommandButton1_Click()
        Dim rg As Range
        Set rg = Range("A1").CurrentRegion
        ListBox1.List = rg.Value
    End Sub
    

    选项 2 或者您像在发布的代码中那样确定最后一行并使用它

    Private Sub CommandButton1_Click()
        Dim rg As Range
        Dim lr As Long
        lr = Cells(Rows.Count, 1).End(xlUp).Row
        Set rg = Range("A1:R" & lr)
        ListBox1.List = rg.Value
    End SUb
    

    请根据需要添加工作表和用户表单参考

    更新基于 选项 2 中 rg 的注释使用以下行

    Set rg = Range("A" & lr & ":R" & lr)
    

    更新 2 我假设您要添加到列表框中的行由包含 S.L.No. 的第一列标识。

    Private Sub CommandButton2_Click()
    
        Dim rg As Range, rg1 As Range
        Dim lr As Long
    
        lr = Cells(Rows.Count, 1).End(xlUp).Row
        Set rg = Range("A1" & ":R" & lr)
    
        rg.AutoFilter Field:=1, Criteria1:=rg.Cells(lr, 1).Value
        Set rg1 = rg.SpecialCells(xlCellTypeVisible)
       ' Assumption is the newly added rows are in one and therefore in the last area
        Set rg1 = rg1.Areas(rg1.Areas.Count)
    
    
        ListBox1.ColumnCount = rg.Columns.Count
        ListBox1.List = rg1.Value
    
        rg.AutoFilter
    End Sub
    

    【讨论】:

    • 您提供的代码确实有效,但它显示了整个数据库范围。我只需要最后输入的数据库条目,例如名为 GRACE PHILLIP 的记录号 025 和她各自的列,直到列 R 显示在列表行上。请注意,GRACE PHILLIP 的条目有 3 行。我该怎么做呢?感谢您的帮助!
    • 好吧,那我误会你了。我更新了答案。这是你需要的吗?
    • 几乎.... 只是“GRACE PHILLIP 在她的记录中有 3 行。我如何显示她的 3 条记录?请注意,在我保留的情况下,这条“3 条记录”会因记录而异输入它们。谢谢!
    • 您如何识别属于 GRACE PHILLIP 的行?通过 S.L.NO?
    • 是的,由 Sl。没有。
    猜你喜欢
    • 2020-04-03
    • 2023-03-28
    • 2021-12-07
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多