【问题标题】:How to delete all cells that do not contain specific values (in VBA/Excel)如何删除所有不包含特定值的单元格(在 VBA/Excel 中)
【发布时间】:2015-03-06 20:44:49
【问题描述】:

我完全不明白如何遵循vba deleting rows that do not contain set values defined in range 中的答案(为此我需要使用 VBA)。根据我收集的信息,我需要指定一个数组,然后使用一些 if then 的东西。

就我而言,我想创建一个只搜索指定列并删除所有不包含特定字母/数字的值的东西。 1,2,3,4,5,s,f,p,a,b,c,o 是我要保留的数字/字母。不包含这些值的单元格(甚至应该删除 11 或 1),我只想删除单元格(而不是整行)并将其下方的单元格向上移动(我相信您可以使用默认的 .delete 命令执行此操作)。

例如,我的列如下所示:

p
一个
1
2
5
s
f
s
8

31
4
f

我想筛选我的数据,以便自动删除所有空白单元格和所有不包含上述数字或字母的单元格(例如本例中的 31 和 8)。

感谢您的帮助!

【问题讨论】:

  • 来吧,非常简单。对阵列进行逆向工程。将要删除的所有值粘贴到数组中,然后将其用作过滤器 - 因此,如果单元格等于数组中的值,则将其删除
  • 您的上述问题与您原来的问题不同。你能提供一些样本数据吗?您要保留的值是否单独存在于单元格中,或者它们是其他文本的一部分?
  • @mehow 我是一个 VBA 菜鸟。我了解基础知识,但不太擅长其他方面,我尝试对我找到的其他代码进行逆向工程,但没有成功。
  • 还有@mehow,我想删除不属于数组的单元格-如果不清楚,请见谅。
  • @user3084100 确切地说,这意味着您需要一个包含所有要删除的字符串的备用数组!对最初的想法进行逆向工程

标签: excel vba


【解决方案1】:
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

【讨论】:

    【解决方案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
    

    【讨论】:

    • 我最后用了这个!使用简单的复制粘贴,但不知道它是如何工作的......
    • @user3084100 很高兴听到它最终奏效了。考虑接受对您有帮助的答案:)
    【解决方案3】:
    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
    

    这将遍历 range("a1:a10") 并删除 value = 任何数组值 (1,2,3) 的任何单元格

    您应该希望能够使用此代码并满足您的需求吗?

    【讨论】:

      【解决方案4】:

      另一种方式 :) 不会删除循环中的单元格。

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-20
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 2013-07-31
        • 1970-01-01
        • 2019-03-19
        • 1970-01-01
        相关资源
        最近更新 更多