如何在VBA中的用户窗体上使用组合框和文本框来搜索和查找活动Excel电子表格中的数据?

问题描述 投票:-5回答:2

我一直在努力寻找过去两天的答案,我仍然不明白如何编码。我正在学习VBA,因为我参与了一个项目,但我对vb.net编码更熟悉。所以对于这个项目,我必须添加/更新/删除数据。在更新和删除按钮单击中,我必须使用搜索到的信息位于从组合框中选择的列下的条件来搜索数据,并且它具有来自文本框中键入的数据的数据。

我不知道如何将两个条件编码在一起。我根据研究得到的最远的是编写一个Find方法,该方法仅通过文本框中键入的内容在活动工作表单元格中进行搜索和选择。如何将组合框编码为与文本框中的内容相关联的条件之一,以便成功搜索电子表格?

到目前为止这是我的代码:

Private Sub cmdSearch_Click()
    Dim strFindWhat As String
    strFindWhat = TextBox1.Text

    On Error GoTo ErrorMessage

    Cells.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _False).Select
    Exit Sub
    ErrorMessage:
        MsgBox ("The data you are searching for does not exist")

End Sub

Private Sub UserForm_Initialize()
    ComboBox1.List = Application.Transpose(Sheet1.Range("A1:D1").Value)
End Sub

电子表格:

删除按钮单击:

用于搜索的文本框:

需要实现组合框条件才能仅在指定的列内搜索文本:

excel vba excel-vba combobox textbox
2个回答
0
投票

所以她是一个新的解决方案。您必须在UserForm1模块中声明3个公共变量。因此,您可以在USerForm打开时为它们提供值,并在单击“搜索”按钮上的多个时间时查找“Naxt值”。

'Public Variables 
    Public bolFirstSearch As Boolean
    Public rng As Excel.Range
    Public cellFound  As Excel.Range

Private Sub ComboBox1_Change()
    bolFirstSearch = False
End Sub

Private Sub CommandButton1_Click()
Dim strFindWhat As String
Dim intRowCB As Integer

On Error GoTo ErrorMessage
If UserForm1.bolFirstSearch = False Then

    strFindWhat = TextBox1.Text
    intRowCB = Cells.Find(What:=ComboBox1.value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Column
    Set rng = Columns(intRowCB)
    rng.Select
        Set cellFound = rng.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        cellFound.Select
Else

    Set cellFound = rng.FindNext(cellFound)
    cellFound.Select
End If
UserForm1.bolFirstSearch = True
Exit Sub
ErrorMessage:

 MsgBox ("The data you are searching for does not exist")
End Sub

Private Sub UserForm_Initialize()
    ComboBox1.List = Application.Transpose(Sheet1.Range("A1:D1").Value)
    bolFirstSearch = False
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    bolFirstSearch = False
End Sub

0
投票
 Public Sub FindValueInColumn(bolDelete As Boolean, strColumnText As String, _
                              strSearchValue As String, strSheetName)

    Dim intColumnIndex As Integer
    Dim rngSearchRange As Range

    On Error Goto ErrHandler

    'Find Column Index
    With ThisWorkbook.Worksheets(strSheetName)
        Set rngSearchRange = .Range("A1", .Cells(1, Columns.Count).End(xlToLeft)).Find(What:=strColumnText, lookat:=xlWhole)
    End With

    If Not rngSearchRange Is Nothing Then
        intColumnIndex = rngSearchRange.Column
    End If

    If intColumnIndex > 0 then
        'Find value in specified column
        With ThisWorkbook.Worksheets(strSheetName)
            Set rngSearchRange = .Range(.Cells(1, intColumnIndex),.Cells(Rows.Count, intColumnIndex).End(xlUp)).Find(What:=strSearchValue, lookat:=xlPart)
        End With

        If rngSearchRange Is Nothing Then
            MsgBox "Value not found."
        Else
        If bolDelete Then
            rngSearchRange.ClearContents
        Else
            rngSearchRange.Select
        End
    Else
        MsgBox "Column not Found"
    End If
Exit sub

ErrHandler:
    MsgBox "Something went wrong there."
    On Error Goto 0
End Sub

这基本上是一个双工作:找到目标列,然后在该列中找到您的搜索值。只需从按钮调用sub即可。从删除按钮传递bolDelete作为true,从搜索按钮传递为false

作为一般提示:错误处理程序是一种很好的做法,但是为了实现功能而引发错误(至少在通过适当的测试可以避免的情况下)。此外,在VBA中永远不需要选择并产生不必要的开销。

这并不能解决所有问题,因为我不知道你的其余代码。我建议使用它作为参考并实现它而不是复制它。

© www.soinside.com 2019 - 2024. All rights reserved.