Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Find a textstring in a "Range" - and then return a value iftextstring is found.

Hi'

Hope you can help.

I need to lookup a collection of textstrings in a specifik range - and
then react if the textstring is found:

Sheet1 contains a number of cells (B2:B5000) each containing a
textstring.
Sheet2 also contains a number of cells ((D2:D999) each containing a
textstring AND a value in range E2:E10.

Now I need to compare (using InString) the cells in Sheet2 with Sheet1
- and if there is a match I need to return the value.


I planned on making a collection of the 999 values I search for, but
how do I match these with the code?
If I find the text in D10 I need to use the value in E10.


Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Find a textstring in a "Range" - and then return a value if textstring is found.

HH,

You can use a VLOOKUP function, like

=VLOOKUP(B2,Sheet2!$D$2:$E$999,2,False)

Then copy down to row 5000.

HTH,
Bernie
MS Excel MVP


"HH" wrote in message
...
Hi'

Hope you can help.

I need to lookup a collection of textstrings in a specifik range - and
then react if the textstring is found:

Sheet1 contains a number of cells (B2:B5000) each containing a
textstring.
Sheet2 also contains a number of cells ((D2:D999) each containing a
textstring AND a value in range E2:E10.

Now I need to compare (using InString) the cells in Sheet2 with Sheet1
- and if there is a match I need to return the value.


I planned on making a collection of the 999 values I search for, but
how do I match these with the code?
If I find the text in D10 I need to use the value in E10.


Thanks.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 133
Default Find a textstring in a "Range" - and then return a value if textst

This should do it - I was a bit unsure why the E range finished at row 10
while the D range went on to 999. I'm assuming the E range is the same:

Sub DoIt()
Dim rng1 As Range
Dim rng2 As Range
Dim lngCtr As Long
For Each rng2 In Worksheets("Sheet2").Range("D2:D999").Cells
For Each rng1 In Worksheets("Sheet1").Range("B2:B5000").Cells
If InStr(rng1.Value, rng2.Value)<0 Then 'found
lngCtr=lngCtr+1

Worksheets("Sheet3").Cells(lngCtr,1).Value=rng2.Of fset(0,1).Value
End If
Next rng1
Next rng2
End Sub

"HH" wrote:

Hi'

Hope you can help.

I need to lookup a collection of textstrings in a specifik range - and
then react if the textstring is found:

Sheet1 contains a number of cells (B2:B5000) each containing a
textstring.
Sheet2 also contains a number of cells ((D2:D999) each containing a
textstring AND a value in range E2:E10.

Now I need to compare (using InString) the cells in Sheet2 with Sheet1
- and if there is a match I need to return the value.


I planned on making a collection of the 999 values I search for, but
how do I match these with the code?
If I find the text in D10 I need to use the value in E10.


Thanks.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Find a textstring in a "Range" - and then return a value iftextst

On 28 Jan., 17:03, Smallweed
wrote:
This should do it - I was a bit unsure why the E range finished at row 10
while the D range went on to 999. I'm assuming the E range is the same:

Sub DoIt()
Dim rng1 As Range
Dim rng2 As Range
Dim lngCtr As Long
For Each rng2 In Worksheets("Sheet2").Range("D2:D999").Cells
For Each rng1 In Worksheets("Sheet1").Range("B2:B5000").Cells
If InStr(rng1.Value, rng2.Value)<0 Then 'found
lngCtr=lngCtr+1

Worksheets("Sheet3").Cells(lngCtr,1).Value=rng2.Of fset(0,1).Value
End If
Next rng1
Next rng2
End Sub


This is just Perfect!
Now I feel inspired ;-)

1)
What if I want the search to begin at a certain row? I know how to
find the row number, but how do I use it in the range?

2)
The advanced and perfect solution would be to offset the sheet1 value
to another column
I can make the value returned from sheet 2 (column E) to be the number
of columns that the value should be offset - is there a way of making
this happen?

Thanks !!!
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 133
Default Find a textstring in a "Range" - and then return a value if te



"HH" wrote:

On 28 Jan., 17:03, Smallweed
wrote:
This should do it - I was a bit unsure why the E range finished at row 10
while the D range went on to 999. I'm assuming the E range is the same:

