Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 80
Default find closest match and copy to sheet1

Hello,

I have a macro that compares sheet1 col. A with sheet2 col.A and copies
sheet2 col.B matches into sheet1 col.B.

Sub CopyIDData()
Dim rng As Range
Dim rng1 As Range
Dim cell As Range
Dim res As Variant

With Worksheets("sheet1")
Set rng = .Range(.Range("A1"), .Range("A1").End(xlDown))
End With
With Worksheets("Sheet2")
Set rng1 = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
End With

For Each cell In rng
res = Application.Match(cell, rng1, 0)
If Not IsError(res) Then
rng1(res, 2).Resize(1, 2).Copy Destination:=cell.Offset(0, 1)
End If
Next
End Sub


----------

Sheet 1

A B C D E F G
12
13
14

Sheet 2

D E F G
11 1 2 3
13 4 5 6
16 7 8 9
14 3 2 1

Result after Function;

Sheet 1

A B C D E F G
12
13 4 5 6
14 3 2 1



Now, What I want is same thing but when there is no matches to be found I
want the macro to find the closest number to sheet2 Col.A and copy its
adjusent to sheet1 Col.B.

I realy need this because it will save me tons of time.

Thank you.

--
Message posted via http://www.officekb.com

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default find closest match and copy to sheet1

In excel, you could use a worksheet formula (an array formula) like:

=MATCH(MIN(ABS(sheet1!B1-sheet2!A1:A25)),ABS(sheet1!B1-sheet2!A1:A25),0)
(hit ctrl-shift-enter)

to get the row number of the closest match.

Option Explicit
Sub CopyIDData()
Dim rng As Range
Dim rng1 As Range
Dim cell As Range
Dim res As Variant

With Worksheets("sheet1")
Set rng = .Range("A1", .Range("A1").End(xlDown))
End With
With Worksheets("Sheet2")
Set rng1 = .Range("A1", .Range("A1").End(xlDown))
End With

For Each cell In rng.Cells
res = Application.Match(cell, rng1, 0)
If IsNumeric(res) Then
'don't change res!
Else
'change it here
res = Application.Evaluate("match(min(abs(" _
& cell.Address(external:=True) & "-" _
& rng1.Address(external:=True) & ")),abs(" _
& cell.Address(external:=True) & "-" _
& rng1.Address(external:=True) & "),0)")
End If
rng1(res, 2).Resize(1, 2).Copy Destination:=cell.Offset(0, 1)
Next
End Sub



"saman110 via OfficeKB.com" wrote:

Hello,

I have a macro that compares sheet1 col. A with sheet2 col.A and copies
sheet2 col.B matches into sheet1 col.B.

Sub CopyIDData()
Dim rng As Range
Dim rng1 As Range
Dim cell As Range
Dim res As Variant

With Worksheets("sheet1")
Set rng = .Range(.Range("A1"), .Range("A1").End(xlDown))
End With
With Worksheets("Sheet2")
Set rng1 = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
End With

For Each cell In rng
res = Application.Match(cell, rng1, 0)
If Not IsError(res) Then
rng1(res, 2).Resize(1, 2).Copy Destination:=cell.Offset(0, 1)
End If
Next
End Sub

----------

Sheet 1

A B C D E F G
12
13
14

Sheet 2

D E F G
11 1 2 3
13 4 5 6
16 7 8 9
14 3 2 1

Result after Function;

Sheet 1

A B C D E F G
12
13 4 5 6
14 3 2 1

Now, What I want is same thing but when there is no matches to be found I
want the macro to find the closest number to sheet2 Col.A and copy its
adjusent to sheet1 Col.B.

I realy need this because it will save me tons of time.

Thank you.

--
Message posted via http://www.officekb.com


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9,101
Default find closest match and copy to sheet1

Sub CopyIDData()
Dim rng As Range
Dim rng1 As Range
Dim cell As Range
Dim res As Variant

With Worksheets("sheet1")
Set rng = .Range(.Range("A1"), .Range("A1").End(xlDown))
End With
With Worksheets("Sheet2")
Set rng1 = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
End With

For Each cell In rng
first = true
for each cell1 in Rng1
if first = true then
closest = rng1
first = false
else
if abs(rng - rng1) < abs(rng - closest) then
closest = rng1)
end if
next cell1
cell.Offset(0, 1) = closest
Next
End Sub


"saman110 via OfficeKB.com" wrote:

Hello,

I have a macro that compares sheet1 col. A with sheet2 col.A and copies
sheet2 col.B matches into sheet1 col.B.

Sub CopyIDData()
Dim rng As Range
Dim rng1 As Range
Dim cell As Range
Dim res As Variant

With Worksheets("sheet1")
Set rng = .Range(.Range("A1"), .Range("A1").End(xlDown))
End With
With Worksheets("Sheet2")
Set rng1 = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
End With

For Each cell In rng
res = Application.Match(cell, rng1, 0)
If Not IsError(res) Then
rng1(res, 2).Resize(1, 2).Copy Destination:=cell.Offset(0, 1)
End If
Next
End Sub


----------

Sheet 1

A B C D E F G
12
13
14

Sheet 2

D E F G
11 1 2 3
13 4 5 6
16 7 8 9
14 3 2 1

Result after Function;

Sheet 1

