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

Excelでセル内の数値を並べ替える方法は?

列のリストで数値を並べ替えるのは簡単で一般的ですが、単一のセル内で数値を並べ替えようとしたことはありますか? XNUMXつずつ並べる以外に良い方法はないかもしれません。ここでは、Excelのセル内の数値を並べ替える方法について説明します。

数式を使用してセル内の数値を並べ替える

ユーザー定義関数を使用してセル内の数値を並べ替える

VBAコードを使用してセル内でコンマで区切られた数値を並べ替える


矢印青い右バブル 数式を使用してセル内の数値を並べ替える

ワークシートのセル内の数値を並べ替えるには、次の長い式を適用できます。次のようにしてください。

1。 データの横に次の数式を入力してください。この例では、セルC1に入力します。スクリーンショットを参照してください。

=TEXT(SUM(SMALL(--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),ROW(INDIRECT("1:"&LEN(A1))))*10^(LEN(A1)-ROW(INDIRECT("1:"&LEN(A1))))),REPT("0",LEN(A1)))

doc-sort-cells-numbers-1

2. 次に、 Ctrl + Shift + Enter キーを一緒に押してから、この数式を適用する範囲まで塗りつぶしハンドルをドラッグすると、数値が小さいものから大きいものへと並べ替えられます。 スクリーンショットを参照してください:

doc-sort-cells-numbers-1

注意:

1.セル内の数値の桁が15を超える場合、この数式は正しい結果を取得しません。

2.番号を降順で並べ替える場合は、次の式を使用できます。 =TEXT(SUM(LARGE(--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),ROW(INDIRECT("1:"&LEN(A1))))*10^(LEN(A1)-ROW(INDIRECT("1:"&LEN(A1))))),REPT("0",LEN(A1))).

3.上記の数式で、A1は、並べ替える数値を含むセルを示しています。必要に応じて変更できます。


矢印青い右バブル ユーザー定義関数を使用してセル内の数値を並べ替える

式にはいくつかの制限があるため、次のものを使用できます ユーザー定義関数 15桁より長いセルの数値を並べ替えます。

1。 を押し続けます Alt + F11 キー、そしてそれは開きます Microsoft Visual Basic forApplicationsウィンドウ.

2に設定します。 OK をクリックします。 インセット > モジュール、次のコードをに貼り付けます モジュールウィンドウ.

VBAコード:セル内の数値を並べ替える

Function SortNumsInCell(pNum As String, Optional pOrder As Boolean) As String
'Update 20140717
Dim xOutput As String
For i = 0 To 9
  For j = 1 To UBound(VBA.Split(pNum, i))
    xOutput = IIf(pOrder, i & xOutput, xOutput & i)
  Next
Next
SortNumsInCell = xOutput
End Function

3. 次に、このコードを保存して閉じ、ワークシートに戻って、この数式を入力します = sortnumsincell(A1) データの横の空白のセルに、スクリーンショットを参照してください。

doc-sort-cells-numbers-1

4。 次に、この数式を含めるセルに塗りつぶしハンドルをドラッグすると、セル内のすべての数値が次のスクリーンショットのように昇順で並べ替えられます。

doc-sort-cells-numbers-1

Note:番号を降順で並べ替える場合は、次の式を入力してください = sortnumsincell(A1,1)。


矢印青い右バブル VBAコードを使用してセル内でコンマで区切られた数値を並べ替える

次のスクリーンショットのように、数字がコンマ、セミコロン、ピリオドなどの特定の文字で区切られている場合、セルでそれらをどのように並べ替えることができますか? ここで、それらを並べ替えるためのVBAコードを紹介します。

doc-sort-cells-numbers-1

1。 を押し続けます Alt + F11 キーを押して Microsoft Visual Basic forApplicationsウィンドウ。

2に設定します。 OK をクリックします。 インセット > モジュール、次のコードをに貼り付けます モジュールウィンドウ.

VBAコード:並べ替え番号はセル内でコンマで区切られます

Sub SortNumsInRange()
'Update 20140717
Dim Rng As Range
Dim WorkRng As Range
Dim Arr As Variant
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set objArrayList = CreateObject("System.Collections.ArrayList")
For Each Rng In WorkRng
    Arr = VBA.Split(Rng.Value, ",")
    For i = 0 To UBound(Arr)
        xMin = i
        For j = i + 1 To UBound(Arr)
            If Arr(xMin) > Arr(j) Then
                xMin = j
            End If
        Next j
        If xMin <> i Then
            temp = Arr(i)
            Arr(i) = Arr(xMin)
            Arr(xMin) = temp
        End If
    Next i
    Rng.Value = VBA.Join(Arr, ",")
