insert column in another workbook on a sheet1 from current workbook

Fordraiders
Fordraiders used Ask the Experts™
on
excel vba
I have data i'm copying to another workbook, from my current workbook.

Dim curWks As Worksheet
Dim templWks As Worksheet
Dim rngToCopy As Range

  
Set curWks = ActiveSheet
With curWks

Set rngToCopy = .Range("A1:AX65453", .Range("a1").End(xlToRight).End(xlDown))
End With

Workbooks.Open _
fileName:="C:\Program Files\enterprise\Customer Copy\Customer_Template.xlsx"


If ActiveSheet.Name = "Project Data" Then
Else
Sheets("project Data").Activate
End If

Set templWks = ActiveSheet
templWks.Cells.Select
templWks.Cells.Clear

' APPENDS DATA
rngToCopy.Copy _
Destination:=templWks.Range("A65453").End(xlUp)


' after i copy data i need to insert a column at  Column P


Workbooks("Customer_Template.xlsx").Close SaveChanges:=True
Application.CutCopyMode = False

Open in new window



What I need:
after i copy data i need to insert a column at  Column P in the other workbook.
And give the Column Header a name "Customer  Price"


Thanks
fordraiders
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Managing Director/Excel VBA Developer
Distinguished Expert 2018
Commented:
Hi,

Try below:
Sub CopyData()
Dim TrgtWB As Workbook
Dim curWks As Worksheet
Dim templWks As Worksheet
Dim rngToCopy As Range

Set curWks = ActiveSheet
With curWks

Set rngToCopy = .Range("A1:AX65453", .Range("a1").End(xlToRight).End(xlDown))
End With

Set TrgtWB = Workbooks.Open(Filename:="C:\Program Files\enterprise\Customer Copy\Customer_Template.xlsx")

If ActiveSheet.Name = "Project Data" Then
Else
TrgtWS.Activate
End If

Set templWks = ActiveSheet
templWks.Cells.Select
templWks.Cells.Clear

' APPENDS DATA
rngToCopy.Copy _
Destination:=templWks.Range("A65453").End(xlUp)


' after i copy data i need to insert a column at  Column P
templWks.Columns("P:P").Insert
templWks.Range("P1").Value = "Customer  Price"
TrgtWB.Close SaveChanges:=True
Application.CutCopyMode = False
End Sub

Open in new window

Author

Commented:
thanks !
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
Pls try below, I have edited above code:
Sub CopyData()
Dim TrgtWB As Workbook
Dim curWks As Worksheet
Dim templWks As Worksheet
Dim rngToCopy As Range

Set curWks = ActiveSheet
With curWks

Set rngToCopy = .Range("A1:AX65453", .Range("a1").End(xlToRight).End(xlDown))
End With

Set TrgtWB = Workbooks.Open(Filename:="C:\Program Files\enterprise\Customer Copy\Customer_Template.xlsx")

If ActiveSheet.Name = "Project Data" Then
Else
Sheets("project Data").Activate
End If

Set templWks = ActiveSheet
templWks.Cells.Select
templWks.Cells.Clear

' APPENDS DATA
rngToCopy.Copy _
Destination:=templWks.Range("A65453").End(xlUp)


' after i copy data i need to insert a column at  Column P
templWks.Columns("P:P").Insert
templWks.Range("P1").Value = "Customer  Price"
TrgtWB.Close SaveChanges:=True
Application.CutCopyMode = False
End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial