Copy/paste VBA with issues

pete4monc

Board Regular
Joined
Jan 12, 2014
Messages
85
Office Version
  1. 365
Platform
  1. Windows
I have a simple vba code that works absolutely fine, it copies data from one Workbook 1 column A and pastes it into Workbook 2 column A.

Problem is workbook 1 is updated weekly and the data in some cells in column A change and I do not want the original data copied over to Workbook 2 to be over written every time the vba is run.
I need some help writing some vba that will copy over only new data from workbook1 and not delete data already previously copied over into Workbook2 ( ie: Workbook 2 Column A will just get longer and longer)

Hopefully I have explained it well enough so you can understand.

Sub CopyColumnToWorkbook()
Dim sourceColumn As Range, targetColumn As Range

Set sourceColumn = Workbooks("Bought-Out-Part Orders.xlsx").Worksheets("Live").Columns("A")
Set targetColumn = Workbooks("BOP Orders.xlsm").Worksheets("Sheet3").Columns("A")

sourceColumn.Copy Destination:=targetColumn
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach screenshots (not a pictures) of your two sheets.
Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Maybe try something like this:
VBA Code:
Sub CopyColumnToWorkbook()
Dim sourceColumn As Range, targetColumn As Range
Dim lrs As Long
Dim lrt As Long

'Find last rows on each sheet
lrs = Workbooks("Bought-Out-Part Orders.xlsx").Worksheets("Live").Cells(Rows.Count, "A").End(xlUp).Row
lrt = Workbooks("BOP Orders.xlsm").Worksheets("Sheet3").Cells(Rows.Count, "A").End(xlUp).Row

'See if new data was added, and if so, copy
If lrs > lrt Then
    Workbooks("Bought-Out-Part Orders.xlsx").Worksheets("Live").Range(Cells(lrt + 1, "A"), Cells(lrs, "A")).Copy _
        Destination:=Workbooks("BOP Orders.xlsm").Worksheets("Sheet3").Cells(lrt + 1, "A")
End If

End Sub
Note: I am not sure if it will let you find the last row without being in that workbook, so you may need to activate it first if the code does not work "as-is".
 
Last edited:
Upvote 0
Maybe try something like this:
VBA Code:
Sub CopyColumnToWorkbook()
Dim sourceColumn As Range, targetColumn As Range
Dim lrs As Long
Dim lrt As Long

'Find last rows on each sheet
lrs = Workbooks("Bought-Out-Part Orders.xlsx").Worksheets("Live").Cells(Rows.Count, "A").End(xlUp).Row
lrt = Workbooks("BOP Orders.xlsm").Worksheets("Sheet3").Cells(Rows.Count, "A").End(xlUp).Row

'See if new data was added, and if so, copy
If lrs > lrt Then
    Workbooks("Bought-Out-Part Orders.xlsx").Worksheets("Live").Range(Cells(lrt + 1, "A"), Cells(lrs, "A")).Copy _
        Destination:=Workbooks("BOP Orders.xlsm").Worksheets("Sheet3").Cells(lrt + 1, "A")
End If

End Sub
Note: I am not sure if it will let you find the last row without being in that workbook, so you may need to activate it first if the code does not work "as-is".
Hi Joe
Unfortunately nothing seems to happen when I try your code. I have both workbooks open, maybe it is something i am missing?
 
Upvote 0
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach screenshots (not a pictures) of your two sheets.
Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
Hi Mumps

First time using XL2BB so I hope it comes out right.
Basically the 1st workbook needs to look into the 2nd workbook column A, find any new entries not already in workbook1.
Enter the MSO number into the 1st workbook without over writing the original data in column A.
New entries could be inserted (not always the last row entry) or deleted in workbook 2 in any order.
Once the orders are completed they will be removed from workbook1 but still need to remain in workbook2.
There will be an issue with the lookup ranges but I shall look at a vba to copy cells based on the MSO number and certain cells later.

Many thanks for your help and please let me know if the XL2BB thing has worked as it should?