Next
End Sub

3。 次に、 F5 キーを押してこのコードを実行し、ポップアウトされたプロンプトボックスに番号が含まれているセルを選択します。スクリーンショットを参照してください。

doc-sort-cells-numbers-1

4。 そして、 OK、セル内のすべての数値は、元の範囲で昇順で並べ替えられています。

Note:上記のコードでは、必要に応じてコンマ「、」を他の任意の文字に変更できます。 そして、このコードはデータを昇順でのみソートできます。


関連記事:

Excelでハイフンを使用して数値を並べ替える方法は?

Excelで最も頻度の高い値でデータを並べ替える方法?

Excelでドメイン別にメールアドレスを並べ替える方法は?

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 (13)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hola, se pueden ordenar letras en una sola celda por orden alfabético? Yo uso excel para Mac. Gracias
This comment was minimized by the moderator on the site
Hi thanks for nice good for sorting numbers which separated by commas within cells with VBA code
Just I have faced one problem with the code.
The code cannot detect three digit number. for example the numbers (65, 93, 53, 72, 64, 85, 103, 48, 77, 81, 54) after applying the code, the new order (103, 48, 53, 54, 64, 65, 72, 77, 81, 85, 93)
Do you have any solution for the problem?
This comment was minimized by the moderator on the site
Hello, omer,May be the below code can help you, please try:
<div data-tag="code">Public Function CellSort(r As Range) As String
Dim bry() As Long, L As Long, U As Long
ch = r(1).Text
ary = Split(ch, ",")
L = LBound(ary)
U = UBound(ary)
ReDim bry(L To U)
For i = LBound(ary) To UBound(ary)
bry(i) = CLng(ary(i))
Next i
Call BubbleSort(bry)
For i = LBound(bry) To UBound(bry)
ary(i) = CStr(bry(i))
Next i
CellSort = Join(ary, ",")
End Function

Sub BubbleSort(arr)
Dim strTemp As Variant
Dim i As Long
Dim j As Long
Dim lngMin As Long
Dim lngMax As Long
lngMin = LBound(arr)
lngMax = UBound(arr)
For i = lngMin To lngMax - 1
For j = i + 1 To lngMax
If arr(i) > arr(j) Then
strTemp = arr(i)
arr(i) = arr(j)
arr(j) = strTemp
End If
Next j
Next i
End SubAfter inserting the above code, please apply this formula: =CellSort(A1).And you will get the result you need.
This comment was minimized by the moderator on the site
How to sort A-Z text within a cell in Excel?
This comment was minimized by the moderator on the site
hOLA, MI PROBLEMA ES QUE TENGO EXEL 2019 EN ESPAÑOL COMO SERIA LA FORMULA?
This comment was minimized by the moderator on the site
Hi, the VBA code seems to output incorrectly, example before 13,50,47,7,39 and after 13-39-47-50-7. Any ideas why?
This comment was minimized by the moderator on the site
i want to sort total an amount 14000 to 20000 from various row Example:- 2000,1500 one row and like that all row amount to arrange
This comment was minimized by the moderator on the site
need to sort 84-12-74-26-98 any order 12-26-74-84-98 or 98-84-74-26-12 thank you
This comment was minimized by the moderator on the site
If CInt(Arr(xMin)) > CInt(Arr(j)) and it works
This comment was minimized by the moderator on the site
Is there is any way to sort more numbers in same time from one cell? Example, i have a list of 50000 asset numbers such as A1234,A1235... and i need to pull 500 specific numbers and i need to pull 500 at the time to make change and save.Thank you
This comment was minimized by the moderator on the site
I have a series of cells with numbers separated by a space that I want to sort. eg 8 4 5 1 6 3 that I want to sort as 1 3 4 5 6 8 Any help appreciated
This comment was minimized by the moderator on the site
Hi, I was wondering how this UDF, =sortnumsincell(A1,1), can be modified more generally, like =sortnumsincell(A1," "," ",,1) where the first argument, A1, is the target cell, the second argument," ", is a delimiter that could take any character, or a space, or nothing, with third argument, " ", a different or same delimiter, and the fourth argument, 1 or 0, indicating an ascending or descending sort, with the result string displaying, correctly sorted, within one cell, with delimiter default same as the original string unless specified in the third term. I would like it to work both on string and numerical, and sometimes the second or third argument might be a line feed, as would be manually entered with alt-enter. You'd be my hero of the month if you could do that. I tried but failed miserably. Thank you.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations