You are on page 1of 7

CODE 1

Private Sub Search_Click() Dim r As Range, rAll As Range, lRow As Long Dim sTerm As String, ctl As Control

Application.ScreenUpdating = False

sTerm = txtDisplay.Value

UserForm1.txtDisplay.Value = ""

With Sheets("Sheet1") Set rAll = .Range(.Cells(1, 4), .Cells(1, 4).End(xlDown)) For Each r In rAll If InStr(r, sTerm) Then lRow = r.Row For Each ctl In Frame2.Controls If TypeName(ctl) = "TextBox" Then If ctl.Tag <> "" Then ctl.ControlSource = ctl.Tag & lRow End If End If Next ctl End If Next r End With UserForm1.txtDisplay.SetFocus End Sub

CODE 1 UPDATED

Sub btnSearch_Click()

Dim r As Range, rAll As Range Dim sTerm As String

Application.ScreenUpdating = False

sTerm = txtDisplay.Value

UserForm1.txtDisplay.Value = ""

With Sheets("Sheet1") Set rAll = .Range(.Cells(1, 3), .Cells(1, 3).End(xlDown)) For Each r In rAll If InStr(r, sTerm) Then With CRLInput

txtStoreNo = r.Offset(0, -2) txtDateSale = r.Offset(0, -1) txtOrderNo = r txtEmployee = r.Offset(0, 1) txtDateMailed = r.Offset(0, 2) txtTimeMailed = r.Offset(0, 3) txtComments = r.Offset(0, 4) End With End If Next r End With UserForm1.txtDisplay.SetFocus

End Sub

Private Sub CommandButton5_Click() Dim RowCount As Long Dim ctl As Control

RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count With Worksheets("Sheet1").Range("A1") .Offset(RowCount, 0).Value = Me.txtStoreNo.Value .Offset(RowCount, 1).Value = Me.txtDateSale.Value .Offset(RowCount, 2).Value = Me.txtOrderNo.Value .Offset(RowCount, 3).Value = Me.txtEmployee.Value .Offset(RowCount, 4).Value = Me.txtDateMailed.Value .Offset(RowCount, 5).Value = Me.txtTimeMailed.Value .Offset(RowCount, 6).Value = Me.txtComments.Value

End With 'Clear the form For Each ctl In Me.Controls If TypeName(ctl) = "TextBox" Or TypeName(ctl) = "ComboBox" Then ctl.Value = "" ElseIf TypeName(ctl) = "CheckBox" Then ctl.Value = False End If Next ctl End Sub

[/HTML]

CODE 2
UserForm setup: 12 TextBoxes 3 named: tbSrch1 thru tbSrch3 (what youre searching for) 9 named: tbResCol1 thru tbResCol9 (populates with ListBox selection to allow edits) 1 ListBox Named: lbResList ColumnWidths: Enter at minimum 1 zero this is to hide the first column which we use for coding. You can also enter addition column widths separate by commas to adjust the remaining columns. You may also want the Headers to show in the ListBox set ColumnHeads to True 2 CommandButtons 1 named: buttSrch 1 named: buttUpdate Places all code in UserForm. In a few places note the change to suite comments and change as needed. Let me know how you make out.

Option Explicit Dim rgData As Range Dim rgResults As Range Dim ListRow As Long Dim SkipEvent As Boolean Dim shData As Worksheet Private Dim Dim Dim Dim Dim Dim Dim Dim Sub buttSrch_Click() shCurrent As Worksheet shResults As Worksheet found As Range firstFound As String SrchCol_1 As String SrchCol_2 As String SrchCol_3 As String r As Long

If tbSrch1 = "" And tbSrch2 = "" And tbSrch3 = "" Then Exit Sub Set shData = Sheets("Data") 'change to suit Set rgData = shData.Cells.CurrentRegion Set rgData = rgData.Offset(1, 0).Resize(rgData.Rows.Count - 1, rgData.Columns.Count) Set shCurrent = ActiveSheet Application.ScreenUpdating = False Application.DisplayAlerts = False On Error Resume Next Sheets("Results").Delete On Error GoTo 0 Application.DisplayAlerts = True Sheets.Add after:=Sheets(Sheets.Count) ActiveSheet.Name = "Results" Set shResults = Sheets("Results") With shResults .Cells(1, 1) = "DataRow"

