如果在多个列中找不到部分字符串,则删除行:VBA
Delete rows if partial string not found in variety of columns: VBA
我正在尝试创建一个宏,用于删除在该行的任何单元格中不包含部分字符串“.csv”的行。需要注意的是,该字符串不会出现在同一列中。
如果在 B 列中找到明确的“.csv”,则以下代码有效:
(我需要部分字符串搜索并跨多个列)
Sub DeleteRows()
' Defines variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
' Defines LastRow as the last row of data based on column E
LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).row
' Sets check range as E1 to the last row of E
Set cRange = Range("B1:B" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
With cRange.Cells(x)
' If the cell does not contain one of the listed values then...
If .Value <> ".csv" Then
'And .Value <> "PENDING" And .Value <> "CANCELLED" Add to line above if you want to add specifications'
' Delete that row
.EntireRow.Delete
End If
End With
' Check next cell, working upwards
Next x
End Sub
使用 InStr() 进行部分搜索并添加一个内部 for 循环以遍历每个单元格(列)。如果您点击要删除的行,则在删除该行后立即使用“exit for”。这将退出内部 for 循环并继续到下一行。
此代码将搜索不包含至少一个“.csv”字符串的所有行并删除整行。
Sub SearchForCSVRowsAndDeleteThem()
Dim CSVLocation As Range, LastCellOfRow As Range, counter As Long, Lastrow As Long, sh As Worksheet
Lastrow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row 'Find Last row
Set sh = ThisWorkbook.Worksheets("Sheet1")
For counter = Lastrow To 2 Step -1
Set LastCellOfRow = sh.Range("XFD" & counter) 'Start searching after last cell of row
Set CSVLocation = sh.Range(counter & ":" & counter). _
Find(".csv", after:=LastCellOfRow, LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlNext) 'Search in each row
If CSVLocation Is Nothing Then
sh.cells(counter,1).EntireRow.Delete 'Delete Row if .csv is not found
End If
Next counter
End Sub
此发布代码的改编使用 COUNTIF
搜索整行以查找与 .csv
的任何部分匹配项,如果找到 none,则整行将被删除。
如果需要,它可以调整为只删除行的一部分,但为此需要更多信息。
Option Explicit
Sub DeleteRows()
' Define variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
Dim Res As Variant
' Define LastRow as the last row of data based on column E
LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
' Sets check range as E1 to the last row of E
Set cRange = Range("B1:B" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
' find if '.csv' found in any cell in the row
Res = Evaluate("COUNTIF(" & x & ":" & x & ", ""*.csv*"")")
' if '.csv' not found delete row
If Res = 0 Then
ActiveSheet.Rows(x).Delete
End If
Next x
End Sub
我正在尝试创建一个宏,用于删除在该行的任何单元格中不包含部分字符串“.csv”的行。需要注意的是,该字符串不会出现在同一列中。
如果在 B 列中找到明确的“.csv”,则以下代码有效: (我需要部分字符串搜索并跨多个列)
Sub DeleteRows()
' Defines variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
' Defines LastRow as the last row of data based on column E
LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).row
' Sets check range as E1 to the last row of E
Set cRange = Range("B1:B" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
With cRange.Cells(x)
' If the cell does not contain one of the listed values then...
If .Value <> ".csv" Then
'And .Value <> "PENDING" And .Value <> "CANCELLED" Add to line above if you want to add specifications'
' Delete that row
.EntireRow.Delete
End If
End With
' Check next cell, working upwards
Next x
End Sub
使用 InStr() 进行部分搜索并添加一个内部 for 循环以遍历每个单元格(列)。如果您点击要删除的行,则在删除该行后立即使用“exit for”。这将退出内部 for 循环并继续到下一行。
此代码将搜索不包含至少一个“.csv”字符串的所有行并删除整行。
Sub SearchForCSVRowsAndDeleteThem()
Dim CSVLocation As Range, LastCellOfRow As Range, counter As Long, Lastrow As Long, sh As Worksheet
Lastrow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row 'Find Last row
Set sh = ThisWorkbook.Worksheets("Sheet1")
For counter = Lastrow To 2 Step -1
Set LastCellOfRow = sh.Range("XFD" & counter) 'Start searching after last cell of row
Set CSVLocation = sh.Range(counter & ":" & counter). _
Find(".csv", after:=LastCellOfRow, LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlNext) 'Search in each row
If CSVLocation Is Nothing Then
sh.cells(counter,1).EntireRow.Delete 'Delete Row if .csv is not found
End If
Next counter
End Sub
此发布代码的改编使用 COUNTIF
搜索整行以查找与 .csv
的任何部分匹配项,如果找到 none,则整行将被删除。
如果需要,它可以调整为只删除行的一部分,但为此需要更多信息。
Option Explicit
Sub DeleteRows()
' Define variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long
Dim Res As Variant
' Define LastRow as the last row of data based on column E
LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
' Sets check range as E1 to the last row of E
Set cRange = Range("B1:B" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
' find if '.csv' found in any cell in the row
Res = Evaluate("COUNTIF(" & x & ":" & x & ", ""*.csv*"")")
' if '.csv' not found delete row
If Res = 0 Then
ActiveSheet.Rows(x).Delete
End If
Next x
End Sub