简体   繁体   English

VBA搜索Textbox1的工作表,如果找到TB1,请检查TB2文本是否相邻

[英]VBA Search a worksheet for Textbox1, if TB1 is found check if TB2 text is adjacent

I am updating this in case anyone else out there may have a need for it and to see if anyone has any suggestions to this code (I am very new to VBA so any critiques are welcome). 我正在更新这个,以防其他人可能需要它,并看看是否有人对此代码有任何建议(我对VBA很新,所以欢迎任何批评)。

The following code will search Column 2 of a giving worksheet for the textBox1 value. 以下代码将在给定工作表的第2列中搜索textBox1值。 If not found it will add the information. 如果没有找到,它将添加信息。 If it is found, it finds all instances of said find and searches the adjacent column for Textbox 2. If not found it adds the information, if it is found it returns a Msgbox. 如果找到它,它会找到所述查找的所有实例并搜索文本框2的相邻列。如果未找到,则添加信息,如果找到则返回一个消息框。

`Private Sub cbAdd_Click()
Dim wss As Worksheet
Dim LR As Long
Dim Fnd As String, FF As String, Change As String
Dim FC As Range, LC As Range, Rng As Range, ChgId As Range
Dim FndChg As Range, SRange As Range, Rng2 As Range

'tb = Text Box, cb = Combo Box
'These are the values that need to be found, if present.
mis = tbMis.Text
Change = tbChg.Text

'Activate the Sheet to search in, then set the search criteria.

ThisWorkbook.Sheets("DB").Activate
Set wss = ThisWorkbook.Sheets("DB") 'wss for me is worksheet searched
Set SRange = wss.Columns(2) 'Sets the range to search with SRange as Column  B of "DB"
Set LC = SRange.Cells(SRange.Cells.count) 'Finds the LastCell (LC) of the  search range
Set FC = SRange.Find(what:=mis, after:=LC) 'FC is the First Cell found matching "mis"
LR = Cells(Rows.count, "A").End(xlUp).Row 'Finds the last row in the "DB"   worksheet, used when adding the information.

'Checking to see if anything was found.
If FC Is Nothing Then
    GoTo AddMis 'If the mis is not found, add the information.
End If

If Not FC Is Nothing Then 'If mis was found FF (First Found) is the address  of where it was found.
    FF = FC.Address
End If

Set Rng = FC

'This loops the search until it finds all instances of mis in column 2.
Do Until FC Is Nothing
Set FC = SRange.FindNext(after:=FC) 'Continues the search after the last    found cell.
Set Rng = Union(Rng, FC) 'Adds the found cells to my range "Rng".
If FC.Address = FF Then Exit Do 'continues the loop until it cycles to the  first found cell.
Loop

Rng.Select
Selection.Offset(0, 1).Select 'Selects adjacent cells in order to see if these match "change".
Set ChgId = Selection.Find(what:=Change, Lookat:=True) 'Will compare Column 3 against info input into "change"

If Not ChgId Is Nothing Then
    GoTo Duplicate
Else
    GoTo AddMis
End If

'Handlers
AddMis:
Sheets("DB").Range("A" & LR + 1).Value = tbSat.Text 'Adds the ComboBox1  selection to the next available row in column 1.
Sheets("DB").Range("A" & LR + 1).Offset(0, 1).Value = tbMis.Text 'Adds tbMis  Text to the same row in column 2.
Sheets("DB").Range("A" & LR + 1).Offset(0, 2).Value = tbChg.Text 'Adds tbChg Text to the same row in column 3.
Sheets("DB").Range("A" & LR + 1).Offset(0, 3).Value = tbPri.Text 'Adds ComboBox2 selection to the same row in column 4.

Msgbox "Information added" 'Lets the user know the information has been added.
Unload Me 'Closes the Userform with the input fields
Exit Sub
Duplicate:
Msgbox "Information has already been input into the database." 'Lets the   user know that the information already exists.
Unload Me
Exit Sub

End Sub'

I would like to thank the entire overflow community, although there may not have been a definitive answer on this question, the site and knowledge base of others available helped me ultimately solve this problem. 我要感谢整个溢出社区,尽管在这个问题上可能没有明确的答案,其他可用的网站和知识库帮助我最终解决了这个问题。

You can use the .Offset property to access cells relative to a certain cell: 您可以使用.Offset属性访问相对于特定单元格的单元格:

'...
If Not Rng Is Nothing Then
    If Rng.Offset(0,1).Value = Findchange Then
        MsgBox "Item already entered." 
    End If
Else
'...

You can't do both checks within one line ( Not Rng Is Nothing And Rng.Offset(0,1).Value = Findchange ) because VBA will check the second condition even if the first isn't true. 你不能在一行内做两个检查( Not Rng Is Nothing And Rng.Offset(0,1).Value = Findchange )因为即使第一个不是真的,VBA也会检查第二个条件。

You can also search column 1 instead of the whole sheet: 您还可以搜索第1列而不是整个工作表:

Set Rng = ws.Columns(1).Find('...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM