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

Excelですべての空白行またはセルの最初の行のみを削除するにはどうすればよいですか?

複数行がセル内の空白と混ざっているワークブックを受け取る場合があります。 セル内のこれらの空白行を削除するにはどうすればよいですか? そして、最初の行だけを削除するのはどうですか? この記事は、ステップバイステップで対処するのに役立ちます。

VBAコードを使用してセル内のすべての空白行を削除する
VBAコードを含むセルの最初の行のみを削除します


VBAコードを使用してセル内のすべての空白行を削除する

以下のスクリーンショットに示すように、セル内のすべての空白行を削除するには、次のVBAコードを実行して実行します。

1。 プレス 他の + F11 キーボードのキーを押すと、 アプリケーション向け Microsoft Visual Basic 窓。

2。 の中に アプリケーション向け Microsoft Visual Basic ウィンドウ、クリック インセット > モジュール。 次に、以下のVBAコードをコピーしてに貼り付けます モジュール 窓。

VBAコード:セル内のすべての空白行を削除します

Sub DoubleReturn()
Dim xRng As Range, xCell As Range
Dim I As Integer
On Error Resume Next
Set xRng = Application.InputBox("Please select range:", "Kutools for Excel", Selection.Address, , , , , 8)
If xRng Is Nothing Then Exit Sub
On Error Resume Next
For Each xCell In xRng
  For I = 1 To Len(xCell) - Len(WorksheetFunction.Substitute(xCell, Chr(10), ""))
   xCell = Replace(xCell, Chr(10) + Chr(10), Chr(10))
  Next
Next
End Sub

3。 プレス F5 コードを実行するためのキー。 次に、削除する空白行のあるセルを選択します。 Kutools for Excel ダイアログボックス。 そして最後に OK

次に、以下のスクリーンショットに示すように、指定したセルからすべての空白行が削除されていることがわかります。 テキストはまだ別の行にあります。


VBAコードを含むセルの最初の行のみを削除します

以下のスクリーンショットに示すように、セルA2とA3の最初の行を削除するには、次のVBAコードが役立ちます。

1。 プレス 他の + F11 キーを押して アプリケーション向け Microsoft Visual Basic 窓。

2。 の中に アプリケーション向け Microsoft Visual Basic ウィンドウ、クリック インセット > モジュール。 次に、以下のVBAコードをコピーしてに貼り付けます モジュール 窓。

VBAコード:セルの最初の行のみを削除します

Option Explicit
Sub RemoveFirstLine(ByRef Target As Range)
    Dim xCell As Range
    For Each xCell In Target.Cells
        xCell.Value = Right(xCell.Value, Len(xCell.Value) - InStr(1, xCell.Value, vbLf))
    Next
End Sub

Sub StartRemove()
Dim xRng As Range
   On Error Resume Next
   Set xRng = Application.InputBox("Please select range:", "Kutools for Excel", Selection.Address, , , , , 8)
   If xRng Is Nothing Then Exit Sub
   On Error Resume Next
   RemoveFirstLine xRng
End Sub

3。 プレス F5 コードを実行するためのキー。 ポップアップで Kutools for Excel ダイアログボックスで、最初の行だけを削除する必要があるセルを選択してから、 OK

次に、以下のスクリーンショットに示すように、指定したセルからすべての最初の行が削除されていることがわかります。


関連記事:

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

🤖 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 (8)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
This Works it removed my all lines but the only issue is it is taking so much time to run for a single cell and i have big data with me to do it and i think it is gonna take tooo much time for that
This comment was minimized by the moderator on the site
Hi Tanveer Khan,
The code runs at normal speed on my side. Can you tell me which Excel version are you using?
This comment was minimized by the moderator on the site
This is my first VBA, I have only got up to your first step and it has solved my problem, so far. I have printed your instructions for future reference. So far I am wrapt. Fabulous instruction too, thank you for your quality skills and detailing them.
Rated 5 out of 5
This comment was minimized by the moderator on the site
Jeg forstår desværre ikke et kvæk af ovenstående. Findes der ikke en nem og ligetil måde at fjerne tomme linier i Exell? Helst uden koder og andet mystisk!

Med venlig hilsen
Lene
This comment was minimized by the moderator on the site
Hello,
If you don't want to keep the original texts in different lines in cells, you can apply the following methods to get it done.
1. Use the Find & Replace feature:
Press the Ctrl + H keys. In the opening Find and Replace dialog box, click to activate the Find what box. Hold down the Alt key while typing 010 on the numeric keypad. Keep the Replace with box empty, click the Replace All button. But all the texts is concentrated on the same line.
2. Use the following formula in a helper column:
=SUBSTITUTE(A1, CONCATENATE(CHAR(13),CHAR(10),CHAR(13),CHAR(10)),CONCATENATE(CHAR(13),CHAR(10)))
where A1 is the cell you want to process.
This comment was minimized by the moderator on the site
Do you know how to adapt this to remove the last line instead of the first line, please? TIA
This comment was minimized by the moderator on the site
Hi,
this works awesome. How can I adjust the code to remove only the lines, not starting with numeric characters?

Thanks and best,
Jack
This comment was minimized by the moderator on the site
Hi Jack,
Sorry can't help you with that yet. Thank you for your comment.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations