搜索引擎使用文本框和组合框

时间:2016-12-20 05:10:28

标签: excel vba excel-vba

我有一个userform,它使用textbox和combobox作为搜索引擎。组合框将具有excel表中某些字段的标题,用户将在文本框中输入搜索值。然后它将在列表框中显示数据

示例 组合框值是"客户","公社","年" .lets说我选择"客户"然后在文本框中输入faa,然后单击搜索按钮,它将在列表框中显示有关faa的所有数据。或者如果我选择"年"并输入1998,它将在列表框中显示1988年的所有数据

问题是我的搜索按钮不起作用,我尝试了一些选项但继续收到错误。下面是我在搜索按钮

中的代码示例
    `Private Sub CommandButton2_Click()

    Dim sat, s As Long
    Dim deg1, deg2 As String

    If TextBox7.Value = "" Then
    MsgBox "Please enter a value", vbExclamation
    TextBox7.SetFocus
    Exit Sub
    End If

    If ComboBox3.Value = "" Or ComboBox3.Value = "-" Then
    MsgBox "Choose a filter field", vbExclamation
    ComboBox3.SetFocus
    Exit Sub
    End If


    deg2 = TextBox7.Value
    Select Case ComboBox3.Value
    Case "Commune"
    For sat = 2 To Cells(65536, "a").End(xlUp).Row
    Set deg1 = Cells(sat, "a")
    If UCase(deg1) Like UCase(deg2) & "*" Then
    ListBox1.AddItem
    ListBox1.List(s, 0) = Cells(sat, "D")
    ListBox1.List(s, 1) = Cells(sat, "E")
    ListBox1.List(s, 2) = Cells(sat, "F")
    ListBox1.List(s, 3) = Cells(sat, "G")
    ListBox1.List(s, 4) = Cells(sat, "H")
    ListBox1.List(s, 5) = Cells(sat, "I")
    ListBox1.List(s, 6) = Cells(sat, "J")
    ListBox1.List(s, 7) = Cells(sat, "K")
    s = s + 1
    End If: Next

    Case "Client"
    For sat = 2 To Cells(65536, "b").End(xlUp).Row
    Set deg1 = Cells(sat, "b")
    If UCase(deg1) Like UCase(deg2) & "*" Then
    ListBox1.AddItem
    ListBox1.List(s, 0) = Cells(sat, "D")
    ListBox1.List(s, 1) = Cells(sat, "E")
    ListBox1.List(s, 2) = Cells(sat, "F")
    ListBox1.List(s, 3) = Cells(sat, "G")
    ListBox1.List(s, 4) = Cells(sat, "H")
    ListBox1.List(s, 5) = Cells(sat, "I")
    ListBox1.List(s, 6) = Cells(sat, "J")
    ListBox1.List(s, 7) = Cells(sat, "K")
    s = s + 1
    End If: Next

    Case "Year"
    For sat = 2 To Cells(65536, "d").End(xlUp).Row
    Set deg1 = Cells(sat, "d")
    If UCase(deg1) Like UCase(deg2) & "*" Then
    ListBox1.AddItem
    ListBox1.List(s, 0) = Cells(sat, "D")
    ListBox1.List(s, 1) = Cells(sat, "E")
    ListBox1.List(s, 2) = Cells(sat, "F")
    ListBox1.List(s, 3) = Cells(sat, "G")
    ListBox1.List(s, 4) = Cells(sat, "H")
    ListBox1.List(s, 5) = Cells(sat, "I")
    ListBox1.List(s, 6) = Cells(sat, "J")
    ListBox1.List(s, 7) = Cells(sat, "K")
    s = s + 1
    End If: Next

   End Select
   End Sub`

1 个答案:

答案 0 :(得分:0)

查询不是您期望的查询时,活跃 worksheet的问题很可能就是< / p>

所以请使用完全限定(至少worksheet个对象)范围引用

此外,请注意VBA声明语句隐式地假设任何非显式声明变量的Variant类型,因此;

Dim deg1, deg2 As String

实际上会从deg2类型声明String,但deg1作为变体

这不应该与你的问题有任何关系,因为在以下声明之后:

Set deg1 = Cells(sat, "a")

deg1将从Variant/Range类型实例化,因此Value仍将被视为默认property,而If UCase(deg1) Like UCase(deg2) & "*" Then将会像您一样工作从deg1类型

开始声明Range

但声明更清晰,更清晰:

Dim deg1 As Range, deg2 As String

或     Dim deg1 As String,deg2 As String

但在后一种情况下,你必须改变所有:

Set deg1 = Cells(sat,...)

deg1 = Cells(sat, ...)

最后,您希望避免重复一串代码行,这些代码行针对微小的细节而有所不同,并采用更模块化的方法(使用帮助器SubFunction s)或者至少只重复该代码一次

以上所有内容都反映在以下重构代码中:

Option Explicit

Private Sub CommandButton2_Click()

    Dim s As Long
    Dim deg1 As String, deg2 As String
    Dim colIndex As String
    Dim cell As Range

    With Me
        If .TextBox7.Value = "" Then
            MsgBox "Please enter a value", vbExclamation
            .TextBox7.SetFocus
            Exit Sub
        End If

        If .ComboBox3.Value = "" Or .ComboBox3.Value = "-" Then
            MsgBox "Choose a filter field", vbExclamation
            .ComboBox3.SetFocus
            Exit Sub
        End If

        deg2 = .TextBox7.Value
        Select Case ComboBox3.Value
            Case "Commune"
                colIndex = "A"
            Case "Client"
                colIndex = "B"
            Case "Year"
                colIndex = "D"
        End Select
    End With

    If colIndex <> "" Then
        With Worksheets("MyDataWorksheetName") '<--| reference your "data" worksheet (change "MyDataWorksheetName" to your actual "data" sheet name
            With .Range(.Cells(1, colIndex), .Cells(.Rows.count, colIndex).End(xlUp)) '<--| reference its column 'colIndex' cells from row 1 down to last not empty row
                .AutoFilter field:=1, Criteria1:=UCase(deg2) & "*" '<--| filter its 1st column (i.e. the only 'columnIdex' column) with 'UCase(deg2) & "*"'
                If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if any cell filtered other than header
                    For Each cell In .Resize(.Rows.count - 1, 1).Offset(1).SpecialCells(xlCellTypeVisible) '<--|loop through filtered cells in 1st column (i.e. column "K")
                        Me.ListBox1.AddItem
                        Me.ListBox1.list(s, 0) = Cells(cell.row, "D")
                        Me.ListBox1.list(s, 1) = Cells(cell.row, "E")
                        Me.ListBox1.list(s, 2) = Cells(cell.row, "F")
                        Me.ListBox1.list(s, 3) = Cells(cell.row, "G")
                        Me.ListBox1.list(s, 4) = Cells(cell.row, "H")
                        Me.ListBox1.list(s, 5) = Cells(cell.row, "I")
                        Me.ListBox1.list(s, 6) = Cells(cell.row, "J")
                        Me.ListBox1.list(s, 7) = Cells(cell.row, "K")
                        s = s + 1
                     Next cell
                End If
            End With
            .AutoFilterMode = False
        End With
    End If

End Sub