20 개의 시트가있는 통합 문서가 있습니다. 각 시트에는 URL이있는 약 30,000 개의 행이 있습니다. 데이터를 보관할 필요가있는 URL (약 10 개의 다른 URL)이 가득합니다. 첫 번째 열 (열 A - URL)에 URL 중 하나가 포함되어 있지 않으면 모든 워크 시트에서 모든 행을 삭제하는 방법이 있습니까?VBA 특정 값이 포함되어 있지 않은 Excel에서 행을 삭제하려면
다음 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가 반환되어 각 행이 삭제되므로 행이 삭제됩니다. 체크가 필요한 경우 'And'를 사용하는 경우 If rng.Cells (i) .Value <> "https : //inside.nov.pvt/ip/hse"및 rng.Cells (i) .Value <> " https : //inside.nov.pvt/ip/hse/qhseprivate "And ... ' –
빠른 답장을 보내 주신 Scott 께 감사드립니다. –
첫 번째 명령에서 https : //inside.nov.pvt/ip/hse 이외의 모든 것을 삭제합니다. 그리고 두 번째 명령으로 다른 모든 것을 삭제합니다. 유지하려는 모든 URL을 배열에 넣으십시오. 필요한 모든 URL에 여러 .find를 수행하고 행 번호를 두 번째 배열에 넣습니다. 임시 두 번째 워크 시트를 만들고 아래로 이동하여 해당 배열의 각 행을 1 씩 1 씩 새 워크 시트에 복사합니다. 그런 다음 기본 워크 시트를 비우고 새 워크 시트의 데이터를 이전 워크 시트로 복사하십시오. 그런 다음 새 워크 시트를 제거하십시오. –