VBA macro to copy/paste data from sheet to another

banneduser123

Banned - Rules violations
Joined
Mar 29, 2006
Messages
181
so let's say I have to worksheets, Sheet1 and Sheet2.

I would like to search for a column header in Sheet2, and if found, copy all the data in that column (except for the header) and paste it into a particular column in Sheet1 beginning from row 2 (to account for the header).

I would then want to do this a couple times over

any help would be appreciated
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Code:
Sub t()
Dim col As String, col2 As String, fn As Range
col1 = InputBox("Enter the header of column to copy. ", "COLUMN TO COPY")
col2 = InputBox("Enter the alpha letter for the destination column on sheet 1.", "DESTINATION COLUMN")
Set fn = Sheets(2).Range("1:1").Find(col1, , xlValues)
    If Not fn Is Nothing Then
        Intersect(Sheets(2).UsedRange.Offset(1), fn.EntireColumn).Copy Sheets(1).Range(col2 & 2)
    End If
End Sub
 
Last edited:
Upvote 0
so after it search and copy/pastes the first column, I want to be able to look for another particular column and copy/paste that, and repeat the process a number of times...was that clear?
 
Upvote 0
Then just run the macro again and change the parameters in the input boxes. The macro does not hold the values you place in those pop-up boxes once the macro completes. The values have to be entered each time the macro runs.

Just a side note: It makes working with data a lot easier if worksheets are designed in consistent formats, i.e. the same headers in the same columns and rows between sheets. That way, you don't have to search for headers, you can just use the regular column and row references of the worksheets to locate and manipulate data.
 
Last edited:
Upvote 0
that wouldn't be efficient to keep running the macro and changing parameters....there's gotta be a better macro to write

the task is fairly simple, there are two files - one file has 10 columns of data, the second file has 5 columns of pre-assigned fields...
I have to take the data from the 1st file and paste it into the proper column in the 2nd file.

having consistent formats between the two files isn't an option...that's why I was trying to write macro
 
Upvote 0
that wouldn't be efficient to keep running the macro and changing parameters....there's gotta be a better macro to write

the task is fairly simple, there are two files - one file has 10 columns of data, the second file has 5 columns of pre-assigned fields...
I have to take the data from the 1st file and paste it into the proper column in the 2nd file.

having consistent formats between the two files isn't an option...that's why I was trying to write macro

Based on the criteria posted thus far, I don't know what else to offer. Computers are really dumb animals with a capability to prodcess data rapidly if given the right logical instructions. They cannot second guess the intent of the user and neither can the programmer in most cases. I understand why you need to use the 'Find' mentod to locate your columns to copy, but unless you have a specific repetitive process that you follow in copying, eg. a consistent order in which the headers will be searched, then the inputbox is the best method to handle variations in copying requirements. If you do have a consistent order of headers which columns are repetively copied, They they can be placed in an array and the Find and Copy porcess can be done in a loop. But that type of information was not furnished in any of the above posts. The destination to paste the copied columns to is also needed for each column to be copied.
 
Last edited:
Upvote 0
If the headers in the destination (paste) sheet are the same as the headers in the source (copy) sheet, then a list of those headers would be all that is needed to writh a macro that would copy and paste your columns. Actually, I would only need the cell references (A1, B1, etcc.) for the destination headers, if they are identical to the source headers.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,750
Messages
6,180,740
Members
452,996
Latest member
nelsonsix66

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