【问题标题】:How do I find words starting with a specific letter and delete that row?如何查找以特定字母开头的单词并删除该行?
【发布时间】:2016-07-14 16:17:01
【问题描述】:

我应该使用什么宏来定位所有以 D 开头的行并删除这些行。 现在我正在使用:(删除 TOTAL 以上的所有文本),这并不总是完美的。

Sub A2a_Deleterowsabove()
Dim foundOne As Range
On Error Resume Next
With ActiveSheet
    Set foundOne = .Range("A:A").Find(what:="TOTAL", After:=.Range("a1"), LookIn:=xlFormulas, _
                                LookAt:=xlPart, SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, MatchCase:=False)
    If foundOne.Row > 1 Then
        Range(.Range("e1"), foundOne.Offset(-1, 0)).EntireRow.delete shift:=xlUp
    Else

End If
End With
End Sub

我正在考虑使用这个:

Sub Delete_Cells_with_D()
Dim i As Integer
For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
If Cells(i, 1) = "D* -" Then Cells(i, 1).EntireRow.Delete shift:=xlUp
Next i
End Sub

我应该在宏 #2 的突出显示部分中输入什么来表明 D 后面的数字是未知的?

感谢您的帮助!

【问题讨论】:

  • If Cells(i,1) Like "D*" Then ...

标签: vba excel


【解决方案1】:
Sub Delete_Cells_with_D()

    Dim i As Integer
    For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
        If Left(Cells(i, 1), 1) = "D" Then Cells(i, 1).EntireRow.Delete shift:=xlUp
    Next i

End Sub

【讨论】:

  • 太棒了!而已!谢谢,托马斯。只是想知道您是否可以向我解释一下我的宏做错了什么?我注意到你把它改成了 你能解释一下这是什么意思吗?最后 1 是变量吗?谢谢!
  • Left 函数返回字符串的最左边部分。第二个参数告诉它要返回多少个字符。
  • 哦,我想我现在明白了。谢谢你向我解释,托马斯。所以 (Cells(i,1),) 表示在单元格 (i) 内是整数 D 和 1),1) 是整数后有多少个字符空格?对 1),1) 部分感到困惑
  • Cells(I,1) 只是字符串值。这是另一个示例 Left("Hello World", 5 ) 将返回 "Hello"
  • 它应该是 5,因为世界上有 5 个字母?
猜你喜欢
  • 2015-07-14
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
  • 2019-07-17
  • 2021-12-29
  • 1970-01-01
相关资源
最近更新 更多