Macro for Copy/Pasting a sequence of columns

ericvanlooy

New Member
Joined
Dec 1, 2017
Messages
4
Hi,

I would greatly appreciate it if you could help (and teach!) this Newbie to macros with the following issue:

I have a workbook with multiple columns containing consolidated data, of which the content needs to be split up into a number of new workbooks.

The data that needs to be copied/pasted into the new workbooks follows a pattern, which I'm sure can be programmed into a Macro.

For instance:
The first 4 columns are always copied into the new workbooks, but column 5 will be variable:
- New workbook 1 will also contain the values from column E
- New workbook 2 will also contain the values from column H (E+3)
- New workbook 3 will also contain the values from column K (E+6)
- New workbook 4 will also contain the values from column N (E+9)

etc...

The copy/paste macro needs to end when the value in row 1 of the 5th column is called "Source"

For now, I have a very manual Macro (based on a recording), but I have a number of Workbooks (with an unequal amount of columns) I would like to apply this Macro on.

This is what I have to give you an idea:


Dim thisWb As Workbook

Set thisWb = ActiveWorkbook

Application.DisplayAlerts = False
Columns("A:E").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ChDir _
thisWb.Path
ActiveWorkbook.SaveAs Filename:= _
thisWb.Path & "" & _
thisWb.ActiveSheet.Name & "-" & ActiveSheet.Range("E1").Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close
thisWb.Activate
Range("A:D,H:H").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ChDir _
thisWb.Path
ActiveWorkbook.SaveAs Filename:= _
thisWb.Path & "" & _
thisWb.ActiveSheet.Name & "-" & ActiveSheet.Range("E1").Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close
thisWb.Activate
Range("A:D,K:K").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ChDir _
thisWb.Path
ActiveWorkbook.SaveAs Filename:= _
thisWb.Path & "" & _
thisWb.ActiveSheet.Name & "-" & ActiveSheet.Range("E1").Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close

thisWb.Close
Application.DisplayAlerts = True
End Sub


Thanks in advance for your help!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Can you clarify what you mean by:
The copy/paste macro needs to end when the value in row 1 of the 5th column is called "Source"
You want columns A:D in all workbooks. Are the fifth columns not always H, K and N? I'm not sure where the
5th column is called "Source"
fits in.
 
Upvote 0
Hi & welcome to the board.
Try this
Code:
Sub Copy2NewBook()
    Dim thisWs As Worksheet
    Dim Pth As String
    Dim Rng As Range
    Dim Cnt As Long
    
    Pth = ThisWorkbook.path
    Set thisWs = ActiveSheet
    
Application.DisplayAlerts = False
Application.ScreenUpdating = False

    Set Rng = Columns("A:D")
    For Cnt = 5 To thisWs.Cells(1, Columns.Count).End(xlToLeft).column Step 3
        Workbooks.Add
        Rng.copy Range("A1")
        thisWs.Columns(Cnt).copy Range("E1")
        ActiveWorkbook.SaveAs FileName:=Pth & "\" & _
            thisWs.Name & "-" & ActiveSheet.Range("E1").Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close , False
    Next Cnt

    thisWb.Close
    Application.DisplayAlerts = True
End Sub
It will copy cols A:D to each new workbook along with col E then H etc till there are no more columns
 
Upvote 0
Can you clarify what you mean by: You want columns A:D in all workbooks. Are the fifth columns not always H, K and N? I'm not sure where the fits in.

I realise it sounds a bit vague.

Yes, all new workbooks will contain columns A:D, but all should have a different 5th column. In the first new workbook, that would be column E, but in the second one that will be H, ... and so on. The fifth column in each new workbook should always be every other 3rd column from the source file (column E for new workbook 1, column H for new workbook 2) until you reach a column where the top cell value is called "Source"

Attached some screenshots that may clarify:



 
Upvote 0
I don't see anything wrong with Fluff's solution; did you try that? The only possible amendment is a check to see if E1 contains the word "Source" and, if so, exit the For loop.

WBD
 
Upvote 0
Modified version to account for Source
Code:
Sub Copy2NewBook()
    Dim thisWs As Worksheet
    Dim Pth As String
    Dim Rng As Range
    Dim Cnt As Long
    Dim FnlCol As Long
    
    Pth = ThisWorkbook.path
    Set thisWs = ActiveSheet
    FnlCol = thisWs.Rows(1).Find("Source", , , xlWhole, , , False, , False)
    
Application.DisplayAlerts = False
Application.ScreenUpdating = False

    Set Rng = Columns("A:D")
    For Cnt = 5 To FnlCol Step 3
        Workbooks.Add
        Rng.copy Range("A1")
        thisWs.Columns(Cnt).copy Range("E1")
        ActiveWorkbook.SaveAs FileName:=Pth & "\" & _
            thisWs.Name & "-" & ActiveSheet.Range("E1").Value & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close , False
    Next Cnt

    thisWb.Close
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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