メインコンテンツへスキップ

Excelのヘッダーをクリックして列データを並べ替える方法は?

ある範囲のデータがあるとします。次に、列ヘッダーをクリックしてデータを昇順または降順で並べ替え、次のスクリーンショットを表示します。 この仕事をExcelでどのように解決できますか?

クリックによるドキュメントの並べ替え1

VBAコードを含む列ヘッダーをクリックしてデータを並べ替えます


矢印青い右バブル VBAコードを含む列ヘッダーをクリックしてデータを並べ替えます

通常、Excelでは、並べ替え機能を適用してデータをすばやく簡単に並べ替えることができますが、セルをクリックするだけでデータを並べ替えるには、次のVBAコードを使用すると便利です。

1。 セルをクリックしてデータを並べ替えるシートタブを右クリックし、 コードを表示 コンテキストメニューから、開いた状態で アプリケーション用のMicrosoftVisual Basic ウィンドウで、次のコードをコピーして空のモジュールに貼り付けます。

VBAコード:セルまたは列ヘッダーをクリックしてデータを並べ替えます。

Public blnToggle As Boolean
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)
'Updateby Extendoffice
Dim LastColumn As Long, keyColumn As Long, LastRow As Long
Dim SortRange As Range
LastColumn = _
Cells.Find(What:="*", After:=Range("A1"), _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
keyColumn = Target.Column
If keyColumn > LastColumn Then Exit Sub
Application.ScreenUpdating = False
Cancel = True
LastRow = Cells(Rows.Count, keyColumn).End(xlUp).Row
Set SortRange = Target.CurrentRegion
blnToggle = Not blnToggle
If blnToggle = True Then
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlAscending, Header:=xlYes
Else
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlDescending, Header:=xlYes
End If
Set SortRange = Nothing
Application.ScreenUpdating = True
End Sub

クリックによるドキュメントの並べ替え2

2。 次に、コードウィンドウを保存して閉じます。データ範囲内のセルまたは列ヘッダーをダブルクリックすると、列が昇順で並べ替えられます。もう一度ダブルクリックすると、列が一度に降順で並べ替えられます。


その他の関連記事:

セルをクリックしてセル値を変更するにはどうすればよいですか?

Excelでセルの内容をクリックするだけでデータをフィルタリングするにはどうすればよいですか?

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性を 50% 向上させ、毎日何百回もマウス クリックを減らすことができます!
Comments (9)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hallo,
der Code funktioniert auch gut bei mir. Allerdings würde ich gerne die oberen beiden Zeilen nicht mit sortieren, da diese die Überschriften sind.
Wie muss ich dann diesen Code ändern?

Vielen Dank!!
This comment was minimized by the moderator on the site
Hello friend,
Here is the VBA you need:

Public blnToggle As Boolean
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)
'Updateby Extendoffice
Dim LastColumn As Long, keyColumn As Long, LastRow As Long
Dim SortRange As Range
LastColumn = _
Cells.Find(What:="*", After:=Range("A1"), _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
keyColumn = Target.Column
If keyColumn > LastColumn Then Exit Sub
Application.ScreenUpdating = False
Cancel = True
LastRow = Cells(Rows.Count, keyColumn).End(xlUp).Row
On Error Resume Next
Set SortRange = Target.CurrentRegion
Dim i As Long
i = 2
Set SortRange = SortRange.Offset(i, 0)
Set SortRange = SortRange.Resize(SortRange.Rows.Count - i, SortRange.Columns.Count)
blnToggle = Not blnToggle
If blnToggle = True Then
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlAscending, Header:=xlNo
Else
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlDescending, Header:=xlNo
End If
Set SortRange = Nothing
Application.ScreenUpdating = True
End Sub


If you have headers of 3 rows, just change "i =2" to "i =3" in the VBA. Hope it helps. Have a great day.

Sincerely,
Mandy
This comment was minimized by the moderator on the site
Hi Mandy/all,

Is it possible to change your code to only sort when the headers are double clicked instead of any cell?

Thank you very much!
This comment was minimized by the moderator on the site
Hello,
To solve your problem, please apply the below code:
Public blnToggle As Boolean
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)
'Updateby Extendoffice
Dim LastColumn As Long, keyColumn As Long, LastRow As Long
Dim SortRange As Range
Dim xAddress As String
Dim xRgI As Range
xAddress = "A1:E2" 'The headers
Set xRgI = Intersect(Range(xAddress), Target)
If xRgI Is Nothing Then Exit Sub
LastColumn = _
Cells.Find(What:="*", After:=Range("A1"), _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
keyColumn = Target.Column
If keyColumn > LastColumn Then Exit Sub
Application.ScreenUpdating = False
Cancel = True
LastRow = Cells(Rows.Count, keyColumn).End(xlUp).Row
On Error Resume Next
Set SortRange = Target.CurrentRegion
Dim i As Long
i = 2
Set SortRange = SortRange.Offset(i, 0)
Set SortRange = SortRange.Resize(SortRange.Rows.Count - i, SortRange.Columns.Count)
blnToggle = Not blnToggle
If blnToggle = True Then
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlAscending, Header:=xlNo
Else
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlDescending, Header:=xlNo
End If
Set SortRange = Nothing
Application.ScreenUpdating = True
End Sub



Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
This works perfectly! Thank you very much skyyang!
This comment was minimized by the moderator on the site
No can do crackerjack - don't work
This comment was minimized by the moderator on the site
Hi, Rob, The above code works well in my Excel, can you give your problem a screenshot here?
This comment was minimized by the moderator on the site
Doesn't work, nothing happens, know how to create module in vba, did that, saved and nothing when header double clicked. Please fix it.
This comment was minimized by the moderator on the site
Works ok to ascend, double click a 2nd time as stated to descend does nothing
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations