如果第一个单元格不是数字 vba Excel,则删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21886113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete row if first cell isn't numeric vba Excel
提问by agustin
I am using following code to delete a row if the first cell is not a number (text or blank cell)
如果第一个单元格不是数字(文本或空白单元格),我将使用以下代码删除一行
Dim LR3 As Long, i3 As Long
With Sheets("Productos")
LR3 = Range("A" & Rows.Count).End(xlUp).Row
For i3 = LR3 To 2 Step -1
If IsNumeric(Sheets("Productos").Range("A" & i3).Value) Then
Else
Rows(i3).Delete
End If
Next i3
End With
LR3 to 2 is used because the first row is a header row, and I don't want it deleted. I don't see anything wrong with the code, and I even get no error. Do you see something wrong? It′s maybe a false procedure?
使用 LR3 到 2 是因为第一行是标题行,我不想删除它。我看不出代码有什么问题,我什至没有发现任何错误。你看有什么不对吗?这可能是一个错误的程序?
回答by Siddharth Rout
The problem with your code which I suspect is that Sheets("Productos")
is not the activesheet. So Rows(i3).Delete
is referring to the activesheet which might not be Sheets("Productos")
我怀疑您的代码的问题Sheets("Productos")
不是活动表。所以Rows(i3).Delete
指的是可能不是的活动表Sheets("Productos")
Notice the use of DOTS in the code below.
请注意以下代码中 DOTS 的使用。
Try this (TRIED AND TESTED)
试试这个(尝试和测试)
Sub Sample()
Dim LR3 As Long, i3 As Long
With Sheets("Productos")
LR3 = .Range("A" & .Rows.Count).End(xlUp).Row
For i3 = LR3 To 2 Step -1
If Not IsNumeric(.Range("A" & i3).Value) Then .Rows(i3).Delete
Next i3
End With
End Sub
EDIT: I missed the Blank Cell part but thanks to Jimmy's post, I saw that.
编辑:我错过了空白单元格部分,但感谢吉米的帖子,我看到了。
Amended Code
修改后的代码
Sub Sample()
Dim LR3 As Long, i3 As Long
With Sheets("Productos")
LR3 = .Range("A" & .Rows.Count).End(xlUp).Row
For i3 = LR3 To 2 Step -1
If Not IsNumeric(.Range("A" & i3).Value) Or _
.Range("A" & i3).Value = "" Then .Rows(i3).Delete
Next i3
End With
End Sub
回答by Jimmy Smith
If the cell is blank, IsNumeric will return true. You could try using len,
如果单元格为空,则 IsNumeric 将返回 true。你可以尝试使用 len,
Dim LR3 As Long, i3 As Long
With Sheets("Productos")
LR3 = .Range("A" & .Rows.Count).End(xlUp).Row
For i3 = LR3 To 2 Step -1
If IsNumeric(.Range("A" & i3).Value) And _
len(.Range("A" & i3).Value) > 0 Then
Else
.Rows(i3).Delete
End If
Next i3
End With