2017-11-30 6 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( "列の名前"))を設定しようとしています。 $ 1から$ K $ 1ですが、sortコマンドに入力すると、それは好きではありません。 ColName1を定義するために別のタイプを使用する必要がありますか?または、2番目の項目を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 
関連する問題