Copy to another column ?

shumba

Board Regular
Joined
Oct 5, 2010
Messages
168
Hi,

The procedure below is designed to copy a three letter code from column A to column B only if the code is not already in column B. This is not working.

Being a novice at VBA please will someone show me where I am going wrong.


Code:
[LEFT]Sub Copy_code()
Do Until IsEmpty(ActiveCell)
[LEFT] If Selection.Value = Range("b:b") Then Selection.Offset(1, 0).Select Else Selection.Copy   _    Range("b:b").Cells(Rows.Count).End(xlUp).Offset(1, 0)
[/LEFT]
Loop
[LEFT]End Sub[/LEFT]
[/LEFT]


Thanks.​

Rob.​
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi shumba,

Below is a bit modified version of your code, but if the value in column A is not found in column B, it copies in B,
but one row below. I didn't modifiy this because I'm not sure if you need in that way.

Code:
Sub Copy_code()

Range("A1").Select

Do Until IsEmpty(ActiveCell)

    code = Application.WorksheetFunction.CountIf(Range("B:B"), Selection.Value)

    If code > 0 Then [COLOR=Green]'This part counts if the current value of A, is present in B[/COLOR]
        Selection.Offset(1, 0).Select
    Else
        Selection.Copy Range("b:b").Cells(Rows.Count).End(xlUp).Offset(1, 0)
    End If
Loop
End Sub

Hope this helps,

Best regards
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,681
Members
452,937
Latest member
Bhg1984

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