将唯一值从一个Excel工作表复制到另一个工作表

时间:2018-08-15 19:50:51

标签: excel vba excel-vba

我正在一个工作簿中工作,其中有100张纸。我在“摘要”选项卡中汇总数据。原始数据表上的每一行数据都包含一列带有标识符编号的列,我将其复制并填充到摘要表中。我只需要每个标识符在摘要表上显示一次即可。我的代码正在为此工作,但是由于某种原因,它会复制第一个唯一值并将其显示两次。摘要选项卡的每个原始数据表都有一列,因此我使用crange在摘要表上找到正确的列。每列在前两行中都有静态数据,因此我希望新数据在第3行中粘贴startgin。对于如何删除重复的第一个值的任何帮助,将不胜感激。

我尝试添加删除重复项功能,但没有用。

Sub empID()
On Error Resume Next
'variables to indentify the column in sheet 2 to look at
Dim c As Integer
Dim cstring As String
'varialbe to identify the last row on the referenced sheet
Dim sh2lastr As Integer
'variable for "sheet2"
Dim sh1 As Worksheet
Set sh1 = ThisWorkbook.Worksheets("Sheet2")
'variable to store the referenced sheet name
Dim sh2 As Worksheet
'variable to store the empid range on the sh2
Dim empID As Range

'start with column c
Dim cname As String
cname = ActiveWorkbook.ActiveSheet.Name
Dim crange As Range
Set crange = sh1.Range("C1:ZZ1").Find(cname, LookIn:=xlValues, lookat:=xlPart)

c = crange.column
'MsgBox "c=" & c
Dim varr
varr = Split(sh1.Cells(1, c).Address(True, False), "$")
cstring = varr(0)
'MsgBox "cstring=" & cstring
Set sh2 = ActiveWorkbook.ActiveSheet
sh2lastr = sh2.Cells(sh2.Rows.Count, 2).End(xlUp).Row
'MsgBox "sh2lastr = " & sh2lastr

sh2.Range(sh2.Cells(8, 2), sh2.Cells(sh2lastr, 2)).AdvancedFilter _
    Action:=xlFilterCopy, _
    copytorange:=sh1.Range(cstring & "3"), _
    unique:=True
'sh1.Range(cstring & "3" & char58 & cstring & sh2lastr).RemoveDuplicates Columns:=1, Header:=xlNo


End Sub

0 个答案:

没有答案