Cookies membantu kami menyampaikan perkhidmatan kami. Dengan menggunakan perkhidmatan kami, anda bersetuju dengan penggunaan cookies.
Petua: Bahasa lain diterjemahkan Google. Anda boleh melawat English versi pautan ini.
Log masuk
x
or
x
x
Daftar
x

or

Bagaimana untuk mendua baris berdasarkan nilai sel dalam lajur?

Sebagai contoh, saya mempunyai pelbagai data yang mengandungi senarai nombor dalam lajur D, dan sekarang, saya ingin menduplikasi seluruh baris beberapa kali berdasarkan nilai angka dalam lajur D untuk mendapatkan hasil yang berikut. Bagaimanakah saya dapat menyalin baris beberapa kali berdasarkan nilai sel dalam Excel?

baris pendua doc oleh sel 1

Baris duplikat beberapa kali berdasarkan nilai sel dengan kod VBA


anak panah gelembung kanan biru Baris duplikat beberapa kali berdasarkan nilai sel dengan kod VBA

Untuk menyalin dan menduplikasi seluruh baris beberapa kali berdasarkan nilai sel, kod VBA berikut boleh membantu anda, sila lakukan seperti ini:

1. Tahan ALT + F11 kunci untuk membuka Microsoft Visual Basic untuk Aplikasi tingkap.

2. Klik Memasukkan > Modul, dan tampal kod berikut dalam Modul Tetingkap.

Kod VBA: Baris duplikat beberapa kali berdasarkan nilai sel:

Sub CopyData()
'Updateby Extendoffice 20160922
    Dim xRow As Long
    Dim VInSertNum As Variant
    xRow = 1
    Application.ScreenUpdating = False
    Do While (Cells(xRow, "A") <> "")
        VInSertNum = Cells(xRow, "D")
        If ((VInSertNum > 1) And IsNumeric(VInSertNum)) Then
           Range(Cells(xRow, "A"), Cells(xRow, "D")).Copy
           Range(Cells(xRow + 1, "A"), Cells(xRow + VInSertNum - 1, "D")).Select
           Selection.Insert Shift:=xlDown
           xRow = xRow + VInSertNum - 1
        End If
        xRow = xRow + 1
    Loop
    Application.ScreenUpdating = False
End Sub

3. Kemudian tekan F5 kunci untuk menjalankan kod ini, seluruh baris telah diduplikasi beberapa kali berdasarkan nilai sel dalam lajur D yang anda perlukan.

Nota: Dalam kod di atas, surat itu A menunjukkan lajur permulaan julat data anda, dan huruf D adalah huruf lajur yang anda ingin duplikat baris berdasarkan. Sila tukar mereka dengan keperluan anda.



Alat Produktiviti yang Disyorkan

Tab Pejabat

bintang emas1 Bawa tab berguna kepada Excel dan perisian Office lain, seperti Chrome, Firefox dan Internet Explorer baru.

Kutools untuk Excel

bintang emas1 Hebat! Tingkatkan produktiviti anda dalam minit 5. Tidak memerlukan kemahiran khas, kecuali dua jam setiap hari!

bintang emas1 Ciri-ciri Baru 300 untuk Excel, Buat Excel Banyak Mudah dan Berkuasa:

  • Merge Cell / Rows / Columns tanpa Kehilangan Data.
  • Menggabungkan dan Menyusun Lembaran Berbilang dan Buku Kerja.
  • Bandingkan Ranges, Salin Pelbagai Rang Undang-Undang, Tukar Teks hingga Tarikh, Penukaran Unit dan Mata Wang.
  • Count by Colors, Subtotal Paging, Urutkan Advanced dan Super Filter,
  • More Select / Insert / Delete / Text / Format / Link / Comment / Workbooks / Worksheets Tools ...

Skrin skrin Kutools untuk Excel

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    KAS · 1 months ago
    Is there a way to update the module to only duplicate new data? I'm working on an ongoing document and do not want the code to duplicate data that has been previously duplicated.
  • To post as a guest, your comment is unpublished.
    Gerardo Enrique Heras Araujo · 2 months ago
    hi, for me is no working, I want to remove letters and number duplicate is possible?
  • To post as a guest, your comment is unpublished.
    Naomi · 2 months ago
    This script seems to be exactly what I need, however, when I run it I am getting an error on the line Selection.Insert Shift:=x1Down

    Any suggestions on how I fix this?
  • To post as a guest, your comment is unpublished.
    Manuel F · 2 months ago
    Thanks! it has been a great solution for all my troubles!
  • To post as a guest, your comment is unpublished.
    Leah · 3 months ago
    Hello, this worked great. However, I have a report with 1000 entries and the code stopped duplicating around entry 480. Is there something that I can add so that it completes the action on the entire report?
    • To post as a guest, your comment is unpublished.
      skyyang · 3 months ago
      Hello, Leah,
      I have tested the code in 2000 rows, and it works well.
      Could you send your worksheet to me for testing the code?
      My email address is skyyang@extendoffice.com
      Look forward to your reply!
      • To post as a guest, your comment is unpublished.
        Leah · 3 months ago
        Hello! I got it to work. It was an error on my side, the report had a few blank rows that were hidden that were causing the script to stop looping. It worked for my report with 8,000 rows! Thank youQ