2017-11-30 18 views
2

여러 개의 열을 정렬 한 다음 0/1로 필터링해야하는 데이터가 있는데 단순히 시트를 사용하고 수행 한 열 주소를 찾고 싶습니다.VBA 정렬 및 범위 개체를 사용하여 필터

Dim ColName1 As Variant 
Set ColName1 = sht.Cells.Find("name of column").Address 

sort 명령의 일부에 사용할 수 있지만 다른 Varient 또는 String = Left (sht.Cells.Find ("column name"). Address, 2)를 제거하려고합니다. $ K $ 1에서 $ 1이지만 sort 명령을 입력 할 때 맘에 들지 않습니다. ColName1을 정의하기 위해 다른 유형을 사용해야합니까? 또는 두 번째 항목을 int/something으로 정의 할 수 있습니까?

ActiveWorkbook.Worksheets("test").sort.SortFields.Add Key:=Range(ColName1.Address & ":K" & LastRow) _ 
     , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal   'categ/material 
With ActiveWorkbook.Worksheets("test").sort 
     .SetRange Range(UsedRange) 
     .Header = xlYes 
     .MatchCase = False 
     .Orientation = xlTopToBottom 
     .SortMethod = xlPinYin 
     .Apply 
    End With 

나는 내가이 동적으로 만들 결국 난 그냥 파일에 읽기 인터페이스로 던져 정렬 할 열을 정의하는 사용자 정의 변수/객체를 사용하고 싶습니다 그래서 정렬 여러 열이 .

또한, 나는 현재 ActiveWorkbook.Worksheets를 참조하지에 대해 많은 것을 읽을 수는 있지만 내가

Set sht = ActiveSheet 

같은 것을 정의 할 때 그것은 단지 sht.sort을 몇 가지 작동하지만 ?? 내가 무엇을 놓치고 있는지 ...

답변

0

VBA에서 정렬하는 것은 너무 힘들다. 다음은이 방법에 접근하는 방법입니다.

Sub TestSort() 

Dim Sht As Worksheet 
Dim SortCol As Long 
Dim LastRow As Long 
Dim LastCol As Long 

Set Sht = ActiveSheet 
LastRow = Cells(Rows.Count, 1).End(xlUp).Row 
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column 
SortCol = Sht.Cells.Find("Name of Column").Column 
Sht.Sort.SortFields.Clear 
Sht.Sort.SortFields.Add Key:=Range(Cells(2, SortCol), Cells(LastRow, SortCol)) 
With Sht.Sort 
    .SetRange Range(Cells(1, 1), Cells(LastRow, LastCol)) 
    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 
End With 
End Sub