2013-02-08 2 views
3

매크로에 익숙하지 만 AW 열을 필터링하려고 시도하고 AW 열에 해당 기준에 맞는 텍스트를 입력하려고합니다. 물론 보이는 셀에 해당 텍스트를 채우고 AZ 열에서 필터링 된 다른 기준을 사용하여 프로세스를 반복하고 싶습니다. 나는 아래의 코딩을 사용하고 있지만 AZ2의 컬럼 AZ를 채우지는 않습니다! 나는 헤더가 영향을 받길 원하지 않는다. 어떤 도움을 주셔서 감사합니다! -AmyVBA 코드를 필터링 한 다음 다른 열의 보이는 셀에 대한 수식을 채우십시오.

Sub Macro16() 

' Macro16 Macro 

'Insert Column - OK 
Columns("AZ:AZ").Select 
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove 
Range("AZ1").Select 
ActiveCell.FormulaR1C1 = "Finalized Comment" 
Rows("1:1").Select 
Range("AS1").Activate 
Selection.AutoFilter 


'Filter Combined Comment for #NA then type "Style linked to a Dropped T/P" 


Dim lastRow As Long 

With ActiveSheet 
    .Range("AW2").AutoFilter Field:=2, Criteria1:="#N/A" 
    lastRow = .Range("AW" & Rows.Count).End(xlUp).Row 
    .Range(.Range("AZ2"), .Range("AZ" & lastRow)). _ 
      SpecialCells(xlCellTypeVisible).Value = _ 
        "Style Linked to a Dropped T/P" 
End With 

'Filter Combined Comment for "Confirmed Cost and Missing HTS Code" then =Combined Comment 

Dim lastRow As Long 

With ActiveSheet 
    .Range("AW2").AutoFilter Field:=2, Criteria1:="Confirmed Cost and Missing HTS Code" 
    lastRow = .Range("AW" & Rows.Count).End(xlUp).Row 
    .Range(.Range("AZ2"), .Range("AZ" & lastRow)). _ 
      SpecialCells(xlCellTypeVisible).Value = _ 
        "Confirmed Cost and Missing HTS Code" 
End With 


'Filter Combined Comment for "Unconfirmed Cost and HTS Code Present" then =Unconfirmed Cost 

Dim lastRow As Long 

With ActiveSheet 
    .Range("AW2").AutoFilter Field:=2, Criteria1:="Unconfirmed Cost and HTS Code Present" 
    lastRow = .Range("AW" & Rows.Count).End(xlUp).Row 
    .Range(.Range("AZ2"), .Range("AZ" & lastRow)). _ 
      SpecialCells(xlCellTypeVisible).Value = _ 
        "Unconfirmed Cost" 
End With 

'Filter Combined Comment for "Unconfirmed Cost and Missing HTS Code" then =Missing HTS 

Dim lastRow As Long 

With ActiveSheet 
    .Range("AW2").AutoFilter Field:=2, Criteria1:="Unconfirmed Cost and Missing HTS Code" 
    lastRow = .Range("AW" & Rows.Count).End(xlUp).Row 
    .Range(.Range("AZ2"), .Range("AZ" & lastRow)). _ 
      SpecialCells(xlCellTypeVisible).Value = _ 
        "Missing HTS Code" 
End With 




End Sub 

답변

1
Sub Tester() 
Dim lastRow As Long 

    With ActiveSheet 
     .Range("AW2").AutoFilter Field:=2, Criteria1:="Test" 
     lastRow = .Range("AW" & Rows.Count).End(xlUp).Row 
     .Range(.Range("AZ2"), .Range("AZ" & lastRow)). _ 
       SpecialCells(xlCellTypeVisible).Value = _ 
         "Style Linked to a Dropped T/P" 
    End With 

End Sub 

EDIT :

Sub Macro16() 

Dim lastRow As Long 

    'Insert Column - OK 
    ActiveSheet.Columns("AZ:AZ").Insert Shift:=xlToRight, _ 
         CopyOrigin:=xlFormatFromLeftOrAbove 
    Range("AZ1").Value = "Finalized Comment" 

    TagRows "#N/A", "Style Linked to a Dropped T/P" 
    TagRows "Confirmed Cost and Missing HTS Code", _ 
      "Confirmed Cost and Missing HTS Code" 
    TagRows "Unconfirmed Cost and HTS Code Present", "Unconfirmed Cost" 
    TagRows "Unconfirmed Cost and Missing HTS Code", "Missing HTS Code" 

