Dynamic selection of column by header name and pasting the whole data into next sheet

Switto

New Member
Joined
Aug 2, 2018
Messages
14
Dear All,

I am working on an excel sheet which has 14 to 15 columns ofdata and I would like to copy some of the columns (according to the headername) and paste it to the next sheet. Sample below:
Reviewed by
PACEClient Name Sub Engaging PartiesDUNS NumberGFIS Client ID
ABCD23200176ITWORX EUROPE LIMITEDITWORX LIMITED27837386656
ABC23210789SPOTIFY LIMITEDSPOTIFY LIMITED39028
AB23210785SPOTIFY LIMITEDSPOTIFY FINANCE 6735267387

<tbody>
</tbody>

How the next sheet looks like:
Reviewed by
PACEClient Name DUNS Number













<tbody>
</tbody>

My problem is the data columns changes every day i.e nextday while pulling the report, client name and data which is in column C would comein column B or E. Hence, I cannot give static code to copy the data. Please seethe below code which was written:
WS1.Cells.Find(what:="Engagement Manager").Select
ActiveCell.Offset(1, 0).Select
FC = ActiveCell.Column
I have tried in different forum and I am new to VBA codingas well.
Please guide me to copy the data till the last row.
Warm Regards
Switto
 
Last edited by a moderator:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
This is how I found a column when the source files were not consistent.
- set variables
vSourceName = workbook name of source file
vSaveName = workbook name of file to save
vLR = Last Row of source file
Note: The On Error was left active because there were further columns to find and paste, it was turned off after last column was found.

Windows(vSourceName).Activate
Rows("1:1").Select
On Error Resume Next
Selection.Find(What:="COLUMN_NAME<column name="">", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
If Err.Number = 0 Then
vCol = Split(Cells(1, ActiveCell.Offset(0, 0).Column).Address(True, False), "$")(0) '<-- Obtain column of found cell
Range(vCol & "1:" & vCol & vLR).Select
Application.CutCopyMode = False
Selection.Copy
Windows(vSaveName).Activate
Range("A1<cell to="" paste="">").Select '<-- which ever cell you want to paste to
<cell to="" paste").select
ActiveSheet.Paste
Else
MsgBox "COLUMN_NAME not found<column name=""><column name="">"
Err.Clear
End If

Note2: When the above code was wrapped with CODE tags rows were deleted (most of the else statement) - sorry for the code format.</column></column></cell></cell></column>
 
Upvote 0
Try this:-
This code should find the 4 headers required in row(1)
Place the specific columns in array "Ray" and post the results on sheet 2 Starting "A1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Dec44
[COLOR="Navy"]Dim[/COLOR] oHds [COLOR="Navy"]As[/COLOR] Variant, H [COLOR="Navy"]As[/COLOR] Variant, Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, nRay [COLOR="Navy"]As[/COLOR] Variant
oHds = Array("Reviewed by", "PACE", "Client Name", "DUNS Number")
Ray = ActiveSheet.UsedRange
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] H [COLOR="Navy"]In[/COLOR] oHds
    Num = Application.Match(H, Range("1:1"), 0)
    Txt = Txt & IIf(Txt = "", Num, "," & Num)
[COLOR="Navy"]Next[/COLOR] H
Ray = Application.Index(Ray, Evaluate("Row(1:" & UBound(Ray, 1) & ")"), Array(Split(Txt, ",")))
Sheets("Sheet11").Range("A1").Resize(UBound(Ray, 1), 4).Value = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thank you for the quick reply :) . I will try this out tomorrow morning. Almost Midnight here.
 
Upvote 0


Thanks for the code. I am getting “run time error 13 – type mismatch”in the following line


Ray = Application.Index(Ray, Evaluate("Row(1:"& UBound(Ray, 1) & ")"), Array(Split(Txt, ",")))


I had made required changes however got the above error.


Due to time constrain, I have used GR00007 code.


I really appreciate your quick post.

Warm Regards
Switto
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,284
Members
448,885
Latest member
LokiSonic

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