我使用此代码删除所有工作表中包含特定文本的所有行。有什么方法可以保留所有行的特定文本并删除其余的行?非常感谢您的帮助。
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
$data_row = '<table class="table table-striped tablesorter">'
. '<thead>'
. '<tr>'
. '<th>ID</th>'
. '<th>Contract Number</th>'
. '<th>Property Name</th>'
. '<th>Street Address</th>'
. '<th>City</th>'
. '<th>State</th>'
. '<th>Zip</th>'
. '<th>Date</th>'
. '<th>Status</th>'
. '</tr>'
. '<tbody>';
foreach($result as $row) {
$data_row .= '<tr>'
. '<td scope="row" class="id-c text-center">'.$row['id'].'</td>'
. '<td>' .$row['contract_number'].'</td>'
. '<td>' .$row['property_name'].'</td>'
. '<td>' .$row['property_address'].'</td>'
. '<td>' .$row['city'].'</td>'
. '<td>' .$row['state'].'</td>'
. '<td>' .$row['zip'].'</td>'
. '<td>' .date("M-d-y", strtotime($row['order_date'])).'</td>'
. '<td>' .$row['status'].'</td>';
}
}
$data_row .= '</tbody>'
. '</table>';
echo $data_row;
答案 0 :(得分:4)
例如,如果您想要使用'Oakville'的行,那么只需将=更改为&lt;&gt;在这段代码本身
Sub WorksheetLoop()
Dim c As Integer
Dim n As Integer
c = ActiveWorkbook.Worksheets.Count
For n = 1 To c Step 1
Last = Worksheets(n).Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (Worksheets(n).Cells(i, "A").Value) <> "Oakville" Then
Worksheets(n).Cells(i, "A").EntireRow.Delete
End If
Next i
Next n
End Sub
答案 1 :(得分:0)
实现这一目标的另一种方法如下......
Sub DeleteRows()
Dim ws As Worksheet
Dim LastRow As Long, LastCol As Long
Application.ScreenUpdating = False
For Each ws In Worksheets
LastRow = ws.UsedRange.Rows.Count
LastCol = ws.UsedRange.Columns.Count + 1
With ws
.Range(.Cells(1, LastCol), .Cells(LastRow, LastCol)).Formula = "=IF(A1=""Oakville"","""",NA())"
On Error Resume Next
.Range(.Cells(1, LastCol), .Cells(LastRow, LastCol)).SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
.Columns(LastCol).Clear
End With
Next ws
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:0)
考虑AutoFilter
方式,无需循环,无需担心删除顺序,也不需要逐个删除;它一次性删除不需要的行。这绝对是我推荐的方法,尝试一下:
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws.UsedRange
.AutoFilter 1, "<>Oakville"
.Offset(1).EntireRow.Delete
.AutoFilter
End With
Next
这保留了“奥克维尔”。删除<>
以删除“Oakville”。