View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ingo Ingo is offline
external usenet poster
 
Posts: 9
Default Macro copy data into another workbook

I found this small macro which copies the data from my source worksheet
("Data4") Range("A1:C1000") to a different workbook ("C:\Destination.xls").
What it does is that it will look for the next empty row and then copy the
data into it. Now I would like to alter the macro so instead of always
copying into the next empty row (and eventually create duplicates) it should
check for the whole range if an entry from column A in the source range
already exists in column A of the detination workbook and in this case
overwrite column B and C of this row in the destination workbook. In case it
does not exist yet, it should write the data in the empty row as the macro
below does already. One more note: the data of the cells in column A of the
source worksheet is always unique, there are no duplicates already in the
source worksheet. Thanks for your help!

Sub Test()
Dim bk As Workbook
Dim bSave As Boolean
Dim lRow As Long

' test to see if Destination.xls is already open

On Error Resume Next
Set bk = Workbooks("Destination.xls")
On Error GoTo 0
If bk Is Nothing Then
bSave = True
Set bk = Workbooks.Open("C:\Destination.xls")
End If

'find first empty row in database
lRow = bk.Worksheets("Test").Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
ThisWorkbook.Sheets("Data4").Range("A1:C1000").Cop y _
Destination:=bk.Worksheets("Test").Cells(lRow, 1)

' if destination was originally closed, then save and close it

If bSave Then bk.Close Savechanges:=True

End Sub