code copy data of certain column from sheet to another doesn't work

leap out

Active Member
Joined
Dec 4, 2020
Messages
275
Office Version
  1. 2016
  2. 2010
hello guys
I have this code it supposes data of column b in sheet1 to column e in sheet2 without any duplicated , but it doesn't happen any thing
so I appreciate if any body provide advice how can fix it .
VBA Code:
Sub CopyUnique()
    Dim s1 As Worksheet, FirstEmptyRow As Long, expCol As Long
    Set s1 = ActiveSheet
    With Sheets("sheet1")
        .Range("b:b").Name = "code1"
        expCol = .Range("code1").Column
        FirstEmptyRow = .Cells(.Rows.Count, expCol).End(xlUp).Row + 1
        s1.Range("e2:e100").Copy .Cells(FirstEmptyRow, expCol)
        .Range("code1").RemoveDuplicates Columns:=1, Header:=xlNo
    End With
End Sub
thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Your code is doing the exact opposite of what you said it should. Which is correct?
 
Upvote 0
data of column b in sheet1 to column e in sheet2
You mean copy sheet 1 column B to sheet 2 column E?

Your code is doing it wrongly then.

Which is your activesheet? Sheet1 or Sheet2?
Your With block is Sheet1, range code1 is in Sheet1, FirstEmptyRow is of Sheet1.
s1's (Activesheet) column E is being copied to Sheet1.
You're removing duplicates in Sheet1.
 
Upvote 0
hi, Fluff
you mean this doesn't copy without duplicated data ?
what about this line
VBA Code:
 .Range("code1").RemoveDuplicates Columns:=1, Header:=xlNo
what works for?
 
Upvote 0
my activesheet is sheet2
Your code was so confusing you probably confused yourself too.

Try:
VBA Code:
Sub CopyUnique()
    Dim s2 As Worksheet, FirstEmptyRow As Long, expCol As Long
    Set s2 = ActiveSheet 'renamed var to "s2" for clarity's sake
    With Sheets("sheet1")
        .Range("b:b").Name = "code1"
        expCol = .Range("code1").Column
        FirstEmptyRow = s2.Cells(.Rows.Count, "E").End(xlUp).Row + 1  'take Sheet2's first empty row in column E, not Sheet1
        .Range("B2:B100").Copy s2.Cells(FirstEmptyRow, "E") 'copy from Sheet1 to Sheet2
        s2.Columns("E").RemoveDuplicates Columns:=1, Header:=xlNo 'remove duplicates in Sheet2
    End With
End Sub
 
Last edited:
Upvote 0
@ Fluff it supposes giving me error to find the error which line but the code does not work at all !
 
Upvote 0
Are you trying to copy col E to Col B or the other way round?
 
Upvote 0

Forum statistics

Threads
1,216,086
Messages
6,128,734
Members
449,466
Latest member
Peter Juhnke

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