Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default VBA: How do I find a value within a selected range?

I have declared a range in a prior statement, selected that range, and now
want to find a value within that range. For some reason, the program is
looking outside of that range. What am I doing wrong?

With ActiveCell
Set range1 = Range(ActiveCell, .Offset(100, 0))
End With

Range(range1.Address).Select

Cells.Find(What:=Location, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default VBA: How do I find a value within a selected range?

You define your range. You select the range (which is not necessary) and then
you use Cells wich completely ignores the selection and searches the whole
sheet. If the location is not fond your code will then crash as you are
trying to activate a cell that does not exist... Try this...

dim range1 as range
dim rngFound as range

Set range1 = Range(ActiveCell, activecell.Offset(100, 0))
set rngfound = range1.find(What:=Location, LookIn:=xlFormulas, _
LookAt:=xlPart, MatchCase:=False)

if rngfound is nothing then
msgbox "location was not found"
else
rngFound.select
end if

--
HTH...

Jim Thomlinson


"Jayne22" wrote:

I have declared a range in a prior statement, selected that range, and now
want to find a value within that range. For some reason, the program is
looking outside of that range. What am I doing wrong?

With ActiveCell
Set range1 = Range(ActiveCell, .Offset(100, 0))
End With

Range(range1.Address).Select

Cells.Find(What:=Location, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default VBA: How do I find a value within a selected range?

On Aug 13, 3:52*pm, Jayne22 wrote:
I have declared a range in a prior statement, selected that range, and now
want to find a value within that range. For some reason, the program is
looking outside of that range. What am I doing wrong?

With ActiveCell
* * * * * * Set range1 = Range(ActiveCell, .Offset(100, 0))
End With

Range(range1.Address).Select

Cells.Find(What:=Location, After:=ActiveCell, LookIn:=xlFormulas, _
* * * * * * LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
* * * * * * MatchCase:=False, SearchFormat:=False).Activate


Hi Jayne:

Try changing your find statement to

range1.Find(...

However, if you are using a Loop or For/Next statement, your range may
change if the ActiveCell statement is within the Loop or For/Next
because your .Find activates the new cell.

HTH,

Steven
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
Find the last cell in a selected range? J@Y Excel Programming 6 June 15th 07 07:25 PM
Find last row and column in a selected range Bernie Deitrick Excel Programming 0 December 20th 06 02:41 PM
Macro to find empty cell and select range to print selected. [email protected] Excel Programming 1 May 15th 06 09:05 AM
Find Findnext in selected range looloo[_2_] Excel Programming 2 January 25th 05 06:51 PM
how to find last row/col index within a selected range Jeff Excel Programming 2 October 6th 04 03:58 PM


All times are GMT +1. The time now is 10:17 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"