VBA Pindah Kopi Copy Kolom B ke Kolom C Jika Kolom C Kosong - habibmarzuqi/Atom GitHub Wiki

Sub PindahKolomBkeCJikaCKosong() Dim ws As Worksheet Dim LastRow As Long Dim i As Long

' Menentukan worksheet yang akan dioperasikan
Set ws = ThisWorkbook.Sheets("metadata3")

' Menentukan baris terakhir dengan data di kolom B
LastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

' Memindahkan isi kolom B ke kolom C jika kolom C kosong
For i = 2 To LastRow
    If IsEmpty(ws.Cells(i, "C")) Then
        ws.Cells(i, "C").Value = ws.Cells(i, "B").Value
        ' ws.Cells(i, "B").Value = "" ' Kosongkan isi kolom B
    End If
Next i

Untuk menginsert kolom yang

Sub InsertBarisDanPindahkanKolomB_keD() Dim ws As Worksheet Dim i As Long Dim lastRow As Long

Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

' Kita mulai dari bawah ke atas agar insert tidak mengacaukan urutan
For i = lastRow To 3 Step -1
    If Trim(ws.Cells(i, "B").Value) <> "" Then
        ' Sisip satu baris penuh di baris i
        ws.Rows(i).Insert Shift:=xlDown

        ' Pindahkan isi dari kolom B (baris di bawah) ke kolom D (baris baru)
        ws.Cells(i, "D").Value = ws.Cells(i + 1, "B").Value

        ' (Opsional) Hapus isi di kolom B baris bawah
        ws.Cells(i + 1, "B").ClearContents
    End If
Next i

MsgBox "Selesai! Baris berhasil disisipkan dan isi kolom B dipindahkan ke D."

End Sub

End Sub

Sub InsertUntukPerubahanKolomC() Dim ws As Worksheet Dim i As Long Dim lastRow As Long

Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row

' Mulai dari bawah agar struktur tidak terganggu
For i = lastRow To 4 Step -1
    ' Jika nilai kolom C tidak sama dengan atasnya
    If ws.Cells(i, "C").Value <> ws.Cells(i - 1, "C").Value Then
        ' Sisipkan baris baru sebelum baris i
        ws.Rows(i).Insert Shift:=xlDown
        
        ' Masukkan nilai dari C(i) ke D(i), yaitu di baris baru
        ws.Cells(i, "D").Value = ws.Cells(i + 1, "C").Value
    End If
Next i

MsgBox "Selesai! Baris baru ditambahkan untuk perubahan nilai di kolom C."

End Sub