我有大量的數據與一些混合了數字,句號和下劃線的單元格。但是,我想創建一個宏,它將刪除包含數字等的單元格,以便只留下包含字母表中的字母的單元格。以下是我現有的代碼,但無法正常工作。我如何解決它?如何根據一定數量的值刪除單元格?
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