Find and move to specified location

Lilly44

New Member
Joined
Feb 7, 2014
Messages
20
Hi,
I have a spreadsheet within a worksheet that contains a variety of headers (each in duplicate). For example, carrot carrot milk milk eggs eggs, etc

I need to be able to find the values under each header and transfer to a defined column on a second spreadsheet which lists them in a particular (correct order). For example, I need to find the values under each of the eggs columns in spreadsheet 1 and move them to columns A and B in spreadsheet 2. Is there an easy way to do this?

Thanks for your help!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Are the duplicate headers in Sheet1 always beside each other?
 
Upvote 0
Try:
Code:
Sub CopyCols()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long, lCol As Long, Header As Range, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lCol = srcWS.Cells(1, Columns.Count).End(xlToLeft).Column
    For x = 1 To lCol Step 2
        Set Header = desWS.Rows(1).Find(srcWS.Cells(1, x).Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not Header Is Nothing Then
            If Header.Column = 2 Then
                srcWS.Cells(2, x).Resize(LastRow - 1, 2).Copy desWS.Cells(2, 1)
            Else
                srcWS.Cells(2, x).Resize(LastRow - 1, 2).Copy desWS.Cells(2, Header.Column)
            End If
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Mumps,
Thank you so much for your help and I'm sorry I had not responded sooner.
The code you gave me works :) I just need to make it start on the second column (for both the source and destination sheets) because the first column has the sample names.
 
Upvote 0
Try:
Code:
Sub CopyCols()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long, lCol As Long, Header As Range, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lCol = srcWS.Cells(1, Columns.Count).End(xlToLeft).Column
    For x = 2 To lCol Step 2
        Set Header = desWS.Rows(1).Find(srcWS.Cells(1, x).Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not Header Is Nothing Then
            srcWS.Cells(2, x).Resize(LastRow - 1, 2).Copy desWS.Cells(2, Header.Column)
        End If
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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