2016-06-29 3 views
0

다른 시트의 값을 통합하는 매크로가 있으며이 값을 기반으로 첫 번째 시트로 돌아가서 삭제해야합니다. 그러나 매크로는 내가 지키고 자하는 것을 삭제하고 있습니다.프로그램 코드에서 보관하려는 데이터를 삭제합니다.

The sheet. 에

The Macro: 
Sub DeleteOthers() 
    Dim r1 As Range, c As Range 
    Dim t As String 

    With Sheets("Sheet2") 
     Set r1 = .Range(.Cells(2, "H"), .Cells(Rows.Count, "H").End(xlUp)) 
    End With 

    For Each c In r1 
     If c.Text = "<<<Keep Row" Then 
      Sheets("Sheet1").Select 
      t = c.Offset(0, -1) 
      Rows(t).ClearContents 
     End If 
    Next 
End Sub 
+2

Vityata가 답을 주었지만'.Text'를 사용하지 말 것을 추가하고 싶습니다. '.Text'는 실제로 화면에 표시되는 문자열입니다. 따라서 열이 좁아지면''#### ''이되고 모든 것이 삭제됩니다. 대신'.Value'를 사용하십시오. – arcadeprecinct

답변

0

변경 : Vityata의 대답 @

If c.Text <> "<<<Keep Row" Then 
0

바로 문제의 핵심에 도달 - 대신 =<> 여기에 주요 문제입니다 사용.

은 (c.Value 대신 c.Text의 사용 @의 arcadeprecinct의 추천은 좋은 생각입니다.)

Excel에서 손으로이 문제를 해결한다면, 당신은 아마 비록 라인 별을 가지 않을 것이다 - 권리? Excel의 기본 제공 필터링을 사용하고 VBA를 사용하면 Range 개체에 AutoFilter 메서드가 내장되어있어 서둘러 똑같은 훌륭한 결과를 얻을 수 있습니다.

Option Explicit 
Sub DeleteOthersWithAutoFilter() 
    Dim r1 As Range, c As Range 

    'Grab the full data range, not just column H 
    With Sheets("Sheet2") 
     Set r1 = .Range(.Cells(1, 1), .Cells(Rows.Count, "N").End(xlUp)) 
    End With 

    With r1 

     'Apply the Autofilter method to column H in the range 
     'identifying any cells NOT containing "<<<Keep Row" 
     .AutoFilter Field:=8, _ 
        Criteria1:="<><<<Keep Row" 

     'Clear the remaining, visible rows while keeping the headers 
     Set c = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible) 
     c.ClearContents 

    End With 

    'Clear the filter 
    Sheets("Sheet2").AutoFilterMode = False 

End Sub 

Range.AutoFilter를 사용하는 대신 개별 행을 반복하면 데이터 세트가 커질 특히, 매우 빠르게 할 수 있습니다.

현재 행과 Range.AutoFilter 통해 반복, 두 전략의 차이점에 대해 자세히 알아볼 수 있습니다 :

행동의 각을 벤치마킹 YouTube 동영상을 포함

https://danwagner.co/how-to-delete-rows-with-range-autofilter/

.

+0

안녕하세요, 고맙습니다.하지만 이렇게하면 시트 2에서 삭제됩니다. 시트 1로 변경하면 삭제하고 싶습니다. 나는 세트 c =와 c.clear 앞에 시트 1을보고 청소를하려고했지만 작업하지는 않습니다. – TerrorJapones