End Sub 

Sub TagRows(TextToFind As String, TagWithText As String) 
    Dim lastRow As Long 
    With ActiveSheet 
     'filter the column for "TextToFind" 
     .Range("AW:AW").AutoFilter Field:=1, Criteria1:=TextToFind 
     'find the last row 
     lastRow = .Range("AW" & Rows.Count).End(xlUp).Row 
     'if any visible rows, fill in the new comment "TagWithText" 
     If lastRow > 2 Then 
      .Range(.Range("AZ2"), .Range("AZ" & lastRow)). _ 
       SpecialCells(xlCellTypeVisible).Value = TagWithText 
     End If 
     .Range("AW:AW").AutoFilter Field:=1 'clear the filter 
    End With 
End Sub 
+0

정말 고마워요! 꿈처럼 일했다. :-) –

+0

Tim -이 코드를 AZ 열의 다른 3 가지 기준으로 다시 사용하려고하면 "현재 스코프에서 중복 선언"오류 (lastRow As Long)가 강조 표시됩니다. –

+0

질문을 업데이트하여 지금 작성한 코드를 추가 할 수 있습니까? –

0

Range.AutoFilter Method을 해체 및 인 메모리 어레이는이 과정을 위로 빠르게한다 내에 엄격 처리 ... 업데이트 비트를 재. 2.06 초 : 열 AW의 값의 75 %가 일치하는 항목을 찾을 것 랜덤 데이터의 250K 행에 대한

Option Explicit 

Sub tagAZ() 
    Dim t As Variant, vFNDs As Variant, vTAGs As Variant 
    Dim a As Long, vAWs As Variant, vAZs As Variant 

    appTGGL bTGGL:=False 

    vFNDs = Array("#N/A", "Confirmed Cost and Missing HTS Code", _ 
        "Unconfirmed Cost and HTS Code Present", _ 
        "Unconfirmed Cost and Missing HTS Code") 
    vTAGs = Array("Style Linked to a Dropped T/P", "Confirmed Cost and Missing HTS Code", _ 
        "Unconfirmed Cost", "Missing HTS Code") 

    With Worksheets("Sheet1") 
     .Columns(52).Insert 
     .Cells(1, 52) = "tag comment" 
     .Columns(52).ColumnWidth = 32 
     With .Range(.Cells(2, 49), .Cells(Rows.Count, 49).End(xlUp)) 
      vAWs = .Cells.Value2 
      ReDim vAZs(LBound(vAWs, 1) To UBound(vAWs, 1), 1 To 1) 

      For a = LBound(vAWs, 1) To UBound(vAWs, 1) 
       Select Case True 
        'catch True errors 
        Case IsError(vAWs(a, 1)) 
         If vAWs(a, 1) = CVErr(xlErrNA) Then _ 
          vAZs(a, 1) = vTAGs(0) 
        'catch text-that-looks-like-an-error 
        Case vAWs(a, 1) = vFNDs(0) 
         vAZs(a, 1) = vTAGs(0) 
        'catch the rest 
        Case vAWs(a, 1) = vFNDs(1) 
         vAZs(a, 1) = vTAGs(1) 
        Case vAWs(a, 1) = vFNDs(2) 
         vAZs(a, 1) = vTAGs(2) 
        Case vAWs(a, 1) = vFNDs(3) 
         vAZs(a, 1) = vTAGs(3) 
       End Select 
      Next a 

     End With 

     'return processed tag comments to the worksheet 
     .Cells(2, 52).Resize(UBound(vAZs, 1), UBound(vAZs, 2)) = vAZs 
    End With 

    appTGGL 

End Sub 

Public Sub appTGGL(Optional bTGGL As Boolean = True) 
    Debug.Print Timer 
    With Application 
     .ScreenUpdating = bTGGL 
     .EnableEvents = bTGGL 
     .DisplayAlerts = bTGGL 
     .Calculation = IIf(bTGGL, xlCalculationAutomatic, xlCalculationManual) 
    End With 
End Sub 

경과 시간. 같은 데이터를 동일한 루프를 통해 실행하면 .AutoFilter Method (동일한 환경 속성이 비활성화 된 상태에서) 24.25 초가 걸립니다.