尝试删除所有行,直到单元格(A,1)具有一定的值

在VBA中有问题

尝试删除所有行,直到第1行中的值=“** GRAD *”

我得到运行时错误438

代码如下

Public Sub Delete() Dim i As Long i = 1 'Start from row 1 Application.ScreenUpdating = False With ThisWorkbook.Worksheets("Sheet1") Do Until .Range("A" & i).Value = "**GRAD" If .Rage("A" & i).Value <> "**GRAD" Then .Rows(i).EntireRow.Delete Else: i = i + 1 'Only increment if the row hasn't been deleted to prevent skipping rows End If Loop End With Application.ScreenUpdating = True End Sub 

一些帮助将不胜感激,新的VBA。

L.荷兰人已经给你回答你的问题

这是另一种更快的方法

删除所有行,直到 1中的值=“** GRAD *”

 Option Explicit Public Sub Delete() Dim lastRowToDelete As Long Dim f As Range With ThisWorkbook.Worksheets("Sheet0001") '<-- reference your worksheet With Range("A1", .Cells(.Rows.Count, 1).End(xlUp)) '<-- reference its columns "A" cells from row 1 sown to last not empty one Set f = .Find(what:="**GRAD", LookIn:=xlValues, lookat:=xlWhole, after:=.Range("A" & .Rows.Count)) '<-- look for the first cell whose value is "**GRAD" If f Is Nothing Then '<-- if not found then... lastRowToDelete = .Rows(.Rows.Count).Row '<-- the last row to delete is the last row of the range Else '<-- otherwise... lastRowToDelete = f.Row - 1 '<-- the last row to delete is the one preceeding the one with the found cell End If End With If lastRowToDelete > 0 Then .Range("A1:A" & lastRowToDelete).EntireRow.Delete 'delete all rows in a single shot End With End Sub 

错字? 我阅读If .Rage("A" & i).Value <> "**GRAD" Then而应该是If .Range("A" & i).Value <> "**GRAD" Then