删除C单元格中不包含7或8个数字的每一行

我一直在四处张望。 但是我不知道google是什么字眼。

我想删除C列中的单元格不包含7或8个数字的每一行。 问题是我不知道如何编码。

VBA代码中的1个字母,1个数字,1个或多个字母,1个或多个数字,空格等符号是什么? 我一直在Google上search几个小时,但我想我只是不知道正确的search词。 我在哪里或如何find这个? 我知道这很愚蠢

非常感谢。

编辑:

@eirikdaude谢谢你的回答。

不知何故,这是行不通的。 这是我的:

Dim lastRow As Long lastRow = Cells(Rows.Count, 3).End(xlUp).Row Dim i As Integer For i = 2 To lastRow If (IsNumeric(Cells(i, 3).Value) And Len(Cells(i, 3).Value) >= 7 And Len(Cells(i, 3).Value) <= 8) Then ' do nothing Else Rows(i).Select Selection.Delete Shift:=xlUp End If Next i 

我一直在尝试一切,但我不明白为什么上面的代码不起作用。

所有我的单元格在Excel中格式化为“标准”是否重要? 因为所有数据都是从txt文件导入的。

有两个可能的原因,你代码是不删除预期的行:

  1. 预期的行被删除可能会跳过代码,因为它从上到下删除行。 删除多行时,正确的方法是向上(即从下到上)

  2. 由于您的数据是从文本文件导入的,因此C列中的值可能在末尾有一些额外的空格。 TRIM的使用会照顾到这种情况。

下面的代码包括两个更正:

 Sub Rng_Delete_Rows() Dim LRowLst As Long, LRow As Long Dim vCllVal As Variant 'Change SheetName as required With ThisWorkbook.Sheets(1) 'Use this if procedure resides in Data workbook 'With ThisWorkbook.Sheets(1) 'Use this if procedure does not reside in Data workbook Application.Goto .Cells(1), 1 LRowLst = .Cells(.Rows.Count, 3).End(xlUp).Row For LRow = LRowLst To 2 Step -1 Rem Get Cell Value At Once vCllVal = Trim(.Cells(LRow, 3).Value2) If Not ((IsNumeric(vCllVal) _ And Len(vCllVal) >= 7 And Len(vCllVal) <= 8)) Then Rem Delete Row .Rows(LRow).EntireRow.Delete End If: Next: End With End Sub 

除非你坚持使用这个正则expression式,我只是检查单元格中的值的长度,如果它是IsNumeric

在你的情况下,这样的事情:

 For Each c In rangeToCheck If IsNumeric(c) And Len(c) >= 7 And Len(c) <=8) Then do your stuff End If Next c