Copying columns of data to second worksheet based on input on third worksheet

StuartE

New Member
Joined
Oct 11, 2024
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
I have a table of data that i want to copy only certain columns from into a second worksheet based on input on a third worksheet.

Data in source data (sheet1) starts in A1 and is of variable column and row numbers
Sheet 2 is destination sheet and data should start to be pasted into A1. All column data should be copied if the column header is included in sheet 3
Sheet 3 contains a variable length list of column headers from sheet 1 required to be included on the report on sheet 2. List starts in cell B2 on this sheet and works down a column
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim v As Variant, hdWS As Worksheet, srcWS As Worksheet, desWS As Worksheet, i As Long, fnd As Range
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    Set hdWS = Sheets("Sheet3")
    v = hdWS.Range("B2", hdWS.Range("B" & Rows.Count).End(xlUp)).Value
    For i = LBound(v) To UBound(v)
        Set fnd = srcWS.Rows(1).Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole)
        If Not fnd Is Nothing Then
            With desWS
                srcWS.Cells(1, fnd.Column).EntireColumn.Copy .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 1)
            End With
        End If
    Next i
    desWS.Columns(1).Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Sub CopyData()
    Application.ScreenUpdating = False
    Dim v As Variant, hdWS As Worksheet, srcWS As Worksheet, desWS As Worksheet, i As Long, fnd As Range
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    Set hdWS = Sheets("Sheet3")
    v = hdWS.Range("B2", hdWS.Range("B" & Rows.Count).End(xlUp)).Value
    For i = LBound(v) To UBound(v)
        Set fnd = srcWS.Rows(1).Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole)
        If Not fnd Is Nothing Then
            With desWS
                srcWS.Cells(1, fnd.Column).EntireColumn.Copy .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 1)
            End With
        End If
    Next i
    desWS.Columns(1).Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,580
Messages
6,166,880
Members
452,080
Latest member
Akin Himself

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