Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy cell to another column

Status
Not open for further replies.

amecari

Technical User
Mar 21, 2001
29
BR
Hi,

I neede to do something like this : I have two columns A and B:
A B
12 12
13 13
14 15
21 16
21
I need to compare range column "A" with range column "B". If I don't find A(n) into B(n) I'd copy this value into columm "C".

I wrote this , but I ins't work.
Thanks for help

Sub compare()
Dim myrng As Range
Dim myrng1 As Range
Dim cel As Range
Dim cel1 As Range
Set myrng = Plan1.Range("a1:a10")
Set myrng1 = Plan1.Range("b1:b10")

For Each cel In myrng
For Each cel1 In myrng1

If cel.Value <> cel1.Value Then
cel.Selection
Selection.Copy
Columns(&quot;c&quot;).PasteSpecial

End If

Next
Next
End Sub
 
IF COL B IS SORTED DESCENDING....


THEN COL C: FORMULA


=IF(ISERROR(VLOOKUP(A1,$B$1:$B$4,1,FALSE)),A1,&quot;&quot;)
 
IF COL B IS SORTED ASCENDING....


THEN COL C: FORMULA


=IF(ISERROR(VLOOKUP(A1,$B$1:$B$4,1,FALSE)),A1,&quot;&quot;)
 
Well I see several things I think is wrong with that. Instead of doing that try this. Put this in worksheet / section change.

rowcount = 10
z = 1

For x = 1 To rowcount
For y = 1 To rowcount

If Range(&quot;a&quot; & x).Value = Range(&quot;b&quot; & y).Value Then

Exit For

Else

If y = rowcount Then

Range(&quot;C&quot; & z).Value = Range(&quot;a&quot; & x).Value
z = z + 1

End If

End If

Next y

Next x

Change the rowcount number if you want to do more then 10. This will write down any number in column A that isn't found anywhere in column B. This will right down from C1 and so on. If you want number not found from A2 and want it to write in A2 remove Z and replace it with A. That should help
 
After thinking about it further. (During lunch) I may have done more then you wanted. Mine would check the entire column. If you just wanted to compare cell by cell on the way down. Try using this.

rowcount = 10

Range(&quot;C1:C&quot; & rowcount).Clear

For x = 1 To rowcount

If Range(&quot;a&quot; & x).Value <> Range(&quot;b&quot; & x).Value Then

Range(&quot;C&quot; & x).Value = Range(&quot;a&quot; & x).Value

End If

Next x


Also another question is, What is running your Compare sub. Do you have something more complex under that. You have some comands set up incorrectly. Your set Range command doesn't seem to work properly. Unless your using a global variable somewhere. I think This would do what your code was meant to do. Sorry, Looking at your code I make out two diffrent things you was trying to do. If you could explain better what you have and what you what type of application your using it for. If this way is the way you wanted great. If its the other way Great also. Also if its my first post please add this line to clear it out incase you changed a value.

Range(&quot;C1:C&quot; & rowcount).Clear

Hope I helped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top