.Cells(1, .Cells(1, .Cells(1, .Cells(1, .Cells(1, .Cells(1, .Cells(1, .Cells(1, .Cells(1, End With 'columns to SrchCol_1 = SrchCol_2 = SrchCol_3 =

2) = "Header 1" 'change to suit 3) = "Header 2" 4) = "Header 3" 5) = "Header 4" 6) = "Header 5" 7) = "Header 6" 8) = "Header 7" 9) = "Header 8" 10) = "Header 9"

search thru - change to suit "A" "D" "C"

lbResList.ListIndex = -1 tbResCol1 = "" tbResCol2 = "" tbResCol3 = "" tbResCol4 = "" tbResCol5 = "" tbResCol6 = "" tbResCol7 = "" tbResCol8 = "" tbResCol9 = "" r = 1 If tbSrch1 <> "" Then With rgData.Columns(SrchCol_1) Set found = .Find(tbSrch1, rgData.Cells(rgData.Rows.Count, SrchCol_1)) If Not found Is Nothing Then firstFound = found.Address Do r = r + 1 found.EntireRow.Copy shResults.Cells(r, 1) shResults.Cells(r, 1).Insert Shift:=xlToRight shResults.Cells(r, 1) = found.Row Set found = .FindNext(found) Loop While Not found Is Nothing And found.Address <> firstFound End If End With End If If tbSrch2 <> "" Then With rgData.Columns(SrchCol_2) Set found = .Find(tbSrch2, rgData.Cells(rgData.Rows.Count, SrchCol_2)) If Not found Is Nothing Then firstFound = found.Address Do r = r + 1 found.EntireRow.Copy shResults.Cells(r, 1) shResults.Cells(r, 1).Insert Shift:=xlToRight shResults.Cells(r, 1) = found.Row Set found = .FindNext(found) Loop While Not found Is Nothing And found.Address <> firstFound End If End With End If If tbSrch3 <> "" Then With rgData.Columns(SrchCol_3) Set found = .Find(tbSrch3, rgData.Cells(rgData.Rows.Count, SrchCol_3)) If Not found Is Nothing Then firstFound = found.Address Do r = r + 1 found.EntireRow.Copy shResults.Cells(r, 1) shResults.Cells(r, 1).Insert Shift:=xlToRight shResults.Cells(r, 1) = found.Row Set found = .FindNext(found) Loop While Not found Is Nothing And found.Address <> firstFound End If End With End If

If r = 1 Then lbResList.RowSource = "" MsgBox "No Results" Else Set rgResults = shResults.Cells.CurrentRegion Set rgResults = rgResults.Offset(1, 0).Resize(rgResults.Rows.Count - 1, rgResults.Columns.Count) rgResults.RemoveDuplicates Columns:=Array(1), Header:=xlNo Set rgResults = shResults.Cells.CurrentRegion Set rgResults = rgResults.Offset(1, 0).Resize(rgResults.Rows.Count - 1, rgResults.Columns.Count) ActiveWorkbook.Names.Add Name:="rgResults", RefersTo:=rgResults lbResList.RowSource = "rgResults" End If shCurrent.Activate Application.ScreenUpdating = True End Sub

Private Sub buttUpdate_Click() Dim DataRow As Long On Error Resume Next DataRow = lbResList.List(lbResList.ListIndex, 0) On Error GoTo 0 If DataRow = 0 Then Exit Sub SkipEvent = True If tbResCol1 = "" And tbResCol2 = "" And tbResCol3 = "" And _ tbResCol4 = "" And tbResCol5 = "" And tbResCol6 = "" And _ tbResCol7 = "" And tbResCol8 = "" And tbResCol9 = "" Then If MsgBox("Delete Entire Record?", vbExclamation + vbYesNo, "Confirm") = vbNo Then Exit Sub Else shData.Rows(DataRow).EntireRow.Delete ListRow = lbResList.ListIndex + 1 rgResults.Rows(ListRow).EntireRow.Delete End If Else If MsgBox("Do updates?", vbExclamation + vbYesNo, "Confirm") = vbNo Then Exit Sub Else With shData .Cells(DataRow, 1) = tbResCol1 .Cells(DataRow, 2) = tbResCol2 .Cells(DataRow, 3) = tbResCol3 .Cells(DataRow, 4) = tbResCol4 .Cells(DataRow, 5) = tbResCol5 .Cells(DataRow, 6) = tbResCol6 .Cells(DataRow, 7) = tbResCol7 .Cells(DataRow, 8) = tbResCol8 .Cells(DataRow, 9) = tbResCol9 End With With rgResults ListRow = lbResList.ListIndex + 1 .Cells(ListRow, 2) = tbResCol1 .Cells(ListRow, 3) = tbResCol2 .Cells(ListRow, 4) = tbResCol3 .Cells(ListRow, 5) = tbResCol4 .Cells(ListRow, 6) = tbResCol5 .Cells(ListRow, 7) = tbResCol6 .Cells(ListRow, 8) = tbResCol7 .Cells(ListRow, 9) = tbResCol8 .Cells(ListRow, 10) = tbResCol9 End With End If End If SkipEvent = False End Sub

Private Sub lbResList_Click() If SkipEvent Then Exit Sub With lbResList ListRow = .ListIndex tbResCol1 = .List(ListRow, tbResCol2 = .List(ListRow, tbResCol3 = .List(ListRow, tbResCol4 = .List(ListRow, tbResCol5 = .List(ListRow, tbResCol6 = .List(ListRow, tbResCol7 = .List(ListRow, tbResCol8 = .List(ListRow, tbResCol9 = .List(ListRow, tbResCol9 = .List(ListRow, 9) End With End Sub

1) 2) 3) 4) 5) 6) 7) 8) 9)

You might also like