Search Function

mrkambo

New Member
Joined
May 7, 2017
Messages
23
Hi

Im struggling to understand why this doesn't work, could someone take a look and advise please

Code:
On Error GoTo error
Dim SearchString As String
Dim Lastrow As Long
Dim ans As String
Dim SearchRange As Range
Dim Lastrowa As Long


Const prefix As String = "COLL"
ans = TextBox1.Value
SearchString = prefix & ans
Lastrow = Sheets("Source").Cells(Rows.Count, "J").End(xlUp).Row
Lastrowa = Sheets(ActiveSheet.Name).Cells(Rows.Count, "A").End(xlUp).Row + 1


Set SearchRange = Sheets("Source").Range("J7:J").Find(SearchString)
Sheets(ActiveSheet.Name).Range("A" & Lastrowa).Value = SearchRange.Offset(, -2).Value
Sheets(ActiveSheet.Name).Range("B" & Lastrowa).Value = SearchRange.Offset(, -3).Value
Sheets(ActiveSheet.Name).Range("C" & Lastrowa).Value = SearchRange.Offset(, -5).Value
Sheets(ActiveSheet.Name).Range("D" & Lastrowa).Value = SearchRange.Offset(, -1).Value
Sheets(ActiveSheet.Name).Range("E" & Lastrowa).Value = SearchRange.Offset(, -9).Value
Sheets(ActiveSheet.Name).Range("F" & Lastrowa).Value = SearchRange.Offset(, 6).Value
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Doesnt work is a bit vague but this line is definately wrong:

Code:
Set SearchRange = Sheets("Source").Range("J7:J").Find(SearchString)

Id say its:

Code:
Set SearchRange = Sheets("Source").Range("J7:J" & Lastrow).Find(SearchString)

You shouldnt really use find like this either. Find, i believe, remembers its last used settings so could produce unintended results. You should give it it's parameters to be sure.
 
Upvote 0
Doesnt work is a bit vague but this line is definately wrong:

Code:
Set SearchRange = Sheets("Source").Range("J7:J").Find(SearchString)

Id say its:

Code:
Set SearchRange = Sheets("Source").Range("J7:J" & Lastrow).Find(SearchString)

You shouldnt really use find like this either. Find, i believe, remembers its last used settings so could produce unintended results. You should give it it's parameters to be sure.

I completely missed that! Thanks, ill go back into it and see where its hiccuping

Update:

Works an absolute treat
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top