2016-07-27 2 views
1

これを更新する予定がある場合は、他の誰かがそれを必要としている可能性があります。 VBAにはとても新しいので、どんな批評も大歓迎です)。VBA TB1が見つかった場合、Textbox1のワークシートを検索するTB2テキストが隣接しているかどうかを確認する

次のコードは、textBox1の値を示すワークシートの2列目を検索します。見つからなければ、情報を追加します。見つかった場合は、前記検索のすべてのインスタンスを見つけて、隣接する列のテキストボックス2を検索する。見つからなければ、情報を追加し、見つかった場合は、Msgboxを返す。

`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' 

この質問に明確な答えがあったかもしれないが、私は可能な他のサイトと知識ベースは私が最終的にこの問題を解決する助け、全体のオーバーフローのコミュニティに感謝したいと思います。

あなたが特定のセルに比べてセルにアクセスするために .Offsetプロパティを使用することができます
+0

は、Excelシートからのすべてのこれらのオプションを取得している場合、あなたはVBAを必要としない、あなただけのデータを使用することができます - >削除重複した –

+0

は列 "A"のみで検索される 'Findstring'であり、見つかった場合、列Bの隣のセルが 'Findchange'に対してチェックされていますか?あなたは列 "A"のすべての一致する値をチェックする必要がありますか? "前"シナリオと "後"シナリオを追加してください。 – user3598756

+0

@ShaiRado列AとBがTB1とTB2の値と一致しない場合は、ワークシートにすべての前の項目を保存し、新しい項目を追加する必要があります。 – cparsons

答えて

0

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

VBAでも最初の場合は、2番目の条件をチェックしますので、あなたは、1つのライン(Not Rng Is Nothing And Rng.Offset(0,1).Value = Findchange)内の両方のチェックを行うことはできません本当ではない。

あなたはまた、全体ではなく、シートの1列を検索することができます。

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

ありがとうございます。私はあなたの提案を実装しましたが、今は常に情報を追加するという問題があります。 – cparsons

+0

あなたの例では、項目は列1にありますが、列2を探しています...? – arcadeprecinct

+0

最初に列2の値を見つけて、TB1と一致するかどうかを調べる必要があります。一致すれば、TB2が隣接する値と一致するかどうかを調べる必要があります。そうでない場合は、次の利用可能な行にTB値を追加します。 – cparsons

関連する問題