Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Find from one sheet to another

I am trying to write some code for Excel that takes the selected value of a
cell on one sheet and searches for it on a second sheet ("Data") in the same
workbook, but within a specific range around the active cell on the second
sheet. My code selects the range on the second sheet without a problem but
errors when attempting to use the find method. Here's the code:

Sub ToData()

Dim SearchTarget As String
Dim RngTop, RngBot As Integer

SearchTarget = Selection 'active cell on sheet 1

Worksheets("Data").Activate
RngRow = ActiveCell.Row

'demarcates range
RngBot = Cells(RngRow, 3).End(xlDown).Row
RngTop = Cells(RngRow, 3).End(xlUp).Row
Set Rng = Range("A" & RngTop, "A" & RngBot)
Rng.Select
'if the last line containing the Find function is omitted, the range is
selected as intended
'when the following Find is added, an error occurs

Rng.Find(What:=SearchTarget, LookAt:= _
xlPart, SearchDirection:=xlNext, MatchCase:=False).Activate

End Sub

Thanks for the help!

Greg
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Find from one sheet to another

Lots of times, it's better to set a range variable to that foundcell. Then you
can test it later to see if the string was actually found:


Option Explicit
Sub ToData()

Dim SearchTarget As String
Dim RngTop As Long
Dim RngBot As Long
Dim RngRow As Long
Dim Rng As Range
Dim FoundCell As Range

SearchTarget = Selection 'active cell on sheet 1

Worksheets("Data").Activate
RngRow = ActiveCell.Row

'demarcates range
RngBot = Cells(RngRow, 3).End(xlDown).Row
RngTop = Cells(RngRow, 3).End(xlUp).Row
Set Rng = Range("A" & RngTop, "A" & RngBot)
'Rng.Select

'if the last line containing the Find function
'is omitted, the range is selected As intended
'when the following Find is added, an error occurs


With Rng
Set FoundCell = .Cells.Find(What:=SearchTarget, LookAt:=xlPart, _
SearchDirection:=xlNext, _
after:=.Cells(.Cells.Count), _
MatchCase:=False)
End With
If FoundCell Is Nothing Then
MsgBox SearchTarget & " wasn't found in: " & Rng.Address(0, 0)
Else
FoundCell.Activate
'or whatever you want to do.
End If

End Sub


Gregory Turk wrote:

I am trying to write some code for Excel that takes the selected value of a
cell on one sheet and searches for it on a second sheet ("Data") in the same
workbook, but within a specific range around the active cell on the second
sheet. My code selects the range on the second sheet without a problem but
errors when attempting to use the find method. Here's the code:

Sub ToData()

Dim SearchTarget As String
Dim RngTop, RngBot As Integer

SearchTarget = Selection 'active cell on sheet 1

Worksheets("Data").Activate
RngRow = ActiveCell.Row

'demarcates range
RngBot = Cells(RngRow, 3).End(xlDown).Row
RngTop = Cells(RngRow, 3).End(xlUp).Row
Set Rng = Range("A" & RngTop, "A" & RngBot)
Rng.Select
'if the last line containing the Find function is omitted, the range is
selected as intended
'when the following Find is added, an error occurs

Rng.Find(What:=SearchTarget, LookAt:= _
xlPart, SearchDirection:=xlNext, MatchCase:=False).Activate

End Sub

Thanks for the help!

Greg


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Find from one sheet to another

Yes, it works! Thank you very much, Dave.

Happy computing.

Greg

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
how can find sheet on workbook have so many sheet ? Huy Nguyen Excel Worksheet Functions 1 November 22nd 07 08:45 AM
Find data from one sheet in another sheet [email protected] Excel Worksheet Functions 3 August 21st 06 07:08 PM
find a sheet raj Excel Discussion (Misc queries) 2 February 15th 06 10:41 AM
Find A Value In Sheet? BL69 Excel Discussion (Misc queries) 2 June 13th 05 06:06 PM
Find changes and add them to the new sheet sg[_4_] Excel Programming 0 October 27th 04 09:59 PM


All times are GMT +1. The time now is 04:32 PM.

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"