【问题标题】:Userform to display values from multiple rows用户窗体显示多行的值
【发布时间】:2021-04-25 02:46:06
【问题描述】:

我创建了一个用户表单,使用户可以在文本框中输入最多 5 个唯一 ID,以及 6 个不同的信息(文本框和列表框的混合),这些信息对于所有 5 个 ID 输入都保持不变。

然后将此信息登录到工作表中,每个唯一 ID 都有自己的行,其余 6 条信息在每个对应行中重复。

对于每个记录的 ID 行,都有一个唯一的参考编号。将自动生成,这意味着您将拥有一个表,其中前两列如下所示:

Unique Reference Number Unique ID
001 2120
001 2130
001 8765
002 7688
002 7684
002 7682
002 7681
002 7666

我在用户表单中添加了一个命令按钮和文本框,以使用户能够搜索唯一的参考编号(例如 001),但我希望代码能够在电子表格中找到所有相应的行(最多 5 行)包含搜索到的参考编号,然后在用于在用户表单中记录信息的相同文本/列表框中显示最多 5 行和 6 条信息。

当我当前搜索参考号时,用户表单在第一个 ID 文本框中显示第一个 ID,并在相应的文本框中显示 6 条信息,没有问题。但随后它会在第二个 ID 文本框中显示所有后续行的 ID 号 - 这意味着它正在查找正确的信息,但未将其显示到用户表单中的正确文本框中。

本质上,我试图让代码循环遍历工作表中的第一列并找到所有匹配值(参考编号),然后从相应 ID 文本框中的每一行中检索并显示唯一 ID 信息用户表单。

Private Sub CommandButton1_Click()

    Dim x As Long
    Dim y As Long
    Dim found As Boolean 

    With Sheets("Example Spreadsheet")
        x = .Range("A" & .Rows.Count).End(xlUp).Row
        For y = 1 To x
            If .Cells(y, 1).Text = Search.Value Then
                If Not found Then 
                    found = True 
                    Me.ID1.Value = .Cells(y, 2)
                    Me.Branch.Value = .Cells(y, 3)
                    Me.AccountNo.Value = .Cells(y, 4)
                    Me.Name.Value = .Cells(y, 5)
                    Me.DateReceived.Value = .Cells(y, 6)
                    Me.DateClosed.Value = .Cells(y, 7)
                Else 
                    Me.ID2.Value = Me.ID2.Value & .Cells(y, 2)
                End If
            End If
        Next y
    End With

End Sub

代码仅涉及文本框 ID1 和 ID2,但我尝试合并其他 ID3-5 文本框,无法正确显示信息。

【问题讨论】:

  • 不确定我是否遵循这一点。一旦found 变为True 它永远不会重置为False,这是否重要?不确定您是否需要该布尔值,并且使用Find 可能更有效。
  • 感谢您的 cmets。是的,我也不确定是否需要布尔值-目前正在学习。肯定会考虑 Find 功能。

标签: excel vba for-loop userform


【解决方案1】:

在做一些涉及查找匹配的事情时,我喜欢将这两个部分分开——首先找到所有匹配,然后处理它们。使您的代码更清晰,您可以专注于主要任务,而不是在查找和处理之间进行上下文切换。 (以下未经测试的代码)

Private Sub CommandButton1_Click()
    Const MAX_HITS As Long = 5
    Dim x As Long, found As Collection, rw As Range

    'get all matches
    With Sheets("Example Spreadsheet")
        Set found = FindAll(.Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row), Search.Value)
    End With
    
    If found.Count > 0 Then
        For x = 1 To found.Count
            If x = 1 Then
                Set rw = found(x).EntireRow 'the whole row for this matched cell
                Me.ID1.Value = rw.Cells(2).Value
                Me.Branch.Value = rw.Cells(3).Value
                Me.AccountNo.Value = rw.Cells(4).Value
                Me.Name.Value = rw.Cells(5).Value
                Me.DateReceived.Value = rw.Cells(6).Value
                Me.DateClosed.Value = rw.Cells(7).Value
            ElseIf x > MAX_HITS Then
                'make sure we didn't find too many...
                MsgBox "Too many matches (" & found.Count & ") for " & Search.Value
            Else
                Me.Controls("ID" & x).Value = found(x).Value 'refer to control by name
            End If
        Next x
    Else
        MsgBox "No hits for " & Search.Value
    End If
    
End Sub

'return all matching cells in a collection
Public Function FindAll(rng As Range, v) As Collection
    Dim rv As New Collection, f As Range
    Dim addr As String
 
    Set f = rng.Find(what:=v, after:=rng.Cells(rng.Cells.Count), _
        LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False)
    If Not f Is Nothing Then addr = f.Address()
    Do Until f Is Nothing
        rv.Add f
        Set f = rng.FindNext(after:=f)
        If f.Address() = addr Then Exit Do
    Loop
    Set FindAll = rv
End Function

【讨论】:

  • 蒂姆非常感谢您的建议和帮助!我将在今天晚些时候测试/调整您提供的代码,并让您知道它的运行情况。
  • 好的,我已经测试了代码,我们肯定越来越近了!用户窗体中的 ID 框(1、2、3、4、5 等)现在都根据行数填充,但它们填充的是第一列中列出的值(参考编号)而不是第 2 列中的唯一 ID。我正在尝试调整代码以查看是否可以让它查看正确的第二列,但欢迎任何进一步的建议。肯定已经取得了进展,非常感谢您的帮助!
  • 我将 Me.Controls("ID" & x).Value = found(x).Value 修改为 Me.Controls("ID" & x).Value = found(x).Offset( , 1).Value 并且它现在完全按照我希望的那样工作。我非常感谢您对蒂姆的帮助。我非常接近放弃。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
  • 2019-03-13
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多