2017-11-16 2 views
0

일반적인 문제는 내가 찾은 모든 대답을 통해 마침내 거의 효과를 얻었습니다.xlfiltervalues에 대한 필터 기준으로 인접하지 않은 배열

할인 옵션 목록이 있습니다. 이름이 범위 F로, 1 열 아래로 지정됩니다. 사용자가 적용하지 않으려는 할인 혜택을 필터링합니다. 사용자가 선택하면 필터를 제거하고 작동하며 다시 채울 필요가 있습니다.

범위의 루프 및 합집합으로 보이는 셀만있는 배열을 만듭니다. 이 올바르게 작동하지만 일반적으로 인접하지 않은 배열을 생성합니다.

실행할 때 오류가 발생하지 않습니다. 그러나 연속 배열의 끊기 아래 항목은 다시 필터링되지 않습니다.

은 그냥 비 연속 배열을 좋아하지 않는 트랜스의 실현 - 여전히 (그것이 금요일 거의의 가장 쉬운, 가장 고통을 무엇

그대로 지원 및 의심 할 여지없이 다른 사람들이 그렇게 떠나 같은 문제가 필요합니다), Criteria1에게 내 인접하지 않은 배열의 마지막 요소를 포함하도록 설득하는 방법은 무엇입니까?

Sub Filters() 

'Dimension variables 
Dim Rng As Range 
Dim i, Lim As Integer 
Dim w As Worksheet 
Dim Op As Variant 

Set w = ActiveSheet 

'Set Lim as total number of rows in named range "F" (only 1 cell in use but same effect) 
Lim = Range("F").Rows.Count 

'Data has header row so skip to row 2 
i = 2 

'Loop through i up to limit 
Do While i <= Lim 
    'If the row is not hidden by the filters the user chose 

    If Range("F")(i, 1).EntireRow.Hidden = False Then 
     'Check if the range is nothing - if it is, union will not work to itself 
     'Union requires non-empty arguments 

     If Rng Is Nothing Then 
      'Set the Rng to include the current cell from "F" 
      Set Rng = Range("F")(i, 1) 

     Else 
      'If Rng has some value, add the current cell to it by Union 
      Set Rng = Application.Union(Rng, Range("F")(i, 1)) 

     End If 

    End If 

    'Increment i 
    i = i + 1 

    Loop 

    If w.AutoFilter.Filters.Item(1).Operator <> False Then Op = w.AutoFilter.Filters.Item(1).Operator 

    'This gives the correct range, but most often non-contiguous 
    MsgBox Range("F").Address 

    'Remove AutoFilter 
    w.AutoFilterMode = False 




    'Insert Code Here 




    'Put filters back 

    'Check for Rng being non-empty (pointless running code if it is) 
    If Not IsEmpty(Rng) Then 
     'If there is an operator then use the array 
     If Op Then 
      'Found this option useful here - can transpose the array values which generates an array Criteria1 can use 
      'Always xlFilterValues as there will always be more than 2 options 
      'Also the options are taken from the worksheet live so won't change between times so specifying them precisely as strings is ok 
      Range("F").AutoFilter Field:=1, Criteria1:=Application.Transpose(Rng.Value), _ 
      Operator:=xlFilterValues 
     Else 
      'Just filter the range but leave all options available 
      Range("F").AutoFilter Field:=1 
     End If 
    End If 

End Sub 
+0

"사용자가 적용하고 싶지 않은 할인을 필터링합니다. 사용자가 선택한대로 필터링, 작업 및 리필해야합니다." 어떤 종류의 일? "작업"의 성격에 따라 피벗 테이블을 사용하여 수행 할 수 있습니다. (아마도 Excel 2013 이상인 경우 아마도 DataModel을 활용할 것입니다.) – jeffreyweir

+0

답변 해 주셔서 감사합니다! 슬프게도 피벗 테이블이 나올 때 배우지 않았고 오래된 개가 필터 위의 행과 페이지 나누기를 삽입한다는 느낌이 들었습니다. – jfgoodhew1

답변

0

두 번째 카운터를 사용하여 기준으로 포함되어야하는 성공적인 항목을 계산하고 다른 워크 시트의 범위에 기록합니다. 그런 다음 범위를 새 워크 시트의 새 (인접한) 범위로 설정하십시오.

이제는 결국 매력처럼 작동합니다. 오직 하루 종일 Criteria와 함께 작동하는 구문을 찾았고 최대 2 가지 조건에 대해서만 xlOr을 사용할 수 있다는 것을 알았습니다. 그렇지 않으면 xlfiltervalues ​​...

최종 작업 코드는 다음과 같이 얻을 수 있습니다. 유용한 정보 :

Sub Filters() 

'Dimension variables 
Dim Rng As Range 
Dim i, j, Lim As Integer 
Dim w As Worksheet 
Dim Op As Variant 

Set w = ActiveSheet 

'Set Lim as total number of rows in named range "F" (only 1 cell in use but same effect) 
Lim = Range("F").Rows.Count 

'Data has header row so skip to row 2 
i = 2 

'Loop through i up to limit 
Do While i <= Lim 
    'If the row is not hidden by the filters the user chose 

    If Range("F")(i, 1).EntireRow.Hidden = False Then 
     'Check if the range is nothing - if it is, union will not work to itself 
     'Union requires non-empty arguments 

     If Rng Is Nothing Then 
      'Set the Rng to include the current cell from "F" 
      Set Rng = Range("F")(i, 1) 
      Sheets("Sheet2").Range("A75").Value = Range("F")(i, 1).Value 
      j = j + 1 
     Else 
      Sheets("Sheet2").Range("A1").Offset(j, 0).Value = Range("F")(i, 1).Value 
      j = j + 1 
     End If 

    End If 

'Increment i 
i = i + 1 

Loop 

'If there's an operator, save it as variable Op (if needed) 
If w.AutoFilter.Filters.Item(1).Operator <> False Then Op = w.AutoFilter.Filters.Item(1).Operator 


'Remove AutoFilter 
w.AutoFilterMode = False 




'Insert Code Here 

'Pause between the two halves 
MsgBox "" 



'Put filters back 

'Check for Rng being non-empty (pointless running code if it is) 
If Not IsEmpty(Rng) Then 
    'If there is an operator then use the range 
    If Op Then 
     'Found this option useful here - can transpose the array values 
     'Always xlFilterValues as there will always be more than 2 options 
     'Also the options are taken from the worksheet live so won't change between times so specifying them precisely as strings is ok 
     Range("F").AutoFilter Field:=1, Criteria1:=Application.Transpose(Sheets("Sheet2").Range("A75").Resize(j, 1).Value), _ 
     Operator:=xlFilterValues 
    Else 
     'Just filter the range but leave all options available 
     Range("F").AutoFilter Field:=1 
    End If 
End If 


End Sub 
관련 문제