VBA macros that can move up data on different rows based on column A

lihong3210

New Member
Joined
Dec 7, 2023
Messages
2
Office Version
  1. 2011
Platform
  1. Windows
Hi all,

Calling for help and hope VBA or macros can make my life easier. The below screenshot is the current file and I'm trying to eliminate as much duplicate rows on column A as possible. The file is showing mapping of local accounts (column C to I) and how it mapped to consolidated group accounts (column A & B). Is there a way to move E173 to E172 and F174 to F172 to get rid of row 173 and row 174? The file has over 3000 rows and I just want to make them automated instead doing one by one.

Greatly appreciate if someone can help!

1701968425468.png
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
if all duplicate values in column A always continues then this will work:
VBA Code:
Sub MoveRows()
    Dim cll As Range, rng As Range
    Dim lr As Long
    Dim xcll As Range
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    If lr < 2 Then Exit Sub
    Set rng = Range("A2:A" & lr)
    For Each cll In rng
        If Not IsEmpty(cll) Then
            If xcll Is Nothing Then Set xcll = cll
            If cll.Value = xcll.Value Then
                If Not IsEmpty(cll.Offset(, 4)) Then
                    xcll.Offset(, 4).Value = cll.Offset(, 4).Value
                    cll.Offset(, 4).Value = Empty
                End If
                If Not IsEmpty(cll.Offset(, 5)) Then
                    xcll.Offset(, 5).Value = cll.Offset(, 5).Value
                    cll.Offset(, 5).Value = Empty
                End If
            Else
                Set xcll = cll
            End If
        End If
    Next cll
End Sub
 
Upvote 0
if all duplicate values in column A always continues then this will work:
VBA Code:
Sub MoveRows()
    Dim cll As Range, rng As Range
    Dim lr As Long
    Dim xcll As Range
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    If lr < 2 Then Exit Sub
    Set rng = Range("A2:A" & lr)
    For Each cll In rng
        If Not IsEmpty(cll) Then
            If xcll Is Nothing Then Set xcll = cll
            If cll.Value = xcll.Value Then
                If Not IsEmpty(cll.Offset(, 4)) Then
                    xcll.Offset(, 4).Value = cll.Offset(, 4).Value
                    cll.Offset(, 4).Value = Empty
                End If
                If Not IsEmpty(cll.Offset(, 5)) Then
                    xcll.Offset(, 5).Value = cll.Offset(, 5).Value
                    cll.Offset(, 5).Value = Empty
                End If
            Else
                Set xcll = cll
            End If
        End If
    Next cll
End Sub
Hi,

Thank you so much for helping me on this. The code works, however it only works from Column "C" to "F" and not for column "G to I", how do I adjust the code so that it also works for col "G" to "I"? In addition, would it be possible to delete the duplicate account if there are no values from column "C" to "I". For example, row#5 shown in the screenshot should be deleted as there are no values/input in col "C" through "I".

Thanks again for your help on this!

1702304865130.png
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top