フィルター設定 |
|
|
---|---|---|
フィルターの解除 |
|
|
フィルターが掛かっていたら、全て表示 |
|
|
フィルターが絞り込まれている |
|
|
フィルターの状態確認 |
|
|
|
フィルター設定なし ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A14") _ , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C2:C14") _ , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").Sort .SetRange Range("A1:H14") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With フィルター設定在り Range("A1:H14").Select Range("A8").Activate ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _ ("A2:A14"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _ ("C2:C14"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End Withフィルターが掛かっている場合、フィルターによる非表示範囲がソート対象から除かれるので、 フィルター設定の状態が不明の場合は、ソート前にフィルターを解除しておく必要がある。
Set myRange = ActiveSheet.AutoFilter If Not myRange Is Nothing Then 'フィルターが設定されている ActiveSheet.Range("A1").AutoFilter End If
1 2 3 4 5 6 7 8 9 10 | ActiveWorkbook.Worksheets( "Sheet2" ).Sort.SortFields.Clear ActiveWorkbook.Worksheets( "Sheet2" ).Sort.SortFields.Add Key:=Range( "Q2:Q" & MaxRow_S), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets( "Sheet2" ).Sort .SetRange Range( "A1:S" & CInt (MaxRow)) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With |
ソートのオプション
Key1 文字列、Rangeオブジェクトを指定します。
Order1 xlAscending 昇順に並び替える
xlDescending 降順に並び替える
Key2 2番目に優先される文字列,Rangeオブジェクトを指定します
Order2 xlAscending 昇順に並び替える
xlDescending 降順に並び替える
Key3 3番目に優先される文字列,Rangeオブジェクトを指定します
Order3 xlAscending 昇順に並び替える
xlDescending 降順に並び替える
Header xlGuess 先頭行をタイトル行か自動判定する
xlNo 先頭行をタイトル行と見なさない
xlYes 先頭行をタイトル行と見なす
OrderCustom
MatchCase TRUE 大文字・小文字を区別する
FALSE 大文字・小文字を区別しない
Orientation xlSortRows 上から下へ(行の並び替え)
xlSortColumn 左から右へ(列の並び替え)
SortMethod xlPinYin
xlStroke
1 2 3 4 5 6 7 8 9 10 11 | ActiveWorkbook.Worksheets( "Sheet2" ).Sort.SortFields.Clear ActiveWorkbook.Worksheets( "Sheet2" ).Sort.SortFields.Add Key:=Range( "Q2:Q" & MaxRow_S), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ActiveWorkbook.Worksheets( "Sheet2" ).Sort.SortFields.Add Key:=Range( "A2:A" & MaxRow_S), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets( "Sheet2" ).Sort .SetRange Range( "A1:S" & CInt (MaxRow)) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With |
1 2 3 4 5 6 7 8 9 10 11 | ActiveWorkbook.Worksheets(入力Tab).Sort.SortFields.Clear ActiveWorkbook.Worksheets(入力Tab).Sort.SortFields.Add Key:=Range( _ "AB2:AB" & RowMax), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption :=xlSortNormal With ActiveWorkbook.Worksheets(入力Tab).Sort .SetRange Range( "A1:AI" & RowMax) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With |
1 2 3 4 5 6 7 8 9 10 11 | ActiveWorkbook.Worksheets(入力Tab).Sort.SortFields.Clear ActiveWorkbook.Worksheets(入力Tab).Sort.SortFields.Add Key:=Range( _ "R2:R" & RowMax), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption :=xlSortNormal With ActiveWorkbook.Worksheets(入力Tab).Sort .SetRange Range( "A1:AI" & RowMax) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With |
検索 |
|
|
---|---|---|
置換 |
|
1 2 3 4 5 6 7 | schWhat = "7" Set FoundCell = Range( "W:W" ).Find(What:=schWhat) If FoundCell Is Nothing Then MsgBox "検索に失敗しました" Else DelStart = FoundCell.Row End If |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Dim schSheet As String '検索シート名 Dim schColumns As String '検索列 Dim schRg As Range '検索範囲 Dim schWhat As String '検索文字 Dim fndCell As Range '検索したセル Dim fstRow As Long '検索したセルの行 Dim fstColumn As Integer '検索したセルの列 schSheet = 入力Tab schColumns = "R:R" schWhat = "101" '検索範囲をセット Set schRg = Worksheets(schSheet).Columns(schColumns) 'アクティブセルを検索範囲の使っている最後のセルにする Range( "R2" ). Select '検索開始 Set fndCell = schRg.Find(What:=schWhat, after:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:= False , MatchByte:= False ) '見つかった! If Not (fndCell Is Nothing ) Then '最初に見つかったセルの行、列番号を記憶 fstRow = fndCell.Row fstColumn = fndCell.Column End If |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | Dim schSheet As String '検索シート名 Dim schColumns As String '検索列 Dim schRg As Range '検索範囲 Dim schWhat As String '検索文字 Dim fndCell As Range '検索したセル Dim fstRow As Long '検索したセルの行 Dim fstColumn As Integer '検索したセルの列 schSheet = 入力Tab schColumns = "W:W" schWhat = "0" '検索範囲をセット Set schRg = Worksheets(schSheet).Columns(schColumns) '検索範囲のアクティブセル以降を検索 Range( "W2" ). Select '検索開始 Set fndCell = schRg.Find(What:=schWhat, after:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:= False , MatchByte:= False ) '見つかった! If Not (fndCell Is Nothing ) Then '最初に見つかったセルの行、列番号を記憶 fstRow = fndCell.Row fstColumn = fndCell.Column End If |