VBA宏删除包含#N / A的单元格并向上移动单元格(而不是行)

时间:2018-07-22 20:40:45

标签: excel vba

我正在创建排名,我需要删除所有带有#N / A的单元格(粘贴为文本,而不是公式),并删除这些单元格并将其压缩。

工作表包含503个原始数据,我需要从A列到T列。 enter image description here

预先感谢,我已经尝试了该网站的许多VBA代码,但找不到任何可行的方法。

2 个答案:

答案 0 :(得分:2)

尝试

dim rng as range

with worksheets("sheet1")
    on error resume next
    set rng = .range("A:T").specialcells(xlcelltypeformulas, xlerrors)
    if not rng is nothing then
        rng.delete shift:=xlup
    end if
    set rng = .range("A:T").specialcells(xlcelltypeconstants, xlerrors)
    if not rng is nothing then
        rng.delete shift:=xlup
    end if        
    on error goto 0
end with

答案 1 :(得分:1)

这应该有效。有多种方法可以解决您的问题,但是由于您没有那么多数据集,因此我只修改了一些可用的代码。

Sub KillPoundNa()
Dim rCell As Range, WS As Worksheet, KillRng As Range, UndesireableText As String

UndesireableText = "#N/A"
Set WS = ActiveSheet

Set KillRng = WS.Cells(Rows.Count, 1)

For Each rCell In WS.UsedRange.Cells

    If InStr(1, rCell.Text, UndesireableText, vbTextCompare) > 0 Then
        Set KillRng = Union(KillRng, rCell)
    End If

Next rCell

KillRng.Delete (xlUp)

End Sub