Copy the whole column if top cell equals certain text

joey1984

New Member
Joined
Aug 7, 2013
Messages
25
Hi all,

I have two tabs in excel. One called Defects, the second called RawData.

When i extract the data from a system, and copy it to the Raw Data, I want my first tab (Defects) search for where in the Raw Data the first row contains a specific text e.g. Issue Number then copy the corresponding values in that column over to the first tab Defects.

Any ideas?
 

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.
Hi all,

I have two tabs in excel. One called Defects, the second called RawData.

When i extract the data from a system, and copy it to the Raw Data, I want my first tab (Defects) search for where in the Raw Data the first row contains a specific text e.g. Issue Number then copy the corresponding values in that column over to the first tab Defects.

Any ideas?

Further to the above, I want it error proof, where if in the Raw data the column changes from say Column A to Column B, it still copies the right column.
 
Upvote 0
Code:
Option Explicit


Sub joey()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Raw Data")
    Set s2 = Sheets("Defects")
    Dim lc As Long, lr As Long, i As Long
    Dim str As String, lc2 As Long
    lc = s2.Cells(1, Columns.Count).End(xlToLeft).Column
    str = InputBox("What string to search in first row?")
    For i = 1 To lc
        If s2.Cells(1, i) = str Then
            lr = s2.Cells(Rows.Count, i).End(xlUp).Row
            lc2 = s1.Cells(1, Columns.Count).End(xlToLeft).Column
            Range(Cells(1, i), Cells(lr, i)).Copy s1.Cells(1, lc2 + 1)
           
        End If
    Next i
End Sub

This is untested code as there is no worksheet provided to test.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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