VBA help - Copy and paste specific columns to new unnamed workbook

Mchy

New Member
Joined
Jan 22, 2022
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone, I am trying to copy specific columns from the active sheet, create a new workbook and paste the specific columns into the new workbook. However, my issue is that I manage to create a new workbook but was unable to copy the columns and paste. I need some help in this, much appreciated. My code is as below

Sub copy columns()
Dim lastrow as long
dim erow as long

Workbooks.add

Last row = activesheet.select - cells(rows.count, 1).end(xlup).row
for I = 1 to lastrow

Activesheet.select.cells(i, 1).copy
erow=sheet1.cells(rows.count, 1).end(xlup).offset(1,0).row

Activesheet.select.paste destination:=worksheets("sheet1").activate.cells(erow,1)

Activesheet.select.cells(i, 2).copy
Activesheet.select.paste destination:=worksheets("sheet1").activate.cells(erow,2)

Activesheet.select.cells(i, 9).copy
Activesheet.select.paste destination:=worksheets("sheet1")activate.cells(erow,3)

Next i

Application.cutcopymode = false
range("A1").select

End sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
It seems to me that you're trying to copy columns A, B and I (1, 2 & 9) from the active sheet into columns A, B and C in a new workbook? If that's the case, then the following code should give you that.

VBA Code:
Option Explicit
Sub CopyColumns()
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ActiveSheet
    Set wb = Workbooks.Add
    Set ws2 = wb.Sheets(1)
    
    With ws1
        .Columns(1).Copy ws2.Cells(1, 1)
        .Columns(2).Copy ws2.Cells(1, 2)
        .Columns(9).Copy ws2.Cells(1, 3)
    End With
End Sub
 
Upvote 0
It seems to me that you're trying to copy columns A, B and I (1, 2 & 9) from the active sheet into columns A, B and C in a new workbook? If that's the case, then the following code should give you that.

VBA Code:
Option Explicit
Sub CopyColumns()
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ActiveSheet
    Set wb = Workbooks.Add
    Set ws2 = wb.Sheets(1)
   
    With ws1
        .Columns(1).Copy ws2.Cells(1, 1)
        .Columns(2).Copy ws2.Cells(1, 2)
        .Columns(9).Copy ws2.Cells(1, 3)
    End With
End Sub
Hi Kevin, firstly I would like to thank you for assisting me. The code works fine! However, when I run it, the file save screen keeps popping out and I have to keep pressing cancel in order to update the rest of the columns. Is there a way to maneuver this situation?
 
Upvote 0
Hi Kevin, firstly I would like to thank you for assisting me. The code works fine! However, when I run it, the file save screen keeps popping out and I have to keep pressing cancel in order to update the rest of the columns. Is there a way to maneuver this situation?
It seems to me that you're trying to copy columns A, B and I (1, 2 & 9) from the active sheet into columns A, B and C in a new workbook? If that's the case, then the following code should give you that.

VBA Code:
Option Explicit
Sub CopyColumns()
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ActiveSheet
    Set wb = Workbooks.Add
    Set ws2 = wb.Sheets(1)
   
    With ws1
        .Columns(1).Copy ws2.Cells(1, 1)
        .Columns(2).Copy ws2.Cells(1, 2)
        .Columns(9).Copy ws2.Cells(1, 3)
    End With
End Sub
The "Update values" screen keeps popping out
 
Upvote 0
The issue you describe usually happens when your file has references to other external files (links) which I didn't experience when I tested the code before I posted it. You can try the amended code to see if it fixes the issue, otherwise I can't help without having the actual file itself.

VBA Code:
Option Explicit
Sub CopyColumns()
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ActiveSheet
    Set wb = Workbooks.Add
    Set ws2 = wb.Sheets(1)
    
    Application.DisplayAlerts = 0
    With ws1
        .Columns(1).Copy ws2.Cells(1, 1)
        .Columns(2).Copy ws2.Cells(1, 2)
        .Columns(9).Copy ws2.Cells(1, 3)
    End With
    Application.DisplayAlerts = 1
End Sub
 
Upvote 0
The issue you describe usually happens when your file has references to other external files (links) which I didn't experience when I tested the code before I posted it. You can try the amended code to see if it fixes the issue, otherwise I can't help without having the actual file itself.

VBA Code:
Option Explicit
Sub CopyColumns()
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ActiveSheet
    Set wb = Workbooks.Add
    Set ws2 = wb.Sheets(1)
   
    Application.DisplayAlerts = 0
    With ws1
        .Columns(1).Copy ws2.Cells(1, 1)
        .Columns(2).Copy ws2.Cells(1, 2)
        .Columns(9).Copy ws2.Cells(1, 3)
    End With
    Application.DisplayAlerts = 1
End Sub
Wow, you're awesome! it worked. Can it be pasted as values instead? As the values pasted are in the formula.
 
Upvote 0
Wow, you're awesome! it worked. Can it be pasted as values instead? As the values pasted are in the formula.
VBA Code:
Option Explicit
Sub CopyColumns()
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ActiveSheet
    Set wb = Workbooks.Add
    Set ws2 = wb.Sheets(1)
    
    Application.DisplayAlerts = 0
    With ws1
        .Columns(1).Copy
        ws2.Cells(1, 1).PasteSpecial xlPasteValues
        .Columns(2).Copy
        ws2.Cells(1, 2).PasteSpecial xlPasteValues
        .Columns(9).Copy
        ws2.Cells(1, 3).PasteSpecial xlPasteValues
        Application.CutCopyMode = 0
    End With
    Application.DisplayAlerts = 1
End Sub
 
Upvote 0
VBA Code:
Option Explicit
Sub CopyColumns()
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ActiveSheet
    Set wb = Workbooks.Add
    Set ws2 = wb.Sheets(1)
   
    Application.DisplayAlerts = 0
    With ws1
        .Columns(1).Copy
        ws2.Cells(1, 1).PasteSpecial xlPasteValues
        .Columns(2).Copy
        ws2.Cells(1, 2).PasteSpecial xlPasteValues
        .Columns(9).Copy
        ws2.Cells(1, 3).PasteSpecial xlPasteValues
        Application.CutCopyMode = 0
    End With
    Application.DisplayAlerts = 1
End Sub
Thank you, is there a way to copy filtered columns? i.e. visible columns instead of the entire column?
 
Upvote 0
Try this.

VBA Code:
Option Explicit
Sub CopyColumns()
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = ActiveSheet
    Set wb = Workbooks.Add
    Set ws2 = wb.Sheets(1)
    
    Application.DisplayAlerts = 0
    With ws1
        .Columns(1).SpecialCells(12).Copy
        ws2.Cells(1, 1).PasteSpecial xlPasteValues
        .Columns(2).SpecialCells(12).Copy
        ws2.Cells(1, 2).PasteSpecial xlPasteValues
        .Columns(9).SpecialCells(12).Copy
        ws2.Cells(1, 3).PasteSpecial xlPasteValues
        Application.CutCopyMode = 0
    End With
    Application.DisplayAlerts = 1
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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