下面的代碼使用一個範圍從工作表中刪除行,我的worbook沒有標準化,所以我的問題是如何在這裏使用InStr函數也刪除類似的值,即如果「蘋果有限公司」在範圍內,那麼它會刪除包含「蘋果」的行或反之亦然......是否有可能?使用InStr函數來刪除範圍內的相似值
更新:第28行:需要object @freeman我無法獲得「Dim Cel as Range」的工作,我已經插入到這裏並在工作表的頂部插入了相同的結果。你寫的'Cel'的語法是?下面
Sub Loop_Example()
Dim ChkRange As Range
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
Set ChkRange = Sheets("Sheet2").Range("A1:A13")
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With Sheet1
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = 2
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "A")
If Not IsError(.Value) Then
If Not IsError(Application.Match(.Value, ChkRange, 0)) Then
.EntireRow.Delete
End If
For Each Cell In ChkRange
If InStr(1, Cell.Value, .Value) > 0 And Cell.Value <> .Value Then
'include the 2nd part of the "AND" to ensure you don't delete based
'on a COMPLETE match, only on a partial match as noted in the comments
Cell.EntireRow.Delete
End If
Next
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub
Sub Loop_Example()
Dim ChkRange As Range
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
Set ChkRange = Sheets("Sheet2").Range("A1:A13")
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With Sheet1
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = 2
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "A")
If Not IsError(.Value) Then
If Not IsError(Application.Match(.Value, ChkRange, 0)) Then
.EntireRow.Delete
End If
For Each Cell In ChkRange
Dim Cel As Range
If InStr(1, Cell.Value, .Value) > 0 And Cell.Value <> .Value Then
'include the 2nd part of the "AND" to ensure you don't delete based
'on a COMPLETE match, only on a partial match as noted in the comments
Cell.EntireRow.Delete
End If
Next
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub
菠蘿是否算作像蘋果公司? – pnuts 2015-04-02 14:44:08
如果整個值存在於另一個字符串中,我不希望宏將其刪除,因爲這會刪除批量值 – 2015-04-02 14:55:34
因此,無論是在開始處還是在後面加上一個空格(或標點符號?),或者在空格前面和末尾(或只有標點符號)?不過呢,說'The'和'The Ltd.'。 – pnuts 2015-04-02 15:06:36