How to copy columns from one sheet to another empty column in new sheet with Macros

%3CLINGO-SUB%20id%3D%22lingo-sub-1412547%22%20slang%3D%22en-US%22%3EHow%20to%20copy%20columns%20from%20one%20sheet%20to%20another%20empty%20column%20in%20new%20sheet%20with%20Macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1412547%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20copy%208%20columns%20(C18%3AJ167%20-%20with%20some%20merged%20cells)%20from%20Sheet%20%22Assignments%22%20to%20the%20next%20empty%20columns%20(starting%20with%20B4%20in%20Sheet%20%22Productivity%20Weekly%22.%20Below%20is%20my%20code.%20However%2C%20the%20problems%20I'm%20facing%3B%3C%2FP%3E%3COL%3E%3CLI%3EIt%20pastes%20it%20starting%20in%20row%20B27154%3C%2FLI%3E%3CLI%3EIt%20only%20pastes%20the%20values%20(not%20format)%3C%2FLI%3E%3CLI%3E%3CP%3EThe%20next%20time%20I%20run%20the%20macros%2C%20it%20puts%20the%20copied%20information%20in%20the%20same%20cells%2C%20not%20the%20next%20column.%20(so%20first%20time%20would%20be%20pasting%20in%20columns%20B%3AK%2C%20the%20next%20time%20columns%20L%3AS)%3C%2FP%3E%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20copycolumns()%0A%0ADim%20lastrow%20As%20Long%0ADim%20LastColumn%20As%20Long%0A%0AWith%20Sheets(%22Productivity%20Weekly%22)%0A%20%20%20%20lastrow%20%3D%20Cells(Rows.Count%2C%201).End(xlUp).Row%0A%20%20%20LastColumn%20%3D%20Cells(1%2C%20Columns.Count).End(xlToLeft).Column%0AEnd%20With%0A%0A%0ASheets(%22Assignments%22).Range(%22C18%3Aj167%22).Copy%0ASheets(%22Productivity%20Weekly%22).Range(%22b%22%20%26amp%3B%20LastColumn%20%2B%201%20%26amp%3B%20lastrow%20%2B%201).PasteSpecial%20Paste%3A%3DxlPasteValues%0A%0AApplication.CutCopyMode%20%3D%20False%0AApplication.EnableEvents%20%3D%20True%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1412547%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1413205%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20copy%20columns%20from%20one%20sheet%20to%20another%20empty%20column%20in%20new%20sheet%20with%20Macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1413205%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F677317%22%20target%3D%22_blank%22%3E%40davidtaillefer%3C%2FA%3E%26nbsp%3BFirst%20of%20all%2C%20tho%20part%20of%20the%20code%20where%20you%20determine%20to%20target%20cell%20is%20not%20correct.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ERange(%22b%22%20%26amp%3B%20LastColumn%20%2B%201%20%26amp%3B%20LastRow%20%2B%201)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3ESuppose%20LastColumn%20%3D%2026%20and%20LastRow%20%3D153%2C%20the%20above%20line%20of%20code%20translates%20to%20%22B%22%20%26amp%3B%2026%2B1%20%26amp%3B%20153%2B1%2C%20which%20results%20in%20a%20cell%20reference%20%22B27154%22%3C%2FP%3E%3CP%3EI%20believe%20the%20following%20piece%20of%20code%20does%20what%20you%20want.%20It%20copies%20values%20and%20formats%20from%20the%20first%20sheet%20into%20the%20next%20empty%20column%20in%20the%20second%20sheet%20from%20row%204.%20If%20B4%20in%20the%20second%20sheet%20is%20empty%20(like%20the%20very%20first%20time%20you%20perform%20the%20copy%2Fpaste%20operation%20to%20an%20empty%20sheet)%20it%20will%20start%20in%20in%20B4.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESub%20copycolumns()%0A%0ADim%20TargetSheet%20As%20Object%0ASet%20TargetSheet%20%3D%20Sheets(%22Productivity%20Weekly%22)%0A%0ADim%20TargetColumn%20As%20Integer%0A%0ATargetColumn%20%3D%20TargetSheet.Range(%22B4%22).CurrentRegion.Columns.Count%20%2B%202%0A%0AIf%20TargetSheet.Range(%22B4%22)%20%3D%20%22%22%20Then%0A%20%20%20%20TargetColumn%20%3D%202%0AEnd%20If%0A%0ASheets(%22Assignments%22).Range(%22C18%3Aj167%22).Copy%0A%0ATargetSheet.Activate%0ATargetSheet.Cells(4%2C%20TargetColumn).Select%0A%0ASelection.PasteSpecial%20Paste%3A%3DxlPasteAllUsingSourceTheme%2C%20Operation%3A%3DxlNone%20_%0A%20%20%20%20%20%20%20%20%2C%20SkipBlanks%3A%3DFalse%2C%20Transpose%3A%3DFalse%0ASelection.PasteSpecial%20Paste%3A%3DxlPasteValues%2C%20Operation%3A%3DxlNone%2C%20SkipBlanks%20_%0A%20%20%20%20%20%20%20%20%3A%3DFalse%2C%20Transpose%3A%3DFalse%0A%20%20%20%20%20%20%20%20%0AApplication.CutCopyMode%20%3D%20False%0A%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3BNot%20the%20most%20elegant%20piece%20of%20programming%2C%20but%20it%20works%20for%20the%20task%20you%20want%20to%20perform.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1416021%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20copy%20columns%20from%20one%20sheet%20to%20another%20empty%20column%20in%20new%20sheet%20with%20Macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1416021%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%20Thank%20you.%26nbsp%3B%20%26nbsp%3BTwo%20issues%3A%3C%2FP%3E%3CP%3EWhen%20I%20run%20this%2C%20I%20get%20an%20error.%20%22Run%20Time%20Error%3A%20'1004'%3A%20To%20do%20this%20all%20the%20Merged%20Cells%20must%20be%20the%20same%20size.%20%22%3C%2FP%3E%3CP%3EIt%20is%20happening%20in%20the%20part%20of%20the%20code%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3ESelection.PasteSpecial%20Paste%3A%3DxlPasteValues%2C%20Operation%3A%3DxlNone%2C%20SkipBlanks%20_%0A%20%20%20%20%20%20%20%20%3A%3DFalse%2C%20Transpose%3A%3DFalse%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20the%20second%20time%20that%20I%20run%20it%2C%20instead%20of%20starting%20to%20paste%20in%20J4%20(which%20would%20be%20the%20next%20empty%20column)%20it%20pastes%20in%20L4.%26nbsp%3B%20%26nbsp%3BThis%20means%20that%20the%203rd%20time%20running%20it%2C%20I%20get%20an%20error%20%22Run-time%20error%20'13'%3A%20Type%20Mismatch%22%3C%2FP%3E%3CP%3EThis%20is%20happening%20when%20it%20is%20running%20this%20part%20of%20the%20code%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3EIf%20TargetSheet.Range(%22B4%22)%20%3D%20%22%22%20Then%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20really%20appreciate%20the%20assistance.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1416179%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20copy%20columns%20from%20one%20sheet%20to%20another%20empty%20column%20in%20new%20sheet%20with%20Macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1416179%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F677317%22%20target%3D%22_blank%22%3E%40davidtaillefer%3C%2FA%3E%26nbsp%3BDifficult%20to%20debug%20without%20having%20your%20file%20to%20test%20on.%20The%20code%20I%20provided%20worked%20in%20a%20workbook%20in%20which%20I%20tried%20to%20replicate%20the%20situation%20you%20described.%20With%20regard%20the%20to%20the%20first%20error%2C%20get%20rid%20of%20merged%20cells.%20They%20are%20the%20source%20of%20much%20%22evil%22.%20Use%20%22Format%20cells%2C%20Alignment%2C%20Horizontal%2C%20Center%20across%20selection%22%20in%20stead%20(credit%20to%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9832%22%20target%3D%22_blank%22%3E%40Wyn%20Hopkins%3C%2FA%3E%26nbsp%3Bwho%20mentioned%20this%20in%20a%20recent%20live%20session%20on%20this%20site).%20A%20few%20more%20clicks%20but%20it%20avoids%20the%20problems%20when%20working%20with%20merged%20cells.%3C%2FP%3E%3CP%3EDon't%20really%20understand%20why%20the%20same%20macro%20would%20run%20into%20different%20errors%20for%20every%20time%20it%20is%20run.%20So%2C%20can%20you%20upload%20your%20file%20(without%20any%20confidential%20information%2C%20that%20is)%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1416507%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20copy%20columns%20from%20one%20sheet%20to%20another%20empty%20column%20in%20new%20sheet%20with%20Macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1416507%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3BOk.%26nbsp%3B%20I've%20unmerged%20those%20cells.%26nbsp%3B%20That%20worked%20out%20perfect.%26nbsp%3B%20%26nbsp%3BThen%20I%20adjusted%20the%20code%2C%20removing%20a%20%2B2%20in%20the%205th%20line.%26nbsp%3B%20It%20works%20great%20now.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20appreciate%20all%20of%20the%20assistance.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

I am trying to copy 8 columns (C18:J167 - with some merged cells) from Sheet "Assignments" to the next empty columns (starting with B4 in Sheet "Productivity Weekly". Below is my code. However, the problems I'm facing;

  1. It pastes it starting in row B27154
  2. It only pastes the values (not format)
  3. The next time I run the macros, it puts the copied information in the same cells, not the next column. (so first time would be pasting in columns B:K, the next time columns L:S)

 

Sub copycolumns()

Dim lastrow As Long
Dim LastColumn As Long

With Sheets("Productivity Weekly")
    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
   LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
End With


Sheets("Assignments").Range("C18:j167").Copy
Sheets("Productivity Weekly").Range("b" & LastColumn + 1 & lastrow + 1).PasteSpecial Paste:=xlPasteValues

Application.CutCopyMode = False
Application.EnableEvents = True

End Sub

 

4 Replies
Highlighted

@davidtaillefer First of all, tho part of the code where you determine to target cell is not correct.

Range("b" & LastColumn + 1 & LastRow + 1)

Suppose LastColumn = 26 and LastRow =153, the above line of code translates to "B" & 26+1 & 153+1, which results in a cell reference "B27154"

I believe the following piece of code does what you want. It copies values and formats from the first sheet into the next empty column in the second sheet from row 4. If B4 in the second sheet is empty (like the very first time you perform the copy/paste operation to an empty sheet) it will start in in B4.

Sub copycolumns()

Dim TargetSheet As Object
Set TargetSheet = Sheets("Productivity Weekly")

Dim TargetColumn As Integer

TargetColumn = TargetSheet.Range("B4").CurrentRegion.Columns.Count + 2

If TargetSheet.Range("B4") = "" Then
    TargetColumn = 2
End If

Sheets("Assignments").Range("C18:j167").Copy

TargetSheet.Activate
TargetSheet.Cells(4, TargetColumn).Select

Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
        , SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
Application.CutCopyMode = False


End Sub

 Not the most elegant piece of programming, but it works for the task you want to perform.

Highlighted

@Riny_van_Eekelen  Thank you.   Two issues:

When I run this, I get an error. "Run Time Error: '1004': To do this all the Merged Cells must be the same size. "

It is happening in the part of the code:

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

 

Then the second time that I run it, instead of starting to paste in J4 (which would be the next empty column) it pastes in L4.   This means that the 3rd time running it, I get an error "Run-time error '13': Type Mismatch"

This is happening when it is running this part of the code;

If TargetSheet.Range("B4") = "" Then

 

I really appreciate the assistance.

Highlighted

@davidtaillefer Difficult to debug without having your file to test on. The code I provided worked in a workbook in which I tried to replicate the situation you described. With regard the to the first error, get rid of merged cells. They are the source of much "evil". Use "Format cells, Alignment, Horizontal, Center across selection" in stead (credit to @Wyn Hopkins who mentioned this in a recent live session on this site). A few more clicks but it avoids the problems when working with merged cells.

Don't really understand why the same macro would run into different errors for every time it is run. So, can you upload your file (without any confidential information, that is)?

Highlighted

@Riny_van_Eekelen Ok.  I've unmerged those cells.  That worked out perfect.   Then I adjusted the code, removing a +2 in the 5th line.  It works great now.

 

I appreciate all of the assistance.