2017-02-02 3 views
0

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 
+0

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 ... ' –

+0

빠른 답장을 보내 주신 Scott 께 감사드립니다. –

+0

첫 번째 명령에서 https : //inside.nov.pvt/ip/hse 이외의 모든 것을 삭제합니다. 그리고 두 번째 명령으로 다른 모든 것을 삭제합니다. 유지하려는 모든 URL을 배열에 넣으십시오. 필요한 모든 URL에 여러 .find를 수행하고 행 번호를 두 번째 배열에 넣습니다. 임시 두 번째 워크 시트를 만들고 아래로 이동하여 해당 배열의 각 행을 1 씩 1 씩 새 워크 시트에 복사합니다. 그런 다음 기본 워크 시트를 비우고 새 워크 시트의 데이터를 이전 워크 시트로 복사하십시오. 그런 다음 새 워크 시트를 제거하십시오. –

답변

1

새 시트를 만들고 채우려면이 방법을 사용해보십시오. 원하는 위치에 코드를 추가해야합니다.

Sub saveImportantData() 
    Dim myUrlArray, oldSheetRowArray, arrayCounter As Long 
    Dim tempWS As Worksheet, myWS As Worksheet, newSheetRowCounter As Long 

    ReDim oldSheetRowArray(1 To 1) 
    Set myWS = ActiveSheet 
    Set tempWS = Sheets.Add(After:=Sheets(Worksheets.Count)) 

    newSheetRowCounter = 1 
    arrayCounter = 1 
    myUrlArray = Array("https://inside.nov.pvt/ip/hse", _ 
        "https://inside.nov.pvt/ip/hse/qhseprivate", _ 
        "https://inside.nov.pvt/crp/qhse", _ 
        "https://inside.nov.pvt/crp/qhse/csa", _ 
        "https://inside.nov.pvt/crp/qhse/csa", _ 
        "https://inside.nov.pvt/ops/ehqhse", _ 
        "https://inside.nov.pvt/ops/hsehw", _ 
        "https://inside.nov.pvt/ops/lahse", _ 
        "https://inside.nov.pvt/sites/coloproposal/HSEQ AND GENERAL DOCUMENTS", _ 
        "https://inside.nov.pvt/sites/coloproposal/HSEQ AND GENERAL DOCUMENTS/LA OPERATIONS MEETING APRIL 2012", _ 
        "https://inside.nov.pvt/crp/hse", _ 
        "https://inside.nov.pvt/crp/hse/CorpQHSE", _ 
        "https://inside.nov.pvt/crp/hse/IP", _ 
        "https://inside.nov.pvt/mfg/mfg/HSE", _ 
        "https://inside.nov.pvt/mfg/mfg/HSET", _ 
        "https://inside.nov.pvt/ops/na/HSE", _ 
        "https://inside.nov.pvt/ops/na/HSE/er", _ 
        "https://inside.nov.pvt/ops/na/HSE/GCR", _ 
        "https://inside.nov.pvt/ops/na/HSE/wr", _ 
        "https://inside.nov.pvt/ops/mexopex") 

    For i = 1 To UBound(myUrlArray) 
     With myWS.Range("A1:A10000") 
     Set c = .Find(myUrlArray(i), LookIn:=xlValues) 
      If Not c Is Nothing Then 
       firstAddress = c.Address 
       Do 
        oldSheetRowArray(arrayCounter) = c.Row 
        arrayCounter = arrayCounter + 1 
        ReDim Preserve oldSheetRowArray(1 To arrayCounter) 
        Set c = .FindNext(c) 
       Loop While Not c Is Nothing And c.Address <> firstAddress 
      End If 
     End With 
    Next i 


    Application.ScreenUpdating = False 
    For k = 1 To UBound(oldSheetRowArray) 
     If oldSheetRowArray(k) <> "" Then 
      myWS.Activate 
      myWS.Rows(oldSheetRowArray(k) & ":" & oldSheetRowArray(k)).Select 
      Selection.Copy 
      tempWS.Activate 
      tempWS.Range("A" & newSheetRowCounter).Select 
      ActiveSheet.Paste 
      newSheetRowCounter = newSheetRowCounter + 1 
     End If 
    Next k 
    Application.ScreenUpdating = True 

    Set myWS = Nothing 
    Set tempWS = Nothing 
    Set c = Nothing 

End Sub 
+0

코드에 대해 감사드립니다. 나는 그것을 시도하고 알려 드리겠습니다. 감사합니다. –

+0

완벽하게 작동합니다. 새 워크 시트를 만든 다음 제목 1 행을 복사하고 이전 시트를 삭제하면됩니다. 존 감사합니다. –

관련 문제