Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

How to copy columns from one excel sheet to another based on column name

Dear all,

I've been given a sheet with alot of data.  I want to move specific columns into new excel sheets so that i can import it into a database.

Is there any easier way than manually copying and pasting?

It would also be ideal if a string in a cell i.e. B3 of original sheet be copied into a column on the second sheet described above, with a column name that I define.

Thanks
0
AndyC1000
Asked:
AndyC1000
1 Solution
 
Saqib Husain, SyedEngineerCommented:
If this is a one-time job then the easiest way is to copy then entire data over and then delete the unwanted columns.
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi,

Whether this is a recurring task, or not, the process can be automated.

You will need to provide the complete specification of your requirements though.

Obviously, if there is a deadline, then the time taken to prepare an automated solution may be longer than actually moving the data manually (as ssaqibh mentioned).

BFN,

fp.
0
 
Rob HensonIT & Database AssistantCommented:
You could use the Advanced Filter function.

On the Destination sheet put only the column headers that you require.
Setup a small table with one column header and blank cell below or something that will capture all data.

In the AF function:
Data - source sheet
Criteria - area as setup above
Destination - column headers on destination sheet.
When run, only the columns for which you have headers will be copied over.

Thanks
Rob H
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Martin LissRetired ProgrammerCommented:
Here's a macro. This one copies columns A, C and D from Sheet1 to Sheet2 but those things are easily changed.

Sub CopySomeColumns()
Dim arrCols
Dim lngIndex As Long
Dim lngCol As Long

' These are the column numbers to copy
arrCols = Array(1, 2, 4)
' This starts the pasting at column "A"
lngCol = 1

Application.ScreenUpdating = False

For lngIndex = 0 To UBound(arrCols)
    Worksheets("Sheet1").Activate
    Worksheets("Sheet1").Columns(arrCols(lngIndex)).Select
    Selection.Copy
    Worksheets("Sheet2").Activate
    Worksheets("Sheet2").Columns(lngCol).Select
    ActiveSheet.Paste
    lngCol = lngCol + 1
Next

Application.ScreenUpdating = True
End Sub

Open in new window

0
 
AndyC1000Author Commented:
Thanks Martin that's what I was after.

To extend the macro to copy i.e. columns 10, 11 and 12 into sheet 3, can this be included in the same macro? Or will I need to create another macro each time I need to copy different columns into new sheets?

I copied and adjusted the for statement for the above scenario into the same macro is this the best way?
0
 
Martin LissRetired ProgrammerCommented:
Just change line 7. For example to do what you mention just change 1, 2, 4 to 10, 11, 12. You also need to change Sheet2 to Sheet3 if that's your destination sheet.
0
 
AndyC1000Author Commented:
Great, that worked.

Is it possible to copy column values (i.e column 2) from the third row downwards into the destination sheet?

I'm trying to avoid any manual changes to the data.  I can't delete the data above the table headers because it includes descriptors.
0
 
Martin LissRetired ProgrammerCommented:
Sure. This copies the data from row 3 downward from sheet1 to sheet3.

Sub CopySomeColumns()
Dim arrCols
Dim lngIndex As Long
Dim lngCol As Long
Dim lngLastRow As Long

lngLastRow = ActiveSheet.UsedRange.Rows.Count
' These are the column numbers to copy
arrCols = Array(10, 11, 12)
' This starts the pasting at column "A"
lngCol = 1

Application.ScreenUpdating = False

For lngIndex = 0 To UBound(arrCols)
    Worksheets("Sheet1").Activate
    Worksheets("Sheet1").Range(Cells(3, arrCols(lngIndex)), Cells(lngLastRow, arrCols(lngIndex))).Select
    Selection.Copy
    Worksheets("Sheet3").Activate
    Worksheets("Sheet3").Cells(3, lngCol).Select
    ActiveSheet.Paste
    lngCol = lngCol + 1
Next

Application.ScreenUpdating = True
End Sub

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now