Find only First Value Excel VBA

Kramer328

New Member
Joined
Oct 17, 2018
Messages
7
Hello,

I am trying to copy text from one open workbook to another using vba. I want to have the macro find any values in column I of Worksheet1 and compare them to column A of Worksheet2. If it there is a match, I want to copy the cells that is 8 columns to the left of column I on Worksheet1 and paste it into the cell that is 4 column to the right of the matching cell on Worksheet2. However, I only want to do this for the first match that the macro finds. If it finds another identical match, I want the macro to copy/paste the value 8 colums to the left into Worksheet2. Is this possible? Both of my worksheets have headers and I would like the macro to ignore these cells when searching (Start at A2 instead of A1 for the search). Any help would be greatly appreciated!

Here is an example of what each worksheet should look like after running the macro.
Worksheet1:
ABCDEFGHI
Header1
Header2Header3Header4Header5Header6Header7Header8Header9
1A
2B
3A
4C
5B
6C

<tbody>
</tbody>

Worksheet2 After Macro Runs:
ABCD
Header1Header2Header3Header4
A1
B2
A3
C4
B5
C6

<tbody>
</tbody>

 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG24Oct34
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Q [COLOR="Navy"]As[/COLOR] Variant, Ray() [COLOR="Navy"]As[/COLOR] Variant, K [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]With[/COLOR] Sheets("Sheet1")
    [COLOR="Navy"]Set[/COLOR] Rng = .Range("I2", .Range("I" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        ReDim Preserve Ray(1)
        Ray(1) = Dn.Offset(, -8)
        .Add Dn.Value, Ray
    [COLOR="Navy"]Else[/COLOR]
       Q = .Item(Dn.Value)
       ReDim Preserve Q(UBound(Q) + 1)
       Q(UBound(Q)) = Dn.Offset(, -8)
      .Item(Dn.Value) = Q
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]

[COLOR="Navy"]With[/COLOR] Sheets("Sheet2")
    [COLOR="Navy"]Set[/COLOR] Rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
  c = 0
  [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
      [COLOR="Navy"]If[/COLOR] Dn.Value = K [COLOR="Navy"]Then[/COLOR]
          c = c + 1
          [COLOR="Navy"]If[/COLOR] c <= UBound(.Item(K)) [COLOR="Navy"]Then[/COLOR]
              Dn.Offset(, 3).Value = .Item(K)(c)
          [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]Next[/COLOR] K

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top