Sub DoIt()
Dim rng1 As Range
Dim rng2 As Range
Dim lngCtr As Long
For Each rng2 In Worksheets("Sheet2").Range("D2:D999").Cells
For Each rng1 In Worksheets("Sheet1").Range("B2:B5000").Cells
If InStr(rng1.Value, rng2.Value)<0 Then 'found
lngCtr=lngCtr+1

Worksheets("Sheet3").Cells(lngCtr,1).Value=rng2.Of fset(0,1).Value
End If
Next rng1
Next rng2
End Sub


This is just Perfect!
Now I feel inspired ;-)

1)
What if I want the search to begin at a certain row? I know how to
find the row number, but how do I use it in the range?


If x is the row to start in, the following amendment looks at every cell in
the range between Dx (e.g. D5 if x=5) and D999 (Cells(x,y) is row x, column
y).

For Each rng2 In Worksheets("Sheet2").Range(Cells(x, 4), Cells(999, 4).Cells

2)
The advanced and perfect solution would be to offset the sheet1 value
to another column
I can make the value returned from sheet 2 (column E) to be the number
of columns that the value should be offset - is there a way of making
this happen?


I returned the values to Sheet3, offset down a cell each time so you get a
column of results. Offset(row, col) is useful. E.g., instead of:
Worksheets("Sheet3").Cells(lngCtr,1).Value=rng2.Of fset(0,1).Value in the
above you could do:
rng1.Offset(0, rng2.Value).Value=whatever
Thanks !!!



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Find a textstring in a "Range" - and then return a value if te



If x is the row to start in, the following amendment looks at every cell in
the range between Dx (e.g. D5 if x=5) and D999 (Cells(x,y) is row x, column
y).

For Each rng2 In Worksheets("Sheet2").Range(Cells(x, 4), Cells(999, 4).Cells



2)
The advanced and perfect solution would be to offset the sheet1 value
to another column
I can make the value returned from sheet 2 (column E) to be the number
of columns that the value should be offset - is there a way of making
this happen?


I returned the values to Sheet3, offset down a cell each time so you get a
column of results. Offset(row, col) is useful. E.g., instead of:
Worksheets("Sheet3").Cells(lngCtr,1).Value=rng2.Of fset(0,1).Value in the
above you could do:
rng1.Offset(0, rng2.Value).Value=whatever

Thanks !!!


Seems to be working - wishes my original example had been closer to my
tasks... :-)

If I have hidden columns - do they count when using offset?
e.g. Offset from A to C when B is hidden.


Thanks...
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 133
Default Find a textstring in a "Range" - and then return a value if te

yes they do (there's a range property "visible" you could use to adapt your
code)

"HH" wrote:



If x is the row to start in, the following amendment looks at every cell in
the range between Dx (e.g. D5 if x=5) and D999 (Cells(x,y) is row x, column
y).

For Each rng2 In Worksheets("Sheet2").Range(Cells(x, 4), Cells(999, 4).Cells



2)
The advanced and perfect solution would be to offset the sheet1 value
to another column
I can make the value returned from sheet 2 (column E) to be the number
of columns that the value should be offset - is there a way of making
this happen?


I returned the values to Sheet3, offset down a cell each time so you get a
column of results. Offset(row, col) is useful. E.g., instead of:
Worksheets("Sheet3").Cells(lngCtr,1).Value=rng2.Of fset(0,1).Value in the
above you could do:
rng1.Offset(0, rng2.Value).Value=whatever

Thanks !!!


Seems to be working - wishes my original example had been closer to my
tasks... :-)

If I have hidden columns - do they count when using offset?
e.g. Offset from A to C when B is hidden.


Thanks...

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
keyboard shortcut to return to previous cell after "find" or "got. Nadavb New Users to Excel 1 May 25th 08 01:39 AM
how to return "n/a" when no match is found. Loni - RWT Excel Discussion (Misc queries) 5 October 4th 07 11:04 PM
"No RETURN() or HALT() function found on macro sheet." Will Excel Worksheet Functions 2 January 4th 07 10:10 PM
Getting "compile error" "method or data member not found" on reinstall Bp Excel Programming 1 April 23rd 04 04:42 PM


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