Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Find function in VBA ?

Hello,

I'm new to VBA and wondered if someone could help me out please, as I
don't really know my arse from my elbow ?

I've got an Excel spreadsheet with 2 sheets.

Sheet1 only has values in the first column (A1 to A50)
Sheet2 has values over a larger range (A1 to K500)

I need to be able to look at the value in Sheet1 A1 and search for it
in the range A1 to K500 on Sheet 2.

If the value appears anywhere on Sheet 2 I need to highlight the cell
(on sheet 2) somehow and also insert "found" next to the value (in
column B) on Sheet1.

If the value does not appear on Sheet 2 anywhere I need to check the
value in Sheet1 A2 against the range on Sheet2 . . . .and so on (Hope
this makes sense)

Is this possible ?

Any help or advice would really be appreciated

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Find function in VBA ?


Sub Macro1()
'
Dim lngA As Long
Dim lngB As Long
Dim varFind
Dim strFirstAddress As String

Worksheets("blad1").Activate
For lngA = 1 To Worksheets("blad1").UsedRange.Rows.Count
lngB = 2
With Worksheets("blad2").UsedRange
Set varFind = .Find(Worksheets("blad1").Cells(lngA, 1).Value,
LookIn:=xlValues)
If Not varFind Is Nothing Then
strFirstAddress = varFind.Address
..Range(varFind.Address).Interior.ColorIndex = 36
Cells(lngA, lngB) = strFirstAddress
lngB = lngB + 1
Set varFind = .FindNext(varFind)
Do While Not varFind Is Nothing And varFind.Address <
strFirstAddress
..Range(varFind.Address).Interior.ColorIndex = 36
Cells(lngA, lngB) = varFind.Address
lngB = lngB + 1
Set varFind = .FindNext(varFind)
Loop
End If
End With
Next lngA
'
End Sub


--
H.A. de Wilde
------------------------------------------------------------------------
H.A. de Wilde's Profile: http://www.excelforum.com/member.php...o&userid=30679
View this thread: http://www.excelforum.com/showthread...hreadid=539087

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 104
Default Find function in VBA ?

Good Afternoon

here a bit of code you can try for you project, any question posted me
back...
enjoy,

Rick, (Fairbanks, AK, Land of the Midnight Sun )

Sub FindM()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim rng1 As Range, rng2 As Range
Dim fndRng As Range, cell As Range
Dim firstAdd As String


Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
'Set rng1 = ws1.Range("A1:A50")
Set rng1 = ws1.Range("A1:A" & Range("A1").End(xlDown).Row)
Set rng2 = ws2.Range("A1:K500")

ws1.Activate
For Each cell In rng1
Set fndRng = rng2.Find(what:=cell.Value, _
LookIn:=xlFormulas, searchorder:=xlByRows)
If Not fndRng Is Nothing Then
firstAdd = fndRng.Address
Do
fndRng.Interior.Color = RGB(250, 250, 0)
cell.Offset(0, 1).Value = "Found"
If cell.Offset(0, 2).Value = "" Then
cell.Offset(0, 2) = fndRng.Address
Else
cell.Offset(0, 2) = cell.Offset(0, 2) & "," & fndRng.Address
End If
Set fndRng = rng2.FindNext(fndRng)
Loop While (fndRng.Address < firstAdd)
Else
cell.Offset(0, 1).Value = "Not Found"
End If
Next cell

End Sub



"*******_kestrel" wrote in message
ups.com...
Hello,

I'm new to VBA and wondered if someone could help me out please, as I
don't really know my arse from my elbow ?

I've got an Excel spreadsheet with 2 sheets.

Sheet1 only has values in the first column (A1 to A50)
Sheet2 has values over a larger range (A1 to K500)

I need to be able to look at the value in Sheet1 A1 and search for it
in the range A1 to K500 on Sheet 2.

If the value appears anywhere on Sheet 2 I need to highlight the cell
(on sheet 2) somehow and also insert "found" next to the value (in
column B) on Sheet1.

If the value does not appear on Sheet 2 anywhere I need to check the
value in Sheet1 A2 against the range on Sheet2 . . . .and so on (Hope
this makes sense)

Is this possible ?

Any help or advice would really be appreciated

Thanks



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Find function in VBA ?

Brilliant - Thanks for your help folks !

Quick question Rick - If I wanted to expand or reduce the search range
on Sheet 1 (currently A1:A50) how would I alter it in your example ?

Cheers

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 104
Default Find function in VBA ?

Good Morning,
What I think your trying to ask me is how do determine the range of the
search list if it expands or contract on Sheet1. The way I wrote the code
automatically determines the size of the range for you, ie using this line
code:
Set rng1 = ws1.Range("A1:A" & Range("A1").End(xlDown).Row)
Excel starts at Cell A1 and Searchs Downward in Column A until finds the
first empty cell, thus determine the last row of the range. So if you to
the add or delete rows from the bottom of your range the code will always
determine lastrow of the range for you. Let me caution you, do not leave
empty cells in Column A, when excel finds the first empty cell it things it
at the end of the range. Thus giving a incorrect range size.
If you noticed I remarked out the line of : 'Set rng1 =
ws1.Range("A1:A50"), I let excel determine the range list size in line code
below it. The same line code I mentioned above. I hope this answered your
questiion. Good Luck on your project....

Rick, (Fbks, AK)


"*******_kestrel" wrote in message
oups.com...
Brilliant - Thanks for your help folks !

Quick question Rick - If I wanted to expand or reduce the search range
on Sheet 1 (currently A1:A50) how would I alter it in your example ?

Cheers





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Find function in VBA ?

Cheers for the explanation Rick

Thanks for all your help - you are a star

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to find a function. MikeCampbell Excel Worksheet Functions 2 January 29th 07 01:40 AM
Find Function Guy Lydig Excel Discussion (Misc queries) 2 May 19th 06 07:19 PM
FIND function? Sonya Excel Discussion (Misc queries) 3 October 22nd 05 04:53 AM
Help with the FIND function Ranger Excel Worksheet Functions 1 February 25th 05 03:24 PM
backwards find function to find character in a string of text Ashleigh K. Excel Programming 1 January 14th 04 04:36 PM


All times are GMT +1. The time now is 07:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"