2017-08-11 1 views
1

私はvbaの新人です。比較的簡単なものについています: 私はヘッダー名から列番号を指定しています:データをソートするためのダイナミックレンジ(列)をコーディングするVBA

Dim onomata As Integer 
'find column named Name of ship 
Set acell = rows(1).Find(What:="Name of ship", LookIn:=xlValues, _ 
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False) 

If Not acell Is Nothing Then 
    onomata = acell.Column 
End If 

今、私はこのコラムに基づいて自分のデータをソートする:コマンドが範囲値を要求しながら

Cells.Select 
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear 
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range(*this is where I want to introduce the column*), _ 
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ 
    xlSortTextAsNumbers 
With ActiveWorkbook.ActiveSheet.Sort 
    .SetRange Range("A1:AR100000") 
    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 
End With 

はしかし、私の変数は整数です。それをコード化する方法を教えてもらえますか?

あなたはこれだけを必要とする

答えて

0

ActiveWorkbook.ActiveSheetを使用することには意味がありません、十分です。

onomataの数値結果に従ってソートするには、次のコードを使用してください。

With ActiveSheet 
    .Sort.SortFields.Clear 
    .Sort.SortFields.Add Key:=Columns(onomata), SortOn:=xlSortOnValues, _ 
        Order:=xlAscending, DataOption:=xlSortTextAsNumbers 
End With 
+0

多くのありがとう!それは完全に動作します! –

+0

@ e-gnacioあなたは大丈夫です –

+0

2つ以上の基準で並べ替えたい場合はどうすればよいですか?最初の並べ替えは列Aで、次に列Bで並べ替えるのが好きですか? –

0
ActiveSheet.Sort.SortFields.Add Key:= ActiveSheet.Cells(onomata).EntireColumn 
1

... ActiveSheetを述べ

Dim acell As Range 

'find column named Name of ship 
Set acell = Rows(1).Find(What:="Name of ship", LookIn:=xlValues, _ 
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False) 

If Not acell Is Nothing Then   
    ActiveSheet.Sort.SortFields.Clear 
    ActiveSheet.Range("A1").Sort key1:=acell.Offset(1, 0), order1:=xlAscending, Header:=xlYes 
End If 
+1

「ActiveWorkbook.ActiveSheet」??? –

+0

@ShaiRadoはい、意味がありません。私はそれをよく見ておらず、OPのコードをコピーしました。 Lolそれを指摘してくれてありがとう。 :) – sktneer

関連する問題