用户窗体搜索两个条件,然后将行的数据粘贴到用户窗体文本框
Userform to search for two criteria, then paste row's data to userform textboxes
我收到 运行 一次错误“13”:下面标记的其中一行的类型不匹配。我希望能够有一个用户窗体,您可以在其中键入两个条件,然后它将搜索具有这两个条件的行并将相应的单元格值粘贴到 11 个用户窗体文本框。我不确定为什么它给我这条线的错误,或者是否有更好的方法来做到这一点。
Private Sub CommandButton1_Click()
txt1.Visible = True
txt2.Visible = True
txt3.Visible = True
txt4.Visible = True
txt5.Visible = True
txt6.Visible = True
txt7.Visible = True
txt8.Visible = True
txt9.Visible = True
txt10.Visible = True
txt11.Visible = True
Dim ws As Worksheet
Set ws = Sheets("The Goods")
ws.Activate
Dim SearchSearch As Variant
SearchSearch = txtsearch.Value
Dim SearchName As Variant
SearchName = txtname.Value
If Trim(txtsearch.Value) = "" Then
MsgBox "Search can't be left blank.", vbOKOnly + vbInformation, "Search"
End If
If Trim(txtname.Value) = "" Then
MsgBox "Name can't be left blank.", vbOKOnly + vbInformation, "Name"
End If
Dim FirstAddress As String, cF As Range
With ThisWorkbook.Sheets("The Goods").Range("D:D") 'txtsearch will be in the range D:D
Set cF = .Find(What:=SearchSearch, _
after:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False) ' line that is giving me an error
With ThisWorkbook.Sheets("The Goods").Range("B:B") 'txtname will be in the range B:B
Set cF = .Find(What:=SearchName, _
after:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
txt1.Value = cF.(0, 5).Value
txt2.Value = cF(0, 3).Value
txt3.Value = cF(0, 6).Value
txt4.Value = cF(0, 7).Value
txt5.Value = cF(0, 8).Value
txt6.Value = cF(0, 9).Value
txt7.Value = cF(0, 10).Value
txt8.Value = cF(0, 11).Value
txt9.Value = cF(0, 12).Value
txt10.Value = cF(0, 13).Value
txt11.Value = cF(0, 14).Value
End With
End With
End Sub
Private Sub CommandButton3_Click()
Dim iExit As VbMsgBoxResult
iExit = MsgBox("Are you sure you want to exit?", vbQuestion + vbYesNo, "Search System")
If iExit = vbYes Then
Unload Me
End If
End Sub
我会选择这样的东西:
Private Sub CommandButton1_Click()
Dim i As Long, rngB As Range, n As Long, arrB, arrD
Dim ws As Worksheet
Dim SearchSearch As Variant, SearchName As Variant
For i = 1 To 11
Me.Controls("txt" & i).Visible = True
Next i
Set ws = ThisWorkbook.Sheets("The Goods")
ws.Parent.Activate
ws.Activate
SearchSearch = Trim(txtsearch.Value)
SearchName = Trim(txtname.Value)
'check the trimmed values
If Len(SearchSearch) = 0 Or Len(SearchName) = 0 Then
MsgBox "'Search' and 'Name' can't be left blank.", vbOKOnly + vbInformation, "Search"
Exit Sub
End If
'get search ranges
Set rngB = ws.Range(ws.Range("B1"), ws.Cells(ws.Rows.Count, "B").End(xlUp))
Set rngD = rngB.Offset(0, 2)
'pull the values into a couple of arrays for faster searching
arrB = rngB.Value
arrD = rngD.Value
'loop over the arrays
For n = 1 To UBound(arrB, 1)
If arrB(n, 1) = SearchName And arrD(n, 1) = SearchSearch Then
'got a hit - populate your textboxes
Set cF = rngB.Cells(n, 1)
txt1.Value = cF.Offset(0, 1).Value 'Col C same row
txt2.Value = cF.Offset(0, 2).Value 'Col D same row
txt3.Value = cF.Offset(0, 3).Value 'Col E same row
'etc etc
'OR do something like this:
With rngB.Cells(n, 1).EntireRow
txt1.Value = .Cells(1, "C").Value
txt1.Value = .Cells(1, "D").Value
txt1.Value = .Cells(1, "E").Value
'etc etc
End With
Exit For
End If
Next
If cF Is Nothing Then MsgBox "No match!"
End Sub
下面的代码是一个简单的 For Loop
,它循环遍历 Column B
中的每个 cel
并检查 txtname.Value
,并使用偏移量检查是否 [=15] =] 值等于 txtsearch.Value
。如果两者匹配,那么它将将该行的值写入 userform
文本框。您可以将 TextBox1
更改为 txt1
,等等
Private Sub CommandButton1_Click()
Dim ws As Worksheet, cel As Range
Set ws = Sheets("The Goods")
For Each cel In ws.Cells(2, 2).Resize(ws.Cells(Rows.Count, 2).End(xlUp).Row).Cells
If cel.Value = Me.txtname.Value And cel.Offset(, 2).Value = Me.txtsearch.Value Then
Me.TextBox1.Value = cel.Offset(, 3).Value 'Change to your textbox naming scheme
Me.TextBox2.Value = cel.Offset(, 1).Value
Me.TextBox3.Value = cel.Offset(, 4).Value
Me.TextBox4.Value = cel.Offset(, 5).Value
Me.TextBox5.Value = cel.Offset(, 6).Value
Me.TextBox6.Value = cel.Offset(, 7).Value
Me.TextBox7.Value = cel.Offset(, 8).Value
Me.TextBox8.Value = cel.Offset(, 9).Value
Me.TextBox9.Value = cel.Offset(, 10).Value
Me.TextBox10.Value = cel.Offset(, 11).Value
Me.TextBox11.Value = cel.Offset(, 12).Value
End If
Next cel
End Sub
我收到 运行 一次错误“13”:下面标记的其中一行的类型不匹配。我希望能够有一个用户窗体,您可以在其中键入两个条件,然后它将搜索具有这两个条件的行并将相应的单元格值粘贴到 11 个用户窗体文本框。我不确定为什么它给我这条线的错误,或者是否有更好的方法来做到这一点。
Private Sub CommandButton1_Click()
txt1.Visible = True
txt2.Visible = True
txt3.Visible = True
txt4.Visible = True
txt5.Visible = True
txt6.Visible = True
txt7.Visible = True
txt8.Visible = True
txt9.Visible = True
txt10.Visible = True
txt11.Visible = True
Dim ws As Worksheet
Set ws = Sheets("The Goods")
ws.Activate
Dim SearchSearch As Variant
SearchSearch = txtsearch.Value
Dim SearchName As Variant
SearchName = txtname.Value
If Trim(txtsearch.Value) = "" Then
MsgBox "Search can't be left blank.", vbOKOnly + vbInformation, "Search"
End If
If Trim(txtname.Value) = "" Then
MsgBox "Name can't be left blank.", vbOKOnly + vbInformation, "Name"
End If
Dim FirstAddress As String, cF As Range
With ThisWorkbook.Sheets("The Goods").Range("D:D") 'txtsearch will be in the range D:D
Set cF = .Find(What:=SearchSearch, _
after:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False) ' line that is giving me an error
With ThisWorkbook.Sheets("The Goods").Range("B:B") 'txtname will be in the range B:B
Set cF = .Find(What:=SearchName, _
after:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
txt1.Value = cF.(0, 5).Value
txt2.Value = cF(0, 3).Value
txt3.Value = cF(0, 6).Value
txt4.Value = cF(0, 7).Value
txt5.Value = cF(0, 8).Value
txt6.Value = cF(0, 9).Value
txt7.Value = cF(0, 10).Value
txt8.Value = cF(0, 11).Value
txt9.Value = cF(0, 12).Value
txt10.Value = cF(0, 13).Value
txt11.Value = cF(0, 14).Value
End With
End With
End Sub
Private Sub CommandButton3_Click()
Dim iExit As VbMsgBoxResult
iExit = MsgBox("Are you sure you want to exit?", vbQuestion + vbYesNo, "Search System")
If iExit = vbYes Then
Unload Me
End If
End Sub
我会选择这样的东西:
Private Sub CommandButton1_Click()
Dim i As Long, rngB As Range, n As Long, arrB, arrD
Dim ws As Worksheet
Dim SearchSearch As Variant, SearchName As Variant
For i = 1 To 11
Me.Controls("txt" & i).Visible = True
Next i
Set ws = ThisWorkbook.Sheets("The Goods")
ws.Parent.Activate
ws.Activate
SearchSearch = Trim(txtsearch.Value)
SearchName = Trim(txtname.Value)
'check the trimmed values
If Len(SearchSearch) = 0 Or Len(SearchName) = 0 Then
MsgBox "'Search' and 'Name' can't be left blank.", vbOKOnly + vbInformation, "Search"
Exit Sub
End If
'get search ranges
Set rngB = ws.Range(ws.Range("B1"), ws.Cells(ws.Rows.Count, "B").End(xlUp))
Set rngD = rngB.Offset(0, 2)
'pull the values into a couple of arrays for faster searching
arrB = rngB.Value
arrD = rngD.Value
'loop over the arrays
For n = 1 To UBound(arrB, 1)
If arrB(n, 1) = SearchName And arrD(n, 1) = SearchSearch Then
'got a hit - populate your textboxes
Set cF = rngB.Cells(n, 1)
txt1.Value = cF.Offset(0, 1).Value 'Col C same row
txt2.Value = cF.Offset(0, 2).Value 'Col D same row
txt3.Value = cF.Offset(0, 3).Value 'Col E same row
'etc etc
'OR do something like this:
With rngB.Cells(n, 1).EntireRow
txt1.Value = .Cells(1, "C").Value
txt1.Value = .Cells(1, "D").Value
txt1.Value = .Cells(1, "E").Value
'etc etc
End With
Exit For
End If
Next
If cF Is Nothing Then MsgBox "No match!"
End Sub
下面的代码是一个简单的 For Loop
,它循环遍历 Column B
中的每个 cel
并检查 txtname.Value
,并使用偏移量检查是否 [=15] =] 值等于 txtsearch.Value
。如果两者匹配,那么它将将该行的值写入 userform
文本框。您可以将 TextBox1
更改为 txt1
,等等
Private Sub CommandButton1_Click()
Dim ws As Worksheet, cel As Range
Set ws = Sheets("The Goods")
For Each cel In ws.Cells(2, 2).Resize(ws.Cells(Rows.Count, 2).End(xlUp).Row).Cells
If cel.Value = Me.txtname.Value And cel.Offset(, 2).Value = Me.txtsearch.Value Then
Me.TextBox1.Value = cel.Offset(, 3).Value 'Change to your textbox naming scheme
Me.TextBox2.Value = cel.Offset(, 1).Value
Me.TextBox3.Value = cel.Offset(, 4).Value
Me.TextBox4.Value = cel.Offset(, 5).Value
Me.TextBox5.Value = cel.Offset(, 6).Value
Me.TextBox6.Value = cel.Offset(, 7).Value
Me.TextBox7.Value = cel.Offset(, 8).Value
Me.TextBox8.Value = cel.Offset(, 9).Value
Me.TextBox9.Value = cel.Offset(, 10).Value
Me.TextBox10.Value = cel.Offset(, 11).Value
Me.TextBox11.Value = cel.Offset(, 12).Value
End If
Next cel
End Sub