+ Reply to Thread
Results 1 to 14 of 14

Copy previous column range and paste next to last column

Hybrid View

  1. #1
    Registered User
    Join Date
    07-16-2014
    Location
    Washington
    MS-Off Ver
    2010
    Posts
    37

    Copy previous column range and paste next to last column

    Hi, I'm hoping someone can help point me in the right direction with a request I got today.

    We've got a spreadsheet with Columns A,B,C,D,E,F,G,H,I,J,K,L where the bold columns make up one month. Currently the five columns are copied and pasted moving left to right. In the above example, the 5 columns would be pasted to Columns M,N,O,P,Q and so on, for the 12 months. The first group of columns are static and are not touched.

    What I'm planning on doing is creating another spreadsheet with macros, buttons, a userform perhaps that will interact with the spreadsheet mentioned above. This is to keep the code and so on in a central location.

    So far with the help of web searching, I have this code which perfectly copies the last 5 columns then pastes them on the next blank column. Can this code be changed to work from another spreadsheet?

    Option Explicit
    
    Sub CopyLastFiveColumns()
    Dim LastColumn As Long
    LastColumn = Sheets("Sheet1").Cells.Find("*", [a1], , , xlByColumns, xlPrevious).Column
    On Error GoTo 0
    
    Sheets("Sheet1").Columns(LastColumn - 4).Resize(, 5).Select
    Selection.Copy
    
    '   Enter the rest of your paste code here
    
    Sheets("Sheet1").Columns(LastColumn + 1).Select
    ActiveSheet.Paste
    
    Selection(1).Select
    Application.CutCopyMode = False
    End Sub
    Last edited by askpcguy; 08-18-2014 at 09:54 PM.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy previous column range and paste next to last column

    Quote Originally Posted by askpcguy View Post
    Can this code be changed to work from another spreadsheet?
    Yes it can.

    Thing is, you want to make changes to a working code, to do that, you should give more details. Running it for a different spreadsheet is simply changing Sheet1 to the name of the other spreadsheet.
    Sheets("Sheet1")
    But, by 'spreadsheet', do you mean a new worksheet, or a new workbook? If you meant a workbook, the code will get slightly more tedious.
    多么想要告诉你 我好喜欢你

  3. #3
    Registered User
    Join Date
    07-16-2014
    Location
    Washington
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy previous column range and paste next to last column

    Yes, by spreadsheet I meant new workbook. For example the attached spreadsheets Master.xlsmWorkbook1.xlsx

    The macro/vba will be in Master.xlsm and copy columns in Workbook1.xlsx Right now the code is in Master.xlsm along with sample data but I'm unsure how to change the existing code to copy/paste columns in another workbook.

  4. #4
    Forum Contributor
    Join Date
    08-11-2012
    Location
    bengalur
    MS-Off Ver
    Excel 2003, 2007
    Posts
    152

    Re: Copy previous column range and paste next to last column

    I will help you on this. Give me some time.

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Copy previous column range and paste next to last column

    Hi, askpcguy,

    the first code could be like
    Sub CopyLastFiveColumns()
    Dim LastColumn As Long
    With Sheets("Sheet1")
      LastColumn = .Cells.Find("*", [a1], , , xlByColumns, xlPrevious).Column
      .Columns(LastColumn - 4).Resize(, 5).Copy .Columns(LastColumn + 1)
      Application.CutCopyMode = False
    End With
    End Sub
    I think we all know how an empty workbook will look like, you would need to specify if you only want one sheet in there for example.
    Sub CopyLastFiveColumnsOtherSheet()
    Dim LastColumn As Long
    With Sheets("Sheet1")
      LastColumn = .Cells.Find("*", [a1], , , xlByColumns, xlPrevious).Column
      Workbooks.Add
      .Columns(LastColumn - 4).Resize(, 5).Copy
      ActiveSheet.Paste
      Application.CutCopyMode = False
    End With
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Registered User
    Join Date
    07-16-2014
    Location
    Washington
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy previous column range and paste next to last column

    Quote Originally Posted by HaHoBe View Post
    Hi, askpcguy,

    the first code could be like
    Sub CopyLastFiveColumns()
    Dim LastColumn As Long
    With Sheets("Sheet1")
      LastColumn = .Cells.Find("*", [a1], , , xlByColumns, xlPrevious).Column
      .Columns(LastColumn - 4).Resize(, 5).Copy .Columns(LastColumn + 1)
      Application.CutCopyMode = False
    End With
    End Sub
    I think we all know how an empty workbook will look like, you would need to specify if you only want one sheet in there for example.
    Sub CopyLastFiveColumnsOtherSheet()
    Dim LastColumn As Long
    With Sheets("Sheet1")
      LastColumn = .Cells.Find("*", [a1], , , xlByColumns, xlPrevious).Column
      Workbooks.Add
      .Columns(LastColumn - 4).Resize(, 5).Copy
      ActiveSheet.Paste
      Application.CutCopyMode = False
    End With
    End Sub
    Ciao,
    Holger
    Hello HaHoBe,

    I included the blank workbook just to be complete. The code you have in your post is mostly similar to the code I put together from looking at examples online. Within a macro it will perfectly copy the last 5 columns. I'm trying to take that code and move it to another workbook where it will copy the last 5 columns of another named workbook/worksheet.

    millz, when I run the userform and select the workbook I get a run-time error 91, Object variable or With block variable not set. In fact it appears the code is going back to sheet 1 of the workbook chosen using the Browse button.

  7. #7
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy previous column range and paste next to last column

    In the attached file, I've made a simple form that allows you to choose to paste for an already open workbook, or a closed workbook saved somewhere. Try it and see how it goes. You can make further changes from there.
    Attached Files Attached Files

  8. #8
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy previous column range and paste next to last column

    Change the code for button1 to this and it should work as you wanted, but note that it pastes into the activesheet of the target workbook.
    Private Sub CommandButton1_Click()
        Dim f, v, wb As Workbook, wb2 As Workbook, LastColumn As Long, op As Boolean
        
        If Me.TextBox1.Value = "" And Me.ListBox1.ListIndex < 0 Then
            MsgBox "Select an open workbook, or browse to a workbook to be opened!"
            Exit Sub
        ElseIf Me.TextBox1.Value <> "" Then
            f = Me.TextBox1.Value
            Set wb = Application.Workbooks.Open(f)
        Else
            op = True
            f = Me.ListBox1.Value
            v = Split(f, "\")
            If UBound(v) > 0 Then f = v(UBound(v))
            Set wb = Application.Workbooks(f)
        End If
        
        Set wb2 = ActiveWorkbook
        
        With wb.ActiveSheet
            LastColumn = .Cells.Find("*", [a1], , , xlByColumns, xlPrevious).Column
            On Error GoTo 0
            
            '.Columns(LastColumn - 4).Resize(, 5).Copy .Columns(LastColumn + 1)
            wb2.ActiveSheet.Columns(1).Resize(, 5).Copy .Columns(LastColumn + 1)
            
            '   Enter the rest of your paste code here
            
            Application.CutCopyMode = False
        End With
        
        wb.Save
        If Not op Then wb.Close
        Set wb = Nothing
        Set wb2 = Nothing
        MsgBox "Columns copied!"
    End Sub

  9. #9
    Registered User
    Join Date
    07-16-2014
    Location
    Washington
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy previous column range and paste next to last column

    Hi Millz, apologies for the late reply as the work week ended. We had a look at this today and decided it's easier to start with the columns and formulas already in place for the 12 months. Then I can use the macro recorder to get the VBA needed to copy the source data and paste it where it needs to go. After that we just need to edit the VBA to grab the monthly data.

    Here's what I've got as a test, this is pulling actual data. Is there a way to put this into a macro or shorter code? Or should I post this into a new thread/question?
    Option Explicit
    
    Sub Transpose()
    '
    ' Transpose Macro, one month.
    '
        Windows("MyBook1.xlsm").Activate
        Range("AQ3").Select
        Windows("Data1.xls").Activate
        Range("J2").Select
        Application.CutCopyMode = False
        Range("J2:J6").Select
        Selection.Copy
        Windows("MyBook1.xlsm").Activate
        Range("AQ3:AU3").Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
            Range("AQ10").Select
        Windows("Data1.xls").Activate
        Range("J7").Select
        Application.CutCopyMode = False
        Range("J7:J11").Select
        Selection.Copy
        Windows("MyBook1.xlsm").Activate
        Range("AQ10:AU10").Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
        Selection(1).Select
        Application.CutCopyMode = False
    End Sub

  10. #10
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy previous column range and paste next to last column

    Hi askpcguy, I have no idea what you are after now. Also, I have no idea how your data structure looks like just by looking at your recorded macro for one month. I suggest starting a fresh thread as this is deviating from your original post already.

  11. #11
    Registered User
    Join Date
    07-16-2014
    Location
    Washington
    MS-Off Ver
    2010
    Posts
    37

    Re: Copy previous column range and paste next to last column

    Will do, thanks Millz!

+ 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. Loop range copy based on previous column
    By darkhangelsk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-12-2014, 03:38 PM
  2. [SOLVED] Macro to Concatenate, copy and paste the formula until the last row of the previous column
    By venkat.ramamoordhy1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-23-2013, 04:03 PM
  3. [SOLVED] Macro to Copy Formating from Previous Column and Paste into Last Column
    By rmmohan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-05-2013, 09:37 AM
  4. Copy/Paste to Next Empty Row Overwriting Previous Row When Column A Is Empty
    By grantfitz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2013, 04:34 PM
  5. Vba - find next empty column, insert column, copy paste values & copy paste formulas
    By DoodlesMama in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2012, 12:43 PM

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