View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 6,953
Default Find and Paste Program

Sub FindCells()
Dim rng As Range, cell As Range, rng1 As Range
With Worksheets("Sheet1")
Set rng = .Range("F2:F262")
End With
With Worksheets("VVGI.TC_EST")
For Each cell In rng
Set rng1 = .Range("B1:IV13627").Find(cell.Value)
If Not rng1 Is Nothing Then
cell.Offset(0, 1).Value = .Cells(rng1.Row, 1)
End If
Next
End With
End Sub

Tested in xl2003

As written, it searches VVGI.TC_EST
for each cell in Sheet1!F2:F262. If a match is found, it returns the value
from column A of VVGI.TC_EST in the same row to the adjacent cell being
searched for. For example, if searching for the value in F5, the results are
placed in G5

If you only want to search for a single value in one cell, change
Set rng = .Range("F2:F262")
to something like (searching for value in F5 only:)

Set rng = .Range("F5")

if you wanted the results in column A of that row on Sheet1, then change

cell.Offset(0, 1).Value = .Cells(rng1.Row, 1) ' on cell to the left
to
cell.Offset(0, -5).Value = .Cells(rng1.Row, 1) ' 5 cells to the right - col A

--
Regards,
Tom Ogilvy





"Taru" wrote:


OK I read a little and learned very very little about VBA.

I opened VBA and added a Module, I actually copied and pasted exactly
what you wrote into the module and ran it just like it is below.

Dim rng as Range, cell as Range, rng1 as Range
With Worksheets('Sheet1")
set rng = .Range("A1:A626")
End with
With Worksheets("Sheet2')
for each cell in rng
set rng1 = .Range("B1:Z13000").Find(cell.Value)
if not rng1 is nothing then
cell.offset(0,1).Value = .Cells(rng1.row,1)
end if
Next
end With

After running this a got an error that said Compile error;
Expected:expression.
I assumed this was because you have an apostrophy(') before sheet1
instead of a quote ("). I replaced the ' with a " and that problem went
away. I also modified the ranges so that they fit my document, i.e., I
changed set rng to .Range("F2:F262") and rng1 to .Range("B1:IV13627")

I ran it again and got this error, Compile Error; Invalid outside
procedure. And it highlights with worksheets("sheet1"). and Sheet1 is
the actual name of the sheet I'm using. I went ahead and changed the
second sheets name to its real name, VVGI.TC_EST.

However, I'm not sure If I'm clear on what I need it to do. I don't
need it to compare an entire column. I need it to find a specific cell
and report back the first cell in that row.


--
Taru
------------------------------------------------------------------------
Taru's Profile: http://www.excelforum.com/member.php...o&userid=35494
View this thread: http://www.excelforum.com/showthread...hreadid=552664