2013-12-18 279 views
-1

我完全不明白如何在vba deleting rows that do not contain set values defined in range(我需要使用VBA)中遵循答案。从我收集的信息中,我需要指定一个数组,然后使用一些如果是的东西。如何删除所有不包含特定值的单元格(在VBA/Excel中)

在我的情况下,我想创建一些将只搜索指定列并删除所有不包含特定字母/数字的值。 1,2,3,4,5,s,f,p,a,b,c,o是我想保留的数字/字母。不包含这些值的单元格(甚至11或1应该被删除),我只想删除单元格(而不是整行),并将它下面的单元格移动(我相信你可以使用默认的.delete命令)。

例如我的列是这样的:

p
一个
小号
˚F
小号
f

我想筛选我的数据,以便所有空白单元格和不包含上述数字或字母的所有单元格(例如,在这种情况下为31和8)被自动删除。

感谢您的帮助!

+2

来吧,很简单。反向设计阵列。将所有要删除的值粘贴到数组中,然后将其应用为过滤器 - 如果单元格等于数组中的值,则将其删除 – 2013-12-18 22:02:54

+0

上述问题与您的问题不同。你能提供一些样本数据吗?你想保持的价值观是否存在于单元格中,还是它们是其他文本的一部分? –

+0

@mehow我非常喜欢VBA noob。我了解基础知识,其他许多方面都不太好,我尝试对其他代码进行反向工程,但没有成功。 – user3084100

回答

2

这将做

Sub Main() 

Dim dontDelete 
dontDelete = Array("1", "2", "3", "4", "5", "s", "f", "p", "a", "b", "c", "o") 

Dim i As Long, j As Long 

Dim isThere As Boolean 

For i = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1 
    For j = LBound(dontDelete) To UBound(dontDelete) 
     If StrComp(Range("A" & i), dontDelete(j), vbTextCompare) = 0 Then 
      isThere = True 
     End If 
    Next j 
    If Not isThere Then 
     Range("A" & i).Delete shift:=xlUp 
    End If 
    isThere = False 
Next i 

End Sub 
+1

另一个好的 –

+0

我最终使用了这个!用一个简单的副本粘贴,不知道它是如何工作的,虽然... – user3084100

+0

@ user3084100很高兴听到它在最后工作。考虑接受帮助你的答案:) – 2013-12-19 20:37:25

1
Sub DeleteValues() 
Dim x As Integer 
Dim i As Integer 
Dim Arr(1 To 3) As String 

Arr(1) = "1" 
Arr(2) = "2" 
Arr(3) = "3" 

Range("A1").Select 

For x = 1 To 10 
    For i = 1 To 3 
     If ActiveCell.Value = Arr(i) Then 
      ActiveCell.Delete 
     End If 
    Next i 
    ActiveCell.Offset(1, 0).Select 
Next x 

End Sub 

通过范围(“A1:A10”)这将循环和删除任何细胞,其中值=任何数组值(1,2,3)

的你应该希望能够工作有了这些代码,并适合您的需求?

+0

-1对于较差的循环登录和移动索引 – 2013-12-18 22:49:34

3
Sub Tester() 

Dim sKeep As String, x As Long 
Dim rngSearch As Range, c As Range 

    'C1:C5 has values to keep 
    sKeep = Chr(0) & Join(Application.Transpose(Range("C1:C5").Value), _ 
           Chr(0)) & Chr(0) 

    Set rngSearch = Range("A1:A100") 

    For x = rngSearch.Cells.Count To 1 Step -1 
     Set c = rngSearch.Cells(x) 
     If InStr(sKeep, Chr(0) & c.Value & Chr(0)) = 0 Then 
      c.Delete shift:=xlShiftUp 
     End If 
    Next x 

End Sub 
+1

+ 1不错:) –

+0

+1酷编码风格 – 2013-12-19 07:36:54

1

另一种方式:)这不会删除细胞循环。

Option Explicit 

Sub Sample() 
    Dim ws As Worksheet 
    Dim rngDEL As Range 
    Dim strDel As String 
    Dim arrDel 
    Dim i As Long 

    strDel = "1,11,Blah" '<~~ etc... You can pick this from a range as well 
    arrDel = Split(strDel, ",") 

    Set ws = ThisWorkbook.Sheets("Sheet1") 

    With ws.Columns(1) '<~~ Change this to the relevant column 
     For i = LBound(arrDel) To UBound(arrDel) 
      .Replace What:=arrDel(i), Replacement:="", LookAt:=xlWhole, SearchOrder:= _ 
      xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False 
     Next i 

     On Error Resume Next 
     Set rngDEL = .Cells.SpecialCells(xlCellTypeBlanks) 
     On Error GoTo 0 

     If Not rngDEL Is Nothing Then rngDEL.Delete Shift:=xlShiftUp 
    End With 
End Sub 
相关问题