Loncat ke daftar isi utama

Bagaimana cara menghapus semua baris kosong atau hanya baris pertama di sel di Excel?

Anda mungkin menerima buku kerja dengan beberapa baris yang bercampur dengan kosong di sel. Bagaimana cara menghapus baris kosong ini di sel? Dan bagaimana jika hanya menghapus baris pertama? Artikel ini akan membantu Anda mengatasinya selangkah demi selangkah.

Hapus semua baris kosong di sel dengan kode VBA
Hapus hanya baris pertama dalam sel dengan kode VBA


Hapus semua baris kosong di sel dengan kode VBA

Seperti yang ditunjukkan pada tangkapan layar di bawah, untuk menghapus semua baris kosong di sel, Anda dapat menjalankan kode VBA berikut untuk menyelesaikannya.

1. tekan lain + F11 tombol pada keyboard Anda, lalu membuka a Microsoft Visual Basic untuk Aplikasi jendela.

2. Dalam Microsoft Visual Basic untuk Aplikasi window, klik Menyisipkan > Modul. Kemudian salin dan tempel kode VBA di bawah ini ke file Modul jendela.

Kode VBA: Hapus semua baris kosong di sel

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. tekan F5 kunci untuk menjalankan kode. Kemudian pilih sel dengan garis kosong yang akan Anda hapus di Kutools untuk Excel kotak dialog. Dan terakhir klik OK .

Kemudian Anda dapat melihat semua baris kosong dihapus dari sel tertentu seperti gambar di bawah ini. Teks-teks tersebut masih terletak di baris yang berbeda.


Hapus hanya baris pertama dalam sel dengan kode VBA

Seperti yang ditunjukkan pada tangkapan layar di bawah, untuk menghapus baris pertama di sel A2 dan A3, kode VBA berikut dapat membantu.

1. tekan lain + F11 kunci untuk membuka Microsoft Visual Basic untuk Aplikasi jendela.

2. Dalam Microsoft Visual Basic untuk Aplikasi window, klik Menyisipkan > Modul. Kemudian salin dan tempel kode VBA di bawah ini ke file Modul jendela.

Kode VBA: Hapus hanya baris pertama dalam sel

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. tekan F5 kunci untuk menjalankan kode. Dalam bermunculan Kutools untuk Excel kotak dialog, pilih sel yang perlu Anda hapus hanya baris pertama, lalu klik OK .

Kemudian Anda dapat melihat semua baris pertama dihapus dari sel tertentu seperti gambar di bawah ini.


Artikel terkait:

Alat Produktivitas Kantor Terbaik

馃 Kutools AI Ajudan: Merevolusi analisis data berdasarkan: Eksekusi Cerdas   |  Hasilkan Kode  |  Buat Rumus Khusus  |  Analisis Data dan Hasilkan Grafik  |  Aktifkan Fungsi Kutools...
Fitur Populer: Temukan, Sorot, atau Identifikasi Duplikat   |  Hapus Baris Kosong   |  Gabungkan Kolom atau Sel tanpa Kehilangan Data   |   Putaran tanpa Formula ...
Pencarian Super: VLookup Beberapa Kriteria    VLookup Nilai Berganda  |   VLookup di Beberapa Lembar   |   Pencarian Fuzzy ....
Daftar Drop-down Lanjutan: Buat Daftar Drop Down dengan Cepat   |  Daftar Drop Down yang Bergantung   |  Multi-pilih Drop Down List ....
Manajer Kolom: Tambahkan Jumlah Kolom Tertentu  |  Pindahkan Kolom  |  Alihkan Status Visibilitas Kolom Tersembunyi  |  Bandingkan Rentang & Kolom ...
Fitur Unggulan: Fokus Kisi   |  Tampilan Desain   |   Bar Formula Besar    Manajer Buku Kerja & Lembar   |  Perpustakaan Sumberdaya (Teks otomatis)   |  Pemetik tanggal   |  Gabungkan Lembar Kerja   |  Enkripsi/Dekripsi Sel    Kirim Email berdasarkan Daftar   |  Filter Super   |   Filter Khusus (filter tebal/miring/coret...) ...
15 Perangkat Teratas12 Teks Tools (Tambahkan Teks, Hapus Karakter, ...)   |   50 + Grafik jenis (Gantt Chart, ...)   |   40+ Praktis Rumus (Hitung usia berdasarkan ulang tahun, ...)   |   19 Insersi Tools (Masukkan Kode QR, Sisipkan Gambar dari Jalur, ...)   |   12 Konversi Tools (Angka ke Kata, Konversi Mata Uang, ...)   |   7 Gabungkan & Pisahkan Tools (Lanjutan Gabungkan Baris, Pisahkan Sel, ...)   |   ... dan banyak lagi

Tingkatkan Keterampilan Excel Anda dengan Kutools for Excel, dan Rasakan Efisiensi yang Belum Pernah Ada Sebelumnya. Kutools for Excel Menawarkan Lebih dari 300 Fitur Lanjutan untuk Meningkatkan Produktivitas dan Menghemat Waktu.  Klik Di Sini untuk Mendapatkan Fitur yang Paling Anda Butuhkan...

Deskripsi Produk


Tab Office Membawa antarmuka Tab ke Office, dan Membuat Pekerjaan Anda Jauh Lebih Mudah

  • Aktifkan pengeditan dan pembacaan tab di Word, Excel, PowerPoint, Publisher, Access, Visio, dan Project.
  • Buka dan buat banyak dokumen di tab baru di jendela yang sama, bukan di jendela baru.
  • Meningkatkan produktivitas Anda sebesar 50%, dan mengurangi ratusan klik mouse untuk Anda setiap hari!
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