VBA to Copy Column based on Header Name from cell Value on another Sheet

lamarh755

New Member
Joined
Jan 28, 2020
Messages
35
Office Version
  1. 2013
I have 2 sheets, one named Template and another named Raw Data. I am trying to enter a value in cell U3 on the Template sheet and then run a macro to have the column on the Raw Data sheet with the header name that matches the value in cell U3 on the Template sheet, copied and pasted into cell A17 down on the Template sheet to fill in my template. I am somewhat new to VBA. Any assistance would be greatly appreciated.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Assuming the following:
a) The headers in the "raw data" sheet are in row 1.
b. The data to copy is from row 2 down.

Try this:
VBA Code:
Sub copy_column()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim f As Range
  Dim lr As Long
  
  Set sh1 = Sheets("Raw Data")
  Set sh2 = Sheets("Template")

  Set f = sh1.Range("1:1").Find(sh2.Range("U3").Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    lr = sh1.Cells(Rows.Count, f.Column).End(3).Row
    sh2.Range("A17").Resize(lr - 1).Value = sh1.Cells(2, f.Column).Resize(lr - 1).Value
  Else
    MsgBox "Column name does not exist"
  End If
End Sub

🧙‍♂️
 
Upvote 0
Solution
Assuming the following:
a) The headers in the "raw data" sheet are in row 1.
b. The data to copy is from row 2 down.

Try this:
VBA Code:
Sub copy_column()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim f As Range
  Dim lr As Long
 
  Set sh1 = Sheets("Raw Data")
  Set sh2 = Sheets("Template")

  Set f = sh1.Range("1:1").Find(sh2.Range("U3").Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    lr = sh1.Cells(Rows.Count, f.Column).End(3).Row
    sh2.Range("A17").Resize(lr - 1).Value = sh1.Cells(2, f.Column).Resize(lr - 1).Value
  Else
    MsgBox "Column name does not exist"
  End If
End Sub

🧙‍♂️
This worked perfectly, thank you!!!!!!
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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