简体   繁体   English

我可以获取一系列单元格,删除重复项和空白单元格,然后复制到VBA Excel中已存在的表格中吗?

[英]Can I take a range of cells, remove duplicates and blank cells, then copy into an already existing table in VBA Excel?

I am very new to VBA so forgive me if I'm doing this completely wrong. 我对VBA很新,请原谅我,如果我这样做完全错了。 Basically, to give an overview of what I'm doing, I have a template that is filled out by a user. 基本上,为了概述我正在做的事情,我有一个由用户填写的模板。 There are values spread out over the template that I need to add to a table but I don't want to add blanks or duplicates. 模板上有值,我需要添加到表中,但我不想添加空格或重复项。 I started by condensing them in a continuous range and I was able to figure out how to remove the duplicates, but I can't remove the one blank that is left. 我开始通过连续范围冷凝它们,我能够找出如何删除重复项,但我无法删除剩下的一个空白。 Also, I'm not sure the best way to add them to the table on another worksheet. 另外,我不确定将它们添加到另一个工作表上的表的最佳方法。 This is what I have so far... 这就是我到目前为止......

Sub UpdateKey()

 Range("P10:P36").Select

 Selection.Copy
 Selection.PasteSpecial Paste:=xlPasteValues

 Selection.RemoveDuplicates Columns:=1, Header:=xlNo

End Sub

Like I said, this always leaves me a blank right in the middle which I don't know how to get rid of, and I don't know where to go from here to add what's left to my table. 就像我说的,这总是让我在中间空白,我不知道如何摆脱,我不知道从这里去哪里添加剩下的东西到我的桌子。 Thanks for any help! 谢谢你的帮助!

Try to use following code: 尝试使用以下代码:

Sub UpdateKey()
    With Range("P10:P36")
        .Value = .Value
        .RemoveDuplicates Columns:=1, Header:=xlNo
        On Error Resume Next
        .SpecialCells(xlCellTypeBlanks).Delete xlShiftUp
        On Error GoTo 0
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM