VBA code to Look for Certain Columns by Name and Paste to New Workbook

zach9208

Board Regular
Joined
Dec 15, 2015
Messages
117
Hi all,

I have been searching for a macro for a while now to cleanup a file that has many columns. I have five columns that are named in row 1. I want to grab only these five columns and move to a new workbook. The only catch is that this file sometimes changes and gets additional columns added but the column names never change. In total there are 20+ columns in this workbook and I just want a select few.

For example, I need a macro to scan across row 1 and find (based on the column header name) and copy the following columns: "Portfolio", "Trade Date", "Book Value", "Price Source" and "Currency". I need to skip over column names that do not match. I then want these 5 to paste over the entire column contents to a new workbook.

Any help would be appreciated. Thanks in advance!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Code:
Sub getColumns()
Dim nmary As Variant, sh1 As Worksheet, sh2 As Worksheet, i As Long, rng As Range
Set sh1 = ThisWorkbook.ActiveSheet
nmary = Array("Portfolio", "Trade Date", "Book Value", "Price Source", "Currency")
Workbooks.Add
Set sh2 = ActiveWorkbook.Sheets(1)
    For i = LBound(nmary) To UBound(nmary)
        Set rng = sh1.Rows(1).Find(nmary(i), , xlValues).EntireColumn
        rng.Copy sh2.Cells(1, i + 1)
    Next
End Sub
 
Upvote 0
Thanks, JLGWhiz! This code worked perfectly!! Any way to run fit text to columns too after it pastes?
 
Upvote 0
Thanks, JLGWhiz! This code worked perfectly!! Any way to run fit text to columns too after it pastes?

Code:
Sub getColumns()
Dim nmary As Variant, sh1 As Worksheet, sh2 As Worksheet, i As Long, rng As Range
Set sh1 = ThisWorkbook.ActiveSheet
nmary = Array("Portfolio", "Trade Date", "Book Value", "Price Source", "Currency")
Workbooks.Add
Set sh2 = ActiveWorkbook.Sheets(1)
    For i = LBound(nmary) To UBound(nmary)
        Set rng = sh1.Rows(1).Find(nmary(i), , xlValues).EntireColumn
        rng.Copy sh2.Cells(1, i + 1)
    Next
[COLOR=#0000CD]sh2.Columns.AutoFit[/COLOR]
End Sub
 
Upvote 0
Is there any way that I can look at row 4 for these headers? I tried modifying the code, but I keep getting an error " object variable or with block variable not set".

Set rng = sh1.Rows(4).Find(nmary(i), , xlValues).EntireColumn

What can I do to check for these headers in column 4 instead of 1. I need to still copy the matching columns to the very bottom of the data. Thanks!
 
Upvote 0

Forum statistics

Threads
1,216,575
Messages
6,131,501
Members
449,654
Latest member
andz

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