如何在userform中查找Startdate - Enddate,并在所有列中搜索

时间:2014-02-04 03:41:55

标签: excel excel-vba vba

我是新来的,这是我的第一篇文章。我是excel vba的新手,我正在寻求你的帮助。

我有一个包含搜索表单的文件。我需要2个项目的帮助:

Sheets("Production")我有Columns A-J中的数据。 第一列是一个日期,我想在表单中选择StartDateEndDate搜索。
例如,我在表单中的日期从01/01/201408/02/2014,我想在它们之间选择StartDateEndDate

另一个是在Textbox中搜索单词。现在它只在Column A上,我希望它在所有使用过的Columns上。 但是当我将代码更改为Columns A-L时,它只会在列表框中向我显示从Cell到结尾而不是从Column A到结尾的数据。
提前谢谢。

以下是代码:

Private Sub TitleBox_Change()
    Dim ws As Worksheet
    ListBox.Clear
    Locate TitleBox.Text, Worksheets("production").Range("a:a")
    If ListBox.ListCount = 0 Then
        ListBox.AddItem "-"
        ListBox.List(0, 1) = ""
        ListBox.List(0, 2) = ""
        ListBox.List(0, 3) = ""
        ListBox.List(0, 4) = ""
        ListBox.List(0, 5) = ""
        ListBox.List(0, 6) = ""
        ListBox.List(0, 7) = ""
        ListBox.List(0, 8) = ""
        ListBox.List(0, 9) = ""
    End If
End Sub

Sub Locate(Name As String, Data As Range)
    Dim rngFind As Range
    Dim strFirstFind As String

    On Error Resume Next

    With Data
        Set rngFind = .Find(Name, LookIn:=xlValues, lookat:=xlPart)
        If Not rngFind Is Nothing Then
            strFirstFind = rngFind.Address
            Do
                If rngFind.Row > 1 Then
                    ListBox.AddItem rngFind.Value
                    ListBox.List(ListBox.ListCount - 1, 0) = rngFind.Cells(1, "a")
                    ListBox.List(ListBox.ListCount - 1, 1) = rngFind.Cells(1, "b")
                    ListBox.List(ListBox.ListCount - 1, 2) = rngFind.Cells(1, "c")
                    ListBox.List(ListBox.ListCount - 1, 3) = rngFind.Cells(1, "d")
                    ListBox.List(ListBox.ListCount - 1, 4) = rngFind.Cells(1, "e")
                    ListBox.List(ListBox.ListCount - 1, 5) = rngFind.Cells(1, "f")
                    ListBox.List(ListBox.ListCount - 1, 6) = rngFind.Cells(1, "g")
                    ListBox.List(ListBox.ListCount - 1, 7) = rngFind.Cells(1, "h")
                    ListBox.List(ListBox.ListCount - 1, 8) = rngFind.Cells(1, "i")
                    ListBox.List(ListBox.ListCount - 1, 9) = rngFind.Cells(1, "j")
                End If
            Set rngFind = .FindNext(rngFind)
            Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
        End If
    End With
End Sub

Private Sub UserForm_Initialize()
    TextBox12.Text = Sheets("main").Range("L2").Text
    TextBox12.Value = Format(TextBox12.Value, "mm-dd-yyyy")
    TextBox13.Text = Sheets("main").Range("L3").Text
    TextBox13.Value = Format(TextBox13.Value, "mm-dd-yy")
End Sub

1 个答案:

答案 0 :(得分:1)

这是你在尝试的吗?

<强>代码

Private Sub UserForm_Initialize()        
    TextBox12.Value = Format(Sheets("main").Range("L2").Value, "mm-dd-yyyy")
    TextBox13.Value = Format(Sheets("main").Range("L3").Value, "mm-dd-yyyy")
End Sub

Private Sub MenuButton_Click()
    Unload Me
End Sub

Sub Locate(Name As String, Data As Range)
    Dim rngFind As Range
    Dim strFirstFind As String

    With Data
        Set rngFind = .Find(Name, LookIn:=xlValues, lookat:=xlPart)
        If Not rngFind Is Nothing Then
            strFirstFind = rngFind.Address
            Do
                If rngFind.Row > 1 Then
                    ListBox.AddItem .Cells(rngFind.Row, 1)
                    ListBox.List(ListBox.ListCount - 1, 2) = .Cells(rngFind.Row, 2)
                    ListBox.List(ListBox.ListCount - 1, 3) = .Cells(rngFind.Row, 3)
                    ListBox.List(ListBox.ListCount - 1, 4) = .Cells(rngFind.Row, 4)
                    ListBox.List(ListBox.ListCount - 1, 5) = .Cells(rngFind.Row, 5)
                    ListBox.List(ListBox.ListCount - 1, 6) = .Cells(rngFind.Row, 6)
                    ListBox.List(ListBox.ListCount - 1, 7) = .Cells(rngFind.Row, 7)
                    ListBox.List(ListBox.ListCount - 1, 8) = .Cells(rngFind.Row, 8)
                    ListBox.List(ListBox.ListCount - 1, 9) = .Cells(rngFind.Row, 9)                     
                End If
                Set rngFind = .FindNext(rngFind)
            Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
        End If
    End With

End Sub

Private Sub TitleBox_Change()
    Dim ws As Worksheet

    ListBox.Clear

    Locate TitleBox.Text, Worksheets("production").Range("a:j")

    If ListBox.ListCount = 0 Then
        ListBox.AddItem "-"
        ListBox.List(0, 1) = ""
        ListBox.List(0, 2) = ""
        ListBox.List(0, 3) = ""
        ListBox.List(0, 4) = ""
        ListBox.List(0, 5) = ""
        ListBox.List(0, 6) = ""
        ListBox.List(0, 7) = ""
        ListBox.List(0, 8) = ""
        ListBox.List(0, 9) = ""
    End If
End Sub

<强>截图:

enter image description here