以下编码只是搜索字符串并删除记录,但是当我们搜索号码时,它不会删除记录。请告知出了什么问题。
以下编码不适用于数字(整数),仅适用于字符串。
Dim myFileNameDir As String
Dim ws1 As Worksheet
Dim iRow1 As Long
Dim str As String
myFileNameDir = "C:\Users\GShaikh\Desktop\Book16.xlsx"
Workbooks.Open Filename:=myFileNameDir, UpdateLinks:=0
Set ws1 = Worksheets("Students")
str = ListView1.SelectedItem.SubItems(1)
MsgBox str
With ws1
.AutoFilterMode = False
iRow1 = .Range("B" & .Rows.Count).End(xlUp).row
With .Range("B1:D" & iRow1)
.AutoFilter Field:=1, Criteria1:="=*" & str & "*"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
.AutoFilterMode = False
End With
ActiveWorkbook.Save
ActiveWorkbook.Close
答案 0 :(得分:-1)
您可以尝试如下:
Dim rowCount, row As Integer
Dim myFileNameDir, str As String
myFileNameDir = "C:\Users\GShaikh\Desktop\Book16.xlsx"
Workbooks.Open Filename:=myFileNameDir, UpdateLinks:=0
str = ListView1.SelectedItem.SubItems(1)
'Set start row for checking
row = 1
With Sheets("Students")
'Getting total row count
rowCount = .Range("B" & .Rows.Count).End(xlUp).row
'Loop until row less than or equal with row count
Do While row <= rowCount
'If string is found in B or C or D, delete row and decrease rowCount
If .Range("B" & row) = str Or .Range("C" & row) = str Or .Range("D" & row) = str Then
.Rows(row).EntireRow.Delete
rowCount = rowCount - 1
'Else move to next row
Else
row = row + 1
End If
Loop
End With