Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Repeated search on the same worksheet

Hi folks,

I have trouble doing repeated searches on the same worksheet. I use this
small function to find a specific value on a given worksheet:

Function Find(ByRef sheet As Worksheet, ByVal label As String) As String
Dim cell As Range
Set cell = sheet.Cells.Find(label);
If cell Is Nothing Then
Find = "(not found)"
Exit Function
End If
cell = cell.Cells(1, 2) ' one cell to the right
Find = cell.Value
End Function

I want to have the search on the entire worksheet each time the function
is called. In particular, when I call it twice with the same "sheet" and
"label" arguments, I want it to find exactly the same occurence.

However, the function does only return the first (and only) occurence of
"label" when it is called for the first time. A second call fails and
delivers "(not found)".

What the heck am I doing wrong?

Jens
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Repeated search on the same worksheet

I'll let one of the group experts give you a more definitive answer, but in
the meantime this might give you a place to start;

Set cell = sheet.Cells.Find(label);

sets your "cell" range to a specific cell where you found the first match

cell = cell.Cells(1, 2) ' one cell to the right

may set your range to a specific cell that doesn't include your match

I'd add a line at the top of the procedure to reset the cell range to the
entire sheet, or to prevent duplicate matches, the entire sheet below the
area of the last match.

I'd also avoid using the word "cell" as a variable (maybe "mycell" or
"targetcell" would work), but that's just me.

HTH,
Keith


"Jens Lenge" wrote in message
...
Hi folks,

I have trouble doing repeated searches on the same worksheet. I use this
small function to find a specific value on a given worksheet:

Function Find(ByRef sheet As Worksheet, ByVal label As String) As String
Dim cell As Range
Set cell = sheet.Cells.Find(label);
If cell Is Nothing Then
Find = "(not found)"
Exit Function
End If
cell = cell.Cells(1, 2) ' one cell to the right
Find = cell.Value
End Function

I want to have the search on the entire worksheet each time the function
is called. In particular, when I call it twice with the same "sheet" and
"label" arguments, I want it to find exactly the same occurence.

However, the function does only return the first (and only) occurence of
"label" when it is called for the first time. A second call fails and
delivers "(not found)".

What the heck am I doing wrong?

Jens



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Solution :)

Keith R wrote:
Set cell = sheet.Cells.Find(label);

sets your "cell" range to a specific cell where you found the first match


Yes, this should find the first match of "label" on the sheet.

cell = cell.Cells(1, 2) ' one cell to the right

may set your range to a specific cell that doesn't include your match


This is intended. The function looks for the first occurrence of the
"label" argument, and then returns the value of the cell to the right of it.

The point is that I do not want to *prevent* duplicate matches when the
function is called more than once. Instead, I want to make it return the
exact same result each time it is called with the same arguments.

And, just by chance, I have come across the solution: If I change the
line "cell = cell.Cells(1, 2)" to "Set cell = cell.Cells(1, 2)", then it
works. Don't ask me why.

Jens
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 do I search for repeated numbers in same column MB New Users to Excel 1 October 28th 09 07:16 PM
How can I search for entries that are repeated in the workbook? Ivan Excel Worksheet Functions 4 May 23rd 07 02:17 AM
copy from B worksheet to A worksheet with NO repeated data tikchye_oldLearner57 Excel Discussion (Misc queries) 1 September 29th 06 06:56 PM
how can i know that my worksheet having repeated same values Prem Excel Worksheet Functions 1 August 1st 06 09:15 AM
Printing text in a repeated cell/row that is longer than repeated Valerie Dyet Excel Discussion (Misc queries) 1 February 13th 06 03:27 AM


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