bloodmilksky
Board Regular
- Joined
- Feb 3, 2016
- Messages
- 202
Hi Guys,
I am currently running the below code on my spreadsheet. which is working perfectly other than its returning too many values on some searches and was wondering if anyone knew how to reduce the results to the first 10 answers?
Many thanks
Jamie
Private Sub Worksheet_Change(ByVal Target As Range)
' Defines variables
Dim FindString As String, Rng As Range, sRange As Range, RowNo As Integer
' Defines LastRow as last row of column C of the Sales Rep sheet containing data
LastRow = Sheets("Sales Reps").Cells(Rows.Count, "C").End(xlUp).Row
' If target cell is A1 then...
If Not Intersect(Target, Range("A2")) Is Nothing Then
' If target value is not blank then...
If Target.Value <> "" Then
' Sets FindString as the value of A1 (case insensitive)
FindString = UCase(Target.Value)
' Sets search range as Sales Rep sheet C1 to last row
Set sRange = Sheets("Sales Reps").Range("C1:C" & LastRow)
' Set variable RowNo as 1
RowNo = 2
' For each cell in the search range
For Each Cell In sRange
' If the cell contains the FindString value (case insensitive) then...
If InStr(1, UCase(Cell.Value), FindString) Then
' Copy columns A:D of the cell row from Sales Rep and paste to the current RowNo of column B of Menu
Sheets("Sales Reps").Range("C" & Cell.Row, "G" & Cell.Row).Copy Range("B" & RowNo)
' Increase RowNo by 1 to account for the new data
RowNo = RowNo + 1
End If
' Check next cell in search range
Next Cell
' If the name was not found then...
If Range("B2") = "" Then
' Display an error stating the name is not in the list
MsgBox "Specified name does not exist", vbOKOnly, "Attention!"
' Clear the contents of A1
Target.ClearContents
' Reselect cell A1
Range("A2").Select
End If
' Else if A1 is empty...
Else
' Clear the contents of B:E on the Menu sheet
Range("B2:F11").ClearContents
End If
End If
I am currently running the below code on my spreadsheet. which is working perfectly other than its returning too many values on some searches and was wondering if anyone knew how to reduce the results to the first 10 answers?
Many thanks
Jamie
Private Sub Worksheet_Change(ByVal Target As Range)
' Defines variables
Dim FindString As String, Rng As Range, sRange As Range, RowNo As Integer
' Defines LastRow as last row of column C of the Sales Rep sheet containing data
LastRow = Sheets("Sales Reps").Cells(Rows.Count, "C").End(xlUp).Row
' If target cell is A1 then...
If Not Intersect(Target, Range("A2")) Is Nothing Then
' If target value is not blank then...
If Target.Value <> "" Then
' Sets FindString as the value of A1 (case insensitive)
FindString = UCase(Target.Value)
' Sets search range as Sales Rep sheet C1 to last row
Set sRange = Sheets("Sales Reps").Range("C1:C" & LastRow)
' Set variable RowNo as 1
RowNo = 2
' For each cell in the search range
For Each Cell In sRange
' If the cell contains the FindString value (case insensitive) then...
If InStr(1, UCase(Cell.Value), FindString) Then
' Copy columns A:D of the cell row from Sales Rep and paste to the current RowNo of column B of Menu
Sheets("Sales Reps").Range("C" & Cell.Row, "G" & Cell.Row).Copy Range("B" & RowNo)
' Increase RowNo by 1 to account for the new data
RowNo = RowNo + 1
End If
' Check next cell in search range
Next Cell
' If the name was not found then...
If Range("B2") = "" Then
' Display an error stating the name is not in the list
MsgBox "Specified name does not exist", vbOKOnly, "Attention!"
' Clear the contents of A1
Target.ClearContents
' Reselect cell A1
Range("A2").Select
End If
' Else if A1 is empty...
Else
' Clear the contents of B:E on the Menu sheet
Range("B2:F11").ClearContents
End If
End If