+ Reply to Thread
Results 1 to 5 of 5

Copy columns between worksheets but only until last row

Hybrid View

  1. #1
    Registered User
    Join Date
    08-06-2019
    Location
    Karlsruhe, Germany
    MS-Off Ver
    Office 365
    Posts
    24

    Copy columns between worksheets but only until last row

    Hello everyone!
    For a small project I am having the following issue at the moment:

    I copy columns from one worksheet into the other, depending on what is written in the first row.
    Let's say if it says "Name" then it will copy the entire column to the other worksheet.
    Now I have the problem that it copies the entire column. By that I mean that also all empty cells are copied as well, which I would like to change.
    How can I make the code so that it only copies data from until the last row that has actual data in it?

    So the code is very simple and looks like following:

    Public Sub copy()
        Dim c As Range
        Dim j As Integer
        Dim Source As Worksheet
        Dim Target As Worksheet
        
    
     
        Set Source = ActiveWorkbook.Worksheets("Data")
        Set Target = ActiveWorkbook.Worksheets("dont_touch")
    
        j = 1     ' Start copying to row 1 in target sheet
        For Each c In Source.Range("A1:Z1")   ' Do 1000 rows
            If c = "Name" Then
               Source.Columns(c.Column).Copy Target.Columns(j)
               j = j + 1
            End If
        Next c
        j = 2     ' Start copying to row 2 in target sheet
        For Each c In Source.Range("A1:Z1")   
            If c = "Age" Then
               Source.Columns(c.Column).Copy Target.Columns(j)
               j = j + 1
            End If
        Next c
       Application.ScreenUpdating = True
        
    End Sub
    Thanks a lot in advance!

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Hi !

    Use the UsedRange property of the source worksheet …

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,754

    Re: Copy columns between worksheets but only until last row

    Untested as you did not provide a worksheet.

    Option Explicit
    
    Public Sub copy()
        Dim c As Range
        Dim j As Integer, i As Long
        Dim Source As Worksheet
        Dim Target As Worksheet
        Dim lr As Long
    
     
        Set Source = ActiveWorkbook.Worksheets("Data")
        Set Target = ActiveWorkbook.Worksheets("dont_touch")
    
        j = 1     ' Start copying to row 1 in target sheet
        For Each c In Source.Range("A1:Z1")   ' Do 1000 rows
            If c = "Name" Then
            i = Columns(c.Column)
            lr = Source.Cells(Rows.Count, i).End(xlUp).Row
            Source.Range(Cells(1, i), Cells(lr, i)).copy Target.Columns(j)
               j = j + 1
            End If
        Next c
        j = 2     ' Start copying to row 2 in target sheet
        For Each c In Source.Range("A1:Z1")
            If c = "Age" Then
            i = Columns(c.Column)
            lr = Source.Cells(Rows.Count, i).End(xlUp).Row
            Source.Range(Cells(1, i), Cells(lr, i)).copy Target.Columns(j)
               j = j + 1
            End If
        Next c
       Application.ScreenUpdating = True
        
    End Sub
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,697

    Re: Copy columns between worksheets but only until last row

    Sub Maybe()
    Dim c As Range, headerArr, i As Long
    headerArr = Array("Name", "Age")    '<----- extend as far as you need
    For i = LBound(headerArr) To UBound(headerArr)
        For Each c In Range(Cells(1, 1), Cells(1, Cells(Columns.Count).End(xlToLeft).Column))
            If c.Value = headerArr(i) Then Range(Cells(1, c.Column), Cells(c.End(xlDown).Row, c.Column)).Copy Sheets("Sheet2").Cells(1, Sheets("Sheet2").Cells(1, Columns.Count).End(xlToLeft).Column + 1): Exit For
        Next c
    Next i
    End Sub
    Change references where required


    Slight difference.
    Sub Maybe_A()
    Dim headerArr, sh1 As Worksheet, sh2 As Worksheet, cCol As Long, i As Long, nc As Long
    headerArr = Array("Name", "Age", "Address")    '<---- extend as far as required
    Set sh1 = Worksheets("Data")
    Set sh2 = Worksheets("dont_touch")
        For i = LBound(headerArr) To UBound(headerArr)
            With sh1
                cCol = .Rows(1).Find(headerArr(i)).Column
                nc = IIf(WorksheetFunction.CountA(sh2.Rows(1)) = 0, 1, sh2.Cells(1, sh2.Columns.Count).End(xlToLeft).Column + 1)
                .Range(.Cells(1, cCol), .Cells(.Cells(.Rows.Count, cCol).End(xlUp).Row, cCol)).Copy sh2.Cells(1, nc)
            End With
        Next i
    End Sub
    Last edited by jolivanes; 08-23-2019 at 02:05 AM.

  5. #5
    Registered User
    Join Date
    08-06-2019
    Location
    Karlsruhe, Germany
    MS-Off Ver
    Office 365
    Posts
    24

    Re: Copy columns between worksheets but only until last row

    Hey guys,
    sorry for the late reply. I was out of office for a week.

    I tried all your options and I am very thankful for your replies (!!!), but none of them really do the thing.

    I attached the file.
    When you add a new name in "Data", the copy-function is called and in "dont_touch" it will appear. But you can see then, that the whole column until the infinites cell was copied.
    Therefore, when you then try to open the "Sternenhimmel" worksheet, Excel will crash.
    How can I make the copy function copy only until the last used row?

    Best regards!
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy Columns to new created worksheets (with 3 fixed columns)
    By DHDan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-10-2019, 05:40 PM
  2. Copy Columns between Worksheets (keeping fomulas)
    By wizzz_wizzz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-03-2018, 01:23 AM
  3. [SOLVED] VBA to copy certain Columns from certain worksheets to two different master sheets
    By Jsonic5280 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-08-2016, 10:09 PM
  4. Copy Multiple Worksheets into new columns
    By Edwardanson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2013, 09:11 PM
  5. How to copy Non adjacent columns from many worksheets
    By para quality in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-30-2009, 07:14 AM
  6. Copy different columns from multiple worksheets into 1 worksheet
    By dakke in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-16-2008, 05:50 PM
  7. How do I copy columns between worksheets if the columns don't matc
    By Miriam in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 06-12-2006, 11:35 AM

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