我有大量的数据与一些混合了数字,句号和下划线的单元格。但是,我想创建一个宏,它将删除包含数字等的单元格,以便只留下包含字母表中的字母的单元格。以下是我现有的代码,但无法正常工作。我如何解决它?如何根据一定数量的值删除单元格?
Sub Sample()
Dim ws As Worksheet
Dim strSearch As String
Dim Lrow As Long
strSearch = "."
strSearch = "0"
strSearch = "1"
strSearch = "2"
strSearch = "3"
strSearch = "4"
strSearch = "5"
strSearch = "6"
strSearch = "7"
strSearch = "8"
strSearch = "9"
strSearch = "."
Set ws = Sheets("Sheet1")
With ws
Lrow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Remove any filters
.AutoFilterMode = False
'~~> Filter, offset(to exclude headers) and delete visible rows
With .Range("A1:A" & Lrow)
.AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
'~~> Remove any filters
.AutoFilterMode = False
End With
End Sub
我也有这个代码不能正常工作。我应该使用哪一种,我该如何解决这些问题?另外,我应该使用哪一个?
Sub Test()
Dim cell As Range
For Each cell In Selection
If InStr(1, cell, "1", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, "2", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, "3", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, "4", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, "5", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, "6", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, "7", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, "8", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, "9", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, "0", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
For Each cell In Selection
If InStr(1, cell, ".", vbTextCompare) > 0 Then
cell.EntireRow.Delete
End If
Next
End Sub
您将需要***数组*** ***'strSearch' –
这里有一个很好的问题,只包括搜索字母字符(http://stackoverflow.com/questions/29633517/how-can-i-check - 如果-A-字符串只,包含-字母)。如果你愿意重构,这几乎肯定会简化你的代码。 – Joe
加里 - 对不起,我对此非常有经验,但我确实听到阵列被提及很多。我将在何处以及如何整合它? – Imperdiet