A Macro That Matches then Copies Data

rwmill9716

Active Member
Joined
May 20, 2006
Messages
493
Office Version
  1. 2013
Platform
  1. Windows
I need a macro that will:

1. On Sheet 1, Read Cell A8's value (here 3)
2. Match that value with the values in row 2 (here Col D)
3. Copy the Matched Column to Column A of another Sheet (Sheet2)

Here, I show that copy in Col G, but I need it copied onto another Sheet

Note, my real data set will have many more columns of data to choose from (and the columns of data will be much longer (~50 cells)

Thanks in advance for your help.

Ric




Excel 2010
ABCDEFG
1Data 1Data 2Data 3Data 4Data 3
2Match Row12343
323454
434565
545676
656787
7Match67898
83789109
989101110
10910111211
111011121312
Sheet1
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
What is not specified is whether the will be more than one occurrence of the target value in row 2.
Code:
Sub matchNcopy()
Dim sh1 As Worksheet, sh2 As Worksheet, fn As Range, fAdr As String
Set sh1 = Sheets(1) 'Edit sheet name
Set sh2 = Sheets(2) 'Edit sheet name
    Set fn = sh1.Range("B2", sh1.Cells(2, Columns.Count).End(xlToLeft)).Find(sh1.Range("A8").Value, , xlValues, xlWhole)
        If Not fn Is Nothing Then
            fAdr = fn.Address
            Do
                If sh2.Range("A1") = "" Then
                    fn.EntireColumn.Copy sh2.Range("A1")
                Else
                    fn.EntireColumn.Copy sh2.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
                End If
                Set fn = sh1.Range("B2", sh1.Cells(2, Columns.Count).End(xlToLeft)).FindNext(fn)
            Loop While fAdr <> fn.Address
        End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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