Copying Data without overwriting (edit macro)

jimmynora

New Member
Joined
Oct 20, 2010
Messages
27
I have the following macro that copies data from sheet1 and pastes it to sheet2.

I want to edit this macro to not overwrite the data in sheet2 when pasting in the data from sheet1. it should only paste in the data if the respective cell is empty. if something is already there it should just move on to the next cell.

Previously what this macro did was it saved the old data as a comment in the respective cell (sheet2) and pasted in the data from sheet1.

JN


Code:
Option Explicit
Option Base 1

Sub CopyData()
Dim MyRg As Range
Dim MyData()
Dim SrchRg As Range
Dim MyVal As Range
Dim F
Dim ColSRC, ColDEST
Dim I As Integer
Dim TextVal As String
Dim NbCol As Integer
    NbCol = 3           '  NB OF COLUMNS TO TREAT
    ReDim MyData(NbCol)
    ColSRC = Array("D", "F", "H")  '  HERE  TO MENTION COLUMNS IN SHEET 1  WHERE TO GET DATA
    ColDEST = Array("F", "D", "I") '  HERE  TO MENTION COLUMNS IN SHEET 2  WHERE TO PÜT DATA
    
    With Sheets("Sheet1")
        Set MyRg = .Range(.Range("C5"), .Range("C" & Rows.Count).End(xlUp))
        On Error Resume Next
        For Each MyVal In MyRg
            For I = 1 To NbCol
                MyData(I) = .Cells(MyVal.Row, ColSRC(I))
            Next I
            
            With Sheets("Sheet2")
                Set SrchRg = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
                With SrchRg
                    Set F = .Find(What:=MyVal, After:=.Cells(1, 1), LookIn:=xlValues, _
                            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                            MatchCase:=False)
                    If (Not F Is Nothing) Then
                        For I = 1 To NbCol
                            With .Cells(F.Row, ColDEST(I))
                                If (.Value <> Empty) Then
                                    .AddComment
                                    .Comment.Visible = False
                                    TextVal = .Value
                                    .Comment.Text Text:=TextVal
                                End If
                                .Value = MyData(I)
                            End With
                        Next I
                    End If
                End With
            End With
        Next MyVal
    End With
End Sub

posting here too:
http://www.excelforum.com/excel-programming/766437-copying-data-without-overwriting-edit-macro.html
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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