#1   Report Post  
Posted to microsoft.public.excel.programming
Nic Nic is offline
external usenet poster
 
Posts: 25
Default vba search

Hi,

I want to search a value in a column. But the value is in another sheet
(column I and each cell in that column).

Ex. I want to search all the values of sheetA.ColumnI in ColumnI of sheetB.
if found i give an unique number to that row (ColumnJ) in SheetA and the
same number to the matched row in sheetB (ColumnJ).

I am a beginner so first of all I tried to record a macro.
But it is to static :

Range("I1").Select
Selection.Copy
Sheets("Sheet2").Select
Columns("I:I").Select
Selection.Find(What:="1808,22", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Range("J1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "1"
Sheets("Sheet1").Select
Range("J1").Select
ActiveCell.FormulaR1C1 = "1"
Range("I2").Select
Selection.Copy



I have problems with the selection.Find :
What:=xxx has to be SheetA:I1

So the find must be in a while till end of the rows in the excel-sheet, also
the what must change for each row (I1,I2,I3,...).

How can I do this in a better way?

Thanks for help.
Nic


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default vba search

Maybe something like this will give you boost:

Option Explicit
Sub testme()
Dim rngA As Range
Dim rngB As Range
Dim myCell As Range
Dim res As Variant
Dim matchCtr As Long

With Worksheets("sheet1")
Set rngA = .Range("I1", .Cells(.Rows.Count, "I").End(xlUp))
End With

With Worksheets("sheet2")
Set rngB = .Range("I1", .Cells(.Rows.Count, "I").End(xlUp))
End With

matchCtr = 0
For Each myCell In rngA.Cells
res = Application.Match(myCell.Value, rngB, 0)
If IsError(res) Then
'no match, do nothing
Else
matchCtr = matchCtr + 1
myCell.Offset(0, 1).Value = matchCtr
rngB(res).Offset(0, 1).Value = matchCtr
End If
Next myCell

End Sub


It uses the worksheet function =match() to find if there's a match.

nic wrote:

Hi,

I want to search a value in a column. But the value is in another sheet
(column I and each cell in that column).

Ex. I want to search all the values of sheetA.ColumnI in ColumnI of sheetB.
if found i give an unique number to that row (ColumnJ) in SheetA and the
same number to the matched row in sheetB (ColumnJ).

I am a beginner so first of all I tried to record a macro.
But it is to static :

Range("I1").Select
Selection.Copy
Sheets("Sheet2").Select
Columns("I:I").Select
Selection.Find(What:="1808,22", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Range("J1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "1"
Sheets("Sheet1").Select
Range("J1").Select
ActiveCell.FormulaR1C1 = "1"
Range("I2").Select
Selection.Copy

I have problems with the selection.Find :
What:=xxx has to be SheetA:I1

So the find must be in a while till end of the rows in the excel-sheet, also
the what must change for each row (I1,I2,I3,...).

How can I do this in a better way?

Thanks for help.
Nic


--

Dave Peterson

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
Two search category & two search terms avk Excel Discussion (Misc queries) 1 June 2nd 10 01:25 PM
file search or search files Richad Excel Discussion (Misc queries) 0 October 22nd 09 07:56 PM
Functions (search within search result) reply to this please Nick Excel Worksheet Functions 1 February 17th 09 03:57 AM
Functions (search within search result) Nick Excel Worksheet Functions 1 February 17th 09 03:51 AM
How do I search excel spreadsheets using multiple search criteria. Kasper Excel Worksheet Functions 4 December 15th 05 12:26 AM


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