【发布时间】:2017-02-03 05:36:11
【问题描述】:
我有一个 20 张工作簿。每张纸大约有 30,000 行带有 URL。我有一手需要保存数据的 URL(大约 10 个不同的 URL)。如果第一列(A 列 - URL)不包含其中一个 URL,有没有办法从所有工作表中删除所有行。
我有以下 vba,但它会删除所有行。如果该值与我在下面编码的内容相匹配,我需要保留该行。它还在最后抛出 424 错误(也删除所有行)。任何想法?仅查看 A 列而不是放置单元格区域的任何方式,因为它在每张工作表之间有所不同。
Sub DeleteCells()
Dim rng As Range, i As Integer
'Set the range to evaluate to range.
Set rng = Range("A1:A10000")
'Loop backwards through the rows
'in the range that you want to evaluate.
For i = rng.Rows.Count To 1 Step -1
'If cell i in the range DOES NOT contains an "x", delete the entire row.
If rng.Cells(i).Value <> "https://inside.nov.pvt/ip/hse" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/ip/hse/qhseprivate" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/crp/qhse" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/crp/qhse/csa" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/ops/ehqhse" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/ops/hsehw" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/ops/lahse" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/sites/coloproposal/HSEQ AND GENERAL DOCUMENTS" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/sites/coloproposal/HSEQ AND GENERAL DOCUMENTS/LA OPERATIONS MEETING APRIL 2012" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/crp/hse" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/crp/hse/CorpQHSE" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/crp/hse/IP" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/mfg/mfg/HSE" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/mfg/mfg/HSET" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/ops/na/HSE" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/ops/na/HSE/er" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/ops/na/HSE/GCR" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/ops/na/HSE/wr" Then rng.Cells(i).EntireRow.Delete
If rng.Cells(i).Value <> "https://inside.nov.pvt/ops/mexopex" Then rng.Cells(i).EntireRow.Delete
Next
End Sub
【问题讨论】:
-
使用 if 语句,因为您拥有它们,但每个循环都会返回 true,从而删除该行。你需要一个如果在检查之间使用
AndIf rng.Cells(i).Value <> "https://inside.nov.pvt/ip/hse" And rng.Cells(i).Value <> "https://inside.nov.pvt/ip/hse/qhseprivate" And ... -
感谢 Scott 的快速回复。
-
您将在第一个命令中删除除 inside.nov.pvt/ip/hse 之外的所有内容。然后您将使用第二个命令删除其他所有内容。将您要保留的所有 url 放入一个数组中。对所有需要的 url 执行多个 .find 并将它们的行号放入第二个数组中。创建一个临时的第二个工作表,然后将该数组中的每一行逐一复制到新工作表中。然后清除主工作表,并将新工作表中的数据复制回旧工作表。然后杀死新的工作表。
-
嗨,约翰,好主意。你有样品吗?我很感激。
-
无法发布我的答案,但您可以使用您的少数 URL 构造一个数组,然后检查您的行值是否在数组中。我几乎在this one 中发布了一个答案库。只需为工作表添加一个额外的循环,并将
Row(cell.Row).Style = "Accent1"替换为rng.Cells(i).EntireRow.Delete。当然还有你的网址的果实......