【问题标题】:Search Button VBA : Find values in Excel cell and populate fields based upon finding results搜索按钮 VBA:在 Excel 单元格中查找值并根据查找结果填充字段
【发布时间】:2021-03-04 23:18:06
【问题描述】:

我正在处理一个 Excel 表单,它有一个文本框 txtSearch,用户可以在其中输入一些值。按下时 搜索按钮 btn_search2 如果在 Sheet1 上找到值,则应按找到值的行填充某些字段。否则系统应返回一条消息:未找到数据。

如果我从列 A 输入值但不搜索其他列,我的代码可以正常工作。

对此有什么想法吗?

我的代码如下:

    Dim iSearch As Long, i As Long
    
    iSearch = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
    
    For i = 1 To iSearch
    
        If Trim(Sheet1.Cells(i, 1)) <> Trim(txtSearch.Text) And i = iSearch Then
        
            MsgBox ("Invalid data")
            txtSearch.Text = ""
            txtSearch.SetFocus
            
        End If
    
        If Trim(Sheet1.Cells(i, 1)) = Trim(txtSearch.Text) Then
        
            txtRef.Text = Sheet1.Cells(i, 1)
            txtFirstname.Text = Sheet1.Cells(i, 2)
            txtSurname.Text = Sheet1.Cells(i, 3)
            txtAddress.Text = Sheet1.Cells(i, 4)
    
            txtPostCode.Text = Sheet1.Cells(i, 5)
            txtTelephone.Text = Sheet1.Cells(i, 6)
            txtDateReg.Text = Sheet1.Cells(i, 7)
            txtProve.Text = Sheet1.Cells(i, 8)
            txtMemberType.Text = Sheet1.Cells(i, 9)
            txtMemberFees.Text = Sheet1.Cells(i, 10)
            
            Exit For
            
        End If
        
    Next i


    

【问题讨论】:

    标签: excel vba forms loops button


    【解决方案1】:

    如果我正确理解您的问题,您希望在当前数据区域中搜索所有行和列以查找值 txtSearch,但您的代码仅搜索 A 列中的所有行。正确吗?

    如果是这样,这是因为您针对 txtSearch 检查每行中的值的代码块仅检查每行中的单元格 (i,1)。在单元格索引中,第一个值(在您的情况下为 i)是行号,第二个值(在您的情况下为 1)是列号。由于 i 的值正在变化,因此您正在检查多行。由于第二个索引 (1) 的值没有变化,因此您只检查第 1 列(“A”)。

    要检查所有列,您需要一个内部 for 循环来遍历每行中的所有列。所以你需要另一个变量来检查列的总数和另一个循环来遍历它们,就像这样:

    Dim iSearch As Long, i As Long
    Dim jSearch As Long, j As Long
        
        iSearch = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
        jSearch = Worksheets("Sheet1").Range("A1").CurrentRegion.Columns.Count
        
        For i = 1 To iSearch
            
            For j = 1 to jSearch
    
                If Trim(Sheet1.Cells(i, j)) <> Trim(txtSearch.Text) And i = iSearch And j = jSearch Then
            
                    MsgBox ("Invalid data")
                    txtSearch.Text = ""
                    txtSearch.SetFocus
                
                End If
        
            If Trim(Sheet1.Cells(i, j)) = Trim(txtSearch.Text) Then
            
                txtRef.Text = Sheet1.Cells(i, 1)
                txtFirstname.Text = Sheet1.Cells(i, 2)
                txtSurname.Text = Sheet1.Cells(i, 3)
                txtAddress.Text = Sheet1.Cells(i, 4)
        
                txtPostCode.Text = Sheet1.Cells(i, 5)
                txtTelephone.Text = Sheet1.Cells(i, 6)
                txtDateReg.Text = Sheet1.Cells(i, 7)
                txtProve.Text = Sheet1.Cells(i, 8)
                txtMemberType.Text = Sheet1.Cells(i, 9)
                txtMemberFees.Text = Sheet1.Cells(i, 10)
                
                Exit For
                
            End If
        Next j
            
        Next i
    

    您还可以通过添加 With 块并仅在位于最后一行和最后一列时检查“Trim(...)Trim(...)”来稍微清理您的代码,因为在无论如何您都想继续搜索的所有其他情况:

    Dim iSearch As Long, i As Long
    Dim jSearch As Long, j As Long
        
        With Worksheets("Sheet1").Range("A1").CurrentRegion
            iSearch = .Rows.Count
            jSearch = .Columns.Count
        End With
        
        For i = 1 To iSearch
            
            For j = 1 to jSearch
            If Trim(Sheet1.Cells(i, j)) = Trim(txtSearch.Text) Then
            
                txtRef.Text = Sheet1.Cells(i, 1)
                txtFirstname.Text = Sheet1.Cells(i, 2)
                txtSurname.Text = Sheet1.Cells(i, 3)
                txtAddress.Text = Sheet1.Cells(i, 4)
        
                txtPostCode.Text = Sheet1.Cells(i, 5)
                txtTelephone.Text = Sheet1.Cells(i, 6)
                txtDateReg.Text = Sheet1.Cells(i, 7)
                txtProve.Text = Sheet1.Cells(i, 8)
                txtMemberType.Text = Sheet1.Cells(i, 9)
                txtMemberFees.Text = Sheet1.Cells(i, 10)
                
                Exit For
                
            End If
        Next j
            
        Next i
      
        MsgBox ("Invalid data")
        txtSearch.Text = ""
        txtSearch.SetFocus
        
    

    【讨论】:

    • 感谢艾莉森一切正常。我不得不删除 MsgBox(“无效数据”),因为它会打印搜索结果并显示此消息框。如果有多个搜索结果,是否有任何解决方法?也许将它们打印在列表中?
    • 当然,您可以将它们打印在列表中。您可以使用搜索结果保存所有行的位置(例如,有一个数组 SearchResults 并为每个匹配结果保存行号 i ,然后在末尾有一个循环来遍历 SearchResults 中指示的行的所有值并打印每个一)或随手打印。 MsgBox 是我的错误,我以为它说的是Exit Sub 而不是Exit For——如果它说的是ExitSub,那么MsgBox 只有在没有找到结果的情况下才会出现,否则一旦找到结果子程序就会结束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多