Results 1 to 4 of 4

Macro to Copy and paste data based on column headings

Threaded View

  1. #1
    Registered User
    Join Date
    04-09-2018
    Location
    UK
    MS-Off Ver
    2010
    Posts
    2

    Macro to Copy and paste data based on column headings

    Hi

    I'm very new to writing Macros in Excel, and have done a bit of looking around to try and solve my problem, but I haven't found a solution yet that works.

    I'm trying to write a Macro to do the following:

    I'm trying to copy data from Sheet 1, Workbook 1 based on column headings (so for example, I want to copy all the data under the column name "Sort"). The number of rows of data in this row may increase/decrease. I then want to paste this data into Sheet 2, Workbook 2 under the column name "Name". Columns may be added/removed from both workbooks, which is why I want to write the macro to copy based on the column name rather than a column number.

    I have been using the below code, which I've tried putting together based on similar but slightly different requests I've found online, but when I run the macro, nothing much happens - I've written the Macro in Workbook 2 and it just opens Workbook 1.

    If anyone can see something wrong with my code or suggest an alternative, I'd be extremely grateful for any help. Thanks!!!



    Sub CopyProjectName()
    
    Dim CurrentWS As Worksheet
    Set CurrentWS = ActiveSheet
    
    Dim SourceWS As Worksheet
    Set SourceWS = Workbooks("Workbook1.xlsx").Worksheets("Sheet1")
    Dim SourceHeaderRow As Integer: SourceHeaderRow = 1
    Dim SourceCell As Range, sRange As Range, Rng As Range
    
    Dim TargetWS As Worksheet
    Set TargetWS = Workbooks("Workbook2.xlsm").Worksheets("Sheet2")
    Dim TargetHeader As Range
    Set TargetHeader = TargetWS.Range("A1:AX1")
    
    Dim RealLastRow As Long
    Dim SourceCol As Integer
    
    Range("B2").Select
    
    SourceWS.Activate
         LastCol = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
           Set sRange = Sheets("Sheet1").Range("A1", Cells(1, LastCol))
               With sRange
                   Set Rng = .Find(What:="Sort", _
                   After:=.Cells(1), _
                   LookIn:=xlValues, _
                   LookAt:=xlWhole, _
                   SearchOrder:=xlByRows, _
                   SearchDirection:=xlPrevious, _
                   MatchCase:=False)
           If Not Rng Is Nothing Then
               LastRow = Sheets("Sheet1").Cells(Rows.Count, Rng.Column).End(xlUp).Row
                   Sheets("Sheet1").Range(Rng, Cells(LastRow, Rng.Column)).Copy
                   
    TargetWS.Activate
        Sheets("Sheet2").Range("B1").Paste
                   
        End If
        End With
    
    End Sub
    Last edited by AliGW; 04-09-2018 at 02:49 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA Macro - Copy and paste data match by column headings
    By weijianhk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-26-2015, 11:05 PM
  2. macro to copy paste data based on column headings
    By kets0985 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-22-2014, 02:26 PM
  3. Create macro to copy data using column headings
    By zkhan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2013, 02:01 AM
  4. Macro to match column headings and paste into next available row.
    By vizzy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-06-2013, 10:38 AM
  5. Copy/Paste filtered data from one sheet to another with only matched column headings
    By archerrc in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-08-2013, 05:00 PM
  6. Macro to copy and paste based on a column
    By Mahitha in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2011, 04:48 AM
  7. [SOLVED] Macro Syntax to copy and paste dynamic data based on one column
    By jbsand1001 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-17-2005, 10:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1