gpt4 book ai didi

excel - 从另一个工作表中删除文本等于 #N/A 的行

转载 作者:行者123 更新时间:2023-12-04 21:29:34 25 4
gpt4 key购买 nike

我已经构建了一系列 VBA 宏来为用户完成一些步骤。我已经构建了一个按钮,供用户单击以删除包含#N/A 的任何行。我之前的步骤之一使这只是一个纯文本值。

执行该操作时,它似乎运行,但只是将我的 View 移动到我试图清理的工作表中,实际上并没有删除任何行。

我已经尝试切换部分代码,而不是 = 喜欢

Sub DeleteNAsOnFrank_Click()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

With Sheets("Frank Import Full List")
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "A")
If Not IsError(.Value) Then
If .Value = "#N/A" Then .EntireRow.Delete
End If
End With
Next Lrow
End With

ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub

我希望它删除任何包含文本 If .Value = "#N/A" Then .EntireRow.Delete 的行在 Sheets("Frank Import Full List")

最佳答案

将您的代码更改为

    With .Cells(Lrow, "A")
If IsError(.Value) Then .EntireRow.Delete
End With

但是,我会推荐一种更好的方法来删除这些单元格。使用自动过滤器而不是循环和删除。 你的代码会更快 .也代替 usedrange。使用实际范围。使用 This找到最后一行并使用该范围
Dim ws As Worksheet
Dim delRange As Range
Dim lRow As Long

Set ws = ThisWorkbook.Sheets("Frank Import Full List")

With ws
.AutoFilterMode = False
lRow = .Range("A" & .Rows.Count).End(xlUp).Row

With .Range("A1:A" & lRow)
.AutoFilter Field:=1, Criteria1:="#N/A"
Set delRange = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With

If Not delRange Is Nothing Then delRange.Delete

.AutoFilterMode = False
End With

在行动

enter image description here

关于excel - 从另一个工作表中删除文本等于 #N/A 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57709524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com