View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bernie Deitrick Bernie Deitrick is offline
external usenet poster
 
Posts: 5,441
Default Find and insert?

Sub FindAndInsert()
Dim cell As Range

For Each cell In Sheets("Sheet2").Range("A3:A14")
If IsError(Application.Match(cell.Value, _
Worksheets("Sheet1").Range("A:A"), False)) Then
cell.Copy Worksheets("Sheet1").Range("A65536").End(xlUp)(2)
End If
Next cell
End Sub

HTH,
Bernie
MS Excel MVP


"leonidas" wrote in message
...

Hi,

I have a sheet1 with data in column B. I have a sheet2 with also data
in column B. I have some code below to check if the data from sheet2
also exists on sheet1, and if not, then copy it to sheet1.
The only problem is it works 1 time and then I get an error. I am a
novice in VBE so that's why the code will look unlogical.
Can someone help me to create a better code that will work? Thanks in
advance!


Code:
--------------------
Sub FindAndInsert()

For Each cell In Range("A3:A14")
myvalue = cell.Value
On Error GoTo NextPart
myvalue1 = Sheets("Sheet1").Columns("A:A").Find(What:=myvalue )
GoTo Finalize
NextPart:
cell.Offset(-1, 0).Select
myvalue2 = Selection.Value
Sheets("Sheet1").Select
Columns("A:A").Find(What:=myvalue2).Select
Selection.Offset(1, 0).EntireRow.Insert
Selection.Offset(1, 0).Select
Selection.Value = myvalue
Sheets("Sheet2").Select
Finalize:
Next cell

End Sub
--------------------


--
leonidas
------------------------------------------------------------------------
leonidas's Profile: http://www.excelforum.com/member.php...o&userid=35375
View this thread: http://www.excelforum.com/showthread...hreadid=567838