A B C D E F G
12
13 4 5 6
14 3 2 1



Now, What I want is same thing but when there is no matches to be found I
want the macro to find the closest number to sheet2 Col.A and copy its
adjusent to sheet1 Col.B.

I realy need this because it will save me tons of time.

Thank you.

--
Message posted via http://www.officekb.com


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 80
Default find closest match and copy to sheet1

Joel,

Thank you for responding,

I get syntax error becase this line is in red: closest = rng1)

any idea?
thx.


Joel wrote:
Sub CopyIDData()
Dim rng As Range
Dim rng1 As Range
Dim cell As Range
Dim res As Variant

With Worksheets("sheet1")
Set rng = .Range(.Range("A1"), .Range("A1").End(xlDown))
End With
With Worksheets("Sheet2")
Set rng1 = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
End With

For Each cell In rng
first = true
for each cell1 in Rng1
if first = true then
closest = rng1
first = false
else
if abs(rng - rng1) < abs(rng - closest) then
closest = rng1)
end if
next cell1
cell.Offset(0, 1) = closest
Next
End Sub

Hello,

[quoted text clipped - 55 lines]

Thank you.


--
Message posted via http://www.officekb.com

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 80
Default find closest match and copy to sheet1

Thank you for responding.

Whenn I run the code I get run type error 13
Type mismatch

when I hit debug it showes this line highlighted:

rng1(res, 2).Resize(1, 2).Copy Destination:=cell.Offset(0, 1)

Thx.

Dave Peterson wrote:
In excel, you could use a worksheet formula (an array formula) like:

=MATCH(MIN(ABS(sheet1!B1-sheet2!A1:A25)),ABS(sheet1!B1-sheet2!A1:A25),0)
(hit ctrl-shift-enter)

to get the row number of the closest match.

Option Explicit
Sub CopyIDData()
Dim rng As Range
Dim rng1 As Range
Dim cell As Range
Dim res As Variant

With Worksheets("sheet1")
Set rng = .Range("A1", .Range("A1").End(xlDown))
End With
With Worksheets("Sheet2")
Set rng1 = .Range("A1", .Range("A1").End(xlDown))
End With

For Each cell In rng.Cells
res = Application.Match(cell, rng1, 0)
If IsNumeric(res) Then
'don't change res!
Else
'change it here
res = Application.Evaluate("match(min(abs(" _
& cell.Address(external:=True) & "-" _
& rng1.Address(external:=True) & ")),abs(" _
& cell.Address(external:=True) & "-" _
& rng1.Address(external:=True) & "),0)")
End If
rng1(res, 2).Resize(1, 2).Copy Destination:=cell.Offset(0, 1)
Next
End Sub

Hello,

[quoted text clipped - 58 lines]
--
Message posted via http://www.officekb.com



--
Message posted via http://www.officekb.com



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default find closest match and copy to sheet1

It worked ok with my testing.

How about sharing the data (not the workbook) where the code fails.

"saman110 via OfficeKB.com" wrote:

Thank you for responding.

Whenn I run the code I get run type error 13
Type mismatch

when I hit debug it showes this line highlighted:

rng1(res, 2).Resize(1, 2).Copy Destination:=cell.Offset(0, 1)

Thx.

Dave Peterson wrote:
In excel, you could use a worksheet formula (an array formula) like:

=MATCH(MIN(ABS(sheet1!B1-sheet2!A1:A25)),ABS(sheet1!B1-sheet2!A1:A25),0)
(hit ctrl-shift-enter)

to get the row number of the closest match.

Option Explicit
Sub CopyIDData()
Dim rng As Range
Dim rng1 As Range
Dim cell As Range
Dim res As Variant

With Worksheets("sheet1")
Set rng = .Range("A1", .Range("A1").End(xlDown))
End With
With Worksheets("Sheet2")
Set rng1 = .Range("A1", .Range("A1").End(xlDown))
End With

For Each cell In rng.Cells
res = Application.Match(cell, rng1, 0)
If IsNumeric(res) Then
'don't change res!
Else
'change it here
res = Application.Evaluate("match(min(abs(" _
& cell.Address(external:=True) & "-" _
& rng1.Address(external:=True) & ")),abs(" _
& cell.Address(external:=True) & "-" _
& rng1.Address(external:=True) & "),0)")
End If
rng1(res, 2).Resize(1, 2).Copy Destination:=cell.Offset(0, 1)
Next
End Sub

Hello,

[quoted text clipped - 58 lines]
--
Message posted via http://www.officekb.com



--
Message posted via http://www.officekb.com


--

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
Help! Find Closest Coordinate Match Bill Excel Discussion (Misc queries) 6 May 2nd 23 07:42 PM
Find closest text match for each unique entry in a list Nathan_Decker Excel Discussion (Misc queries) 2 September 23rd 07 01:36 AM
Find closest match and copy saman110 via OfficeKB.com Excel Discussion (Misc queries) 3 August 31st 07 06:30 AM
Find the closest match to a reference number in a row of unsorted Nick Krill Excel Worksheet Functions 3 January 1st 06 08:33 PM
find closest match to a reference number in a row of numbers Nick Krill Excel Discussion (Misc queries) 4 December 21st 05 11:59 AM


All times are GMT +1. The time now is 02:54 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"