Cell Formulas
RangeFormula
B2:B12B2=INDEX('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$L$2:$L$500,AGGREGATE(15,6,(ROW('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$L$2:$L$500)-ROW('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$L$2)+1)/('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$A$2:$A$500=$A2),COUNTIFS($A$2:$A2,$A2)))
C2:C12C2=INDEX('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$D$2:$D$500,AGGREGATE(15,6,(ROW('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$D$2:$D$500)-ROW('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$D$2)+1)/('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$A$2:$A$500=$A2),COUNTIFS($A$2:$A2,$A2)))
D2:D12D2=INDEX('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$E$2:$E$500,AGGREGATE(15,6,(ROW('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$E$2:$E$500)-ROW('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$E$2)+1)/('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$A$2:$A$500=$A2),COUNTIFS($A$2:$A2,$A2)))
E2:E12E2=INDEX('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$F$2:$F$500,AGGREGATE(15,6,(ROW('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$F$2:$F$500)-ROW('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$F$2)+1)/('V:\Purchasing\BOP\[Bought-Out-Part Orders.xlsx]Live'!$A$2:$A$500=$A2),COUNTIFS($A$2:$A2,$A2)))


Bought-Out-Part Orders.xlsx
ABCDEFGHI
1Order NoItemAlloyCK noDescriptionSales qtyStockSupplierPO number
2MSO24260W018094HR33PRP21/126Support Grid1600
3MSO24390M045966MCHR36ACKYN87Subcon Machining - Upper Centre Platen10Mollart EngineeringMP43363
4MSO24390M045969MCHR36ACKYN88Subcon Machining - Upper Outer Platen10Mollart EngineeringMP43363
5MSO24390M045970MCHR36ACKYN89Subcon Machining - Upper Outer Platen10Mollart EngineeringMP43363
6MSO24390M045966HTHR36ACKYN87Subcon Heat Treat - Upper Centre Platen10Thermofax
7MSO24390M045969HTHR36ACKYN88Subcon Heat Treat - Upper Outer Platen10Thermofax
8MSO24390M045970HTHR36ACKYN89Subcon Heat Treat - Upper Outer Platen10Thermofax
9MSO24412A027535HR13COCKYN90Subcon Machine, Heat Treat & Assemble - SPF Tool10Cube Precison EngineeringMP42834
10MSO24414N025702S310CKE218Bar ø1 1/2" x 14" Long20Applied AlloysMP44120
11MSO24414N025703S310CKE219Bar ø1 1/2" x 17" Long20Applied AlloysMP44120
12MSO24745N049507304CKT129HANDLE C/W LUG YK79100153800AlmorMP43589
Live
Cell Formulas
RangeFormula
D2:D12D2=VLOOKUP(B2,Items!$1:$65004,3,FALSE)
E2:E12E2=VLOOKUP(B2,Items!$1:$65004,2,FALSE)
 
Upvote 0
Yeah, just as I thought, you may need to activate the one sheet.
This seems to work, regardless of which file you are in to start:
VBA Code:
Sub CopyColumnToWorkbook()
Dim sourceColumn As Range, targetColumn As Range
Dim lrs As Long
Dim lrt As Long

'Find last rows on each sheet
lrs = Workbooks("Bought-Out-Part Orders.xlsx").Worksheets("Live").Cells(Rows.Count, "A").End(xlUp).Row
lrt = Workbooks("BOP Orders.xlsm").Worksheets("Sheet3").Cells(Rows.Count, "A").End(xlUp).Row

'See if new data was added, and if so, copy
If lrs > lrt Then
    Workbooks("Bought-Out-Part Orders.xlsx").Activate
    Set sourceColumn = Workbooks("Bought-Out-Part Orders.xlsx").Worksheets("Live").Range(Cells(lrt + 1, "A"), Cells(lrs, "A"))
    Set targetColumn = Workbooks("BOP Orders.xlsm").Worksheets("Sheet3").Cells(lrt + 1, "A")
    sourceColumn.Copy Destination:=targetColumn
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,428
Members
448,961
Latest member
nzskater

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