如果最后两个字符等于“VBA”;";然后删除整行
我试图运行一个宏,它查看字符串并确定最后两个字符是否等于“XX”,然后删除整行 我得到一个对象错误哦,下面突出显示如果最后两个字符等于“VBA”;";然后删除整行,vba,excel,excel-2007,Vba,Excel,Excel 2007,我试图运行一个宏,它查看字符串并确定最后两个字符是否等于“XX”,然后删除整行 我得到一个对象错误哦,下面突出显示 Sub Oval2_Click() Last = Cells(Rows.Count, "E").End(xlUp).Row For i = Last To 1 Step -1 `If (Right(Cells(i, "E"), 2).Value) = "TZ" Then` Cells(i, "E").EntireRow.Delete End If Next i
Sub Oval2_Click()
Last = Cells(Rows.Count, "E").End(xlUp).Row
For i = Last To 1 Step -1
`If (Right(Cells(i, "E"), 2).Value) = "TZ" Then`
Cells(i, "E").EntireRow.Delete
End If
Next i
End Sub
请帮忙!谢谢。这个
If (Right(Cells(i, "E"), 2).Value) = "TZ" Then
应该是
If Right(Cells(i, "E").Value, 2) = "TZ" Then
这也应该起作用:
If Right(Cells(i, "E"), 2) = "TZ" Then
从
右侧(单元格(即,“E”),2)中删除.Value
。Value
。它抱怨是因为.Value
在函数上不存在对
虽然您已经有了答案,但我不建议通过循环来实现这一点。如果有大量行,循环将很慢。这里有一个比较快的替代方案。我还对代码进行了注释,这样您在理解它时就不会有问题了
逻辑:
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the respective sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Remove any filters
.AutoFilterMode = False
'~~> Get last row of Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Use autofilter to identify the cells which end in "XX"
With .Range("A1:A" & lRow)
.AutoFilter Field:=1, Criteria1:="=*XX"
'~~> Offset to exclude the header and delete them
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
'~~> Remove any filters
.AutoFilterMode = False
End With
End Sub
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the respective sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Remove any filters
.AutoFilterMode = False
'~~> Get last row of Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Use autofilter to identify the cells which end in "XX"
With .Range("A1:A" & lRow)
.AutoFilter Field:=1, Criteria1:="=*XX"
'~~> Offset to exclude the header and delete them
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
'~~> Remove any filters
.AutoFilterMode = False
End With
End Sub
屏幕截图:
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the respective sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Remove any filters
.AutoFilterMode = False
'~~> Get last row of Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Use autofilter to identify the cells which end in "XX"
With .Range("A1:A" & lRow)
.AutoFilter Field:=1, Criteria1:="=*XX"
'~~> Offset to exclude the header and delete them
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
'~~> Remove any filters
.AutoFilterMode = False
End With
End Sub
哎呀。。。我应该在输入相同的响应之前刷新我的页面。这段代码比其他答案写得好得多。虽然其他方法可能有效,但这是遵循VBA最佳实践的唯一答案。