从每个循环的选择中删除特定行

安德烈

我正在尝试删除不包含新单词的行。

我做的事情:

  1. 手动选择多行
  2. 运行宏,它检查每一行并将其中的新单词添加到字典中。如果没有新词 - 应删除该行。

问题:当宏删除一行时,它应该使用“下一个单元格”转到下一行,但它会跳过一个。

我需要你的帮助,因为我不知道如何让它在 VBA 中工作(这里是新手)。如何防止跳过并处理选择中的每一行?

演示数据:

A B 
A B C
C B 
A C
A B F

我的结果:

A B 
A B C
A C
A B F

应该:

A B 
A B C
A B F

代码:

Sub clean_keys()

' unique words
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")

For Each cell In Selection

    Dim strArray() As String
    Dim intCount As Integer

    strArray = Split(cell.Value, " ")

    Dim intWords As Integer 
    intWords = 0

    For intCount = LBound(strArray) To UBound(strArray)

        If dict.Exists(Trim(strArray(intCount))) Then
            dict(Trim(strArray(intCount))) = dict(Trim(strArray(intCount))) + 1
        Else
            dict.Add Key:=Trim(strArray(intCount)), Item:=1
            intWords = intWords + 1
        End If

    Next

    If intWords = 0 Then
        cell.EntireRow.Delete
    End If

Next cell
End Sub
用户4039065

删除行时始终从底部到顶部运行,否则可能会跳过行(如您所见)。

'don't dim inside a loop
Dim r As Long
Dim strArray As Variant
Dim intCount As Integer
Dim intWords As Integer

With Selection
    For r = .Rows.Count To 1 Step -1

        strArray = Split(Selection.Cells(r).Value & " ", " ")
        intWords = 0

        For intCount = LBound(strArray) To UBound(strArray) - 1
            If dict.Exists(Trim(strArray(intCount))) Then
                dict(Trim(strArray(intCount))) = dict(Trim(strArray(intCount))) + 1
            Else
                dict.Add Key:=Trim(strArray(intCount)), Item:=1
                intWords = intWords + 1
            End If
        Next intCount

        If intWords = 0 Then
            .Cells(r).EntireRow.Delete
        End If

    Next r
End With

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法为每个循环从数据表中删除行

来自分类Dev

无法为每个循环从数据表中删除行

来自分类Dev

从循环中的数据帧中删除特定行

来自分类Dev

使用for循环删除R中特定实例的行

来自分类Dev

每个循环的行已删除的中断

来自分类Dev

为什么必须为每个循环多次运行我的表才能删除表中的行?

来自分类Dev

在一个特定列或Fata框架中为每个唯一值选择随机行

来自分类Dev

在iMacros中循环特定行

来自分类Dev

Bash,sed,删除每个循环的最后一行

来自分类Dev

从循环Vue.js中的每个选择中获取数据

来自分类Dev

在Excel中删除特定行

来自分类Dev

从文件中删除特定行

来自分类Dev

删除特定行中的标签

来自分类Dev

从行中删除特定的模式

来自分类Dev

从文件中删除特定行

来自分类Dev

从 BindingSource 中删除特定行

来自分类Dev

从文件中删除特定行

来自分类Dev

删除 R 中的特定行

来自分类Dev

循环删除Matlab中的特定文件

来自分类Dev

使用for循环从ArrayList中删除特定对象

来自分类Dev

在特定值后删除熊猫数据框中的行(是否循环?)

来自分类Dev

Xpath:从文档节点变量中为每个循环选择节点

来自分类Dev

Postgres-为每个ID选择特定的行

来自分类Dev

从每个分组的 Spark 数据框中选择特定行

来自分类Dev

为每个循环遍历列表时删除列表中的项目

来自分类Dev

在for循环中删除np数组中的行

来自分类Dev

遍历.txt中的行并循环每个“ 15行”

来自分类Dev

如何删除Postgres中特定架构中的每个表?

来自分类Dev

选择/突出显示每一行中每个特定单元格值(“ Apple”)的列范围(AL)