In Need of a Comparing and Moving Macro

mk1502

New Member
Joined
May 27, 2015
Messages
2
Does anyone now of a macro for comparing rows.........if the first column from the first row matches the first column in the second row then return the entire first row and the value from the end of the second row (placing it on the end of the returned first row). If the first column in the first row does not match the first column from the second row just return the first row. I need this to go through about 2500 rows and move them to a new sheet.

Example:

Account # LName FName O/Amount
11111 Dylan Bob 100.00
11111 Dylan Bob 100.00
22222 Dean James 200.00
22222 Dean James 50.00
33333 Presley Elvis 300.00
44444 Tyler Steven 400.00

Return:

Account# LName FName O/Amount U/Amount
11111 Dylan Bob 100.00 100.00
22222 Dean James 200.00 50.00
33333 Presley Elvis 300.00
44444 Tyler Steven 400.00
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
mk1502,

Welcome to the MrExcel forum.

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?


Here is a macro solution, and, function, for you to consider, based on your posted raw data where the Account #'s are grouped/sorted per your example.

You can change the raw data worksheet name in the macro.

Sample raw data:


Excel 2007
ABCD
1Account #LNameFNameO/Amount
211111DylanBob100
311111DylanBob100
422222DeanJames200
522222DeanJames50
633333PresleyElvis300
744444TylerSteven400
8
Sheet1


After the macro (using one array in memory) in a new worksheet Results:


Excel 2007
ABCDE
1Account #LNameFNameO/AmountU/Amount
211111DylanBob100100
322222DeanJames20050
433333PresleyElvis300
544444TylerSteven400
6
Results


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below macro code, and, function
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgData()
' hiker95, 05/27/2015, ME857532
Dim w1 As Worksheet, wr As Worksheet
Dim lr As Long, r As Long, nlr As Long, n As Long
Dim o As Variant, j As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
With w1
  lr = .Cells(.Rows.Count, 1).End(xlUp).Row
  nlr = CountUnique(.Range("A1:A" & lr))
  ReDim o(1 To nlr, 1 To 5)
  j = j + 1: o(j, 1) = "Account #": o(j, 2) = "LName"
  o(j, 3) = "FName": o(j, 4) = "O/Amount": o(j, 5) = "U/Amount"
  For r = 2 To lr
    n = Application.CountIf(w1.Columns(1), w1.Cells(r, 1).Value)
    If n = 1 Then
      j = j + 1: o(j, 1) = .Cells(r, 1).Value: o(j, 2) = .Cells(r, 2).Value
      o(j, 3) = .Cells(r, 3).Value: o(j, 4) = .Cells(r, 4).Value
    ElseIf n > 1 Then
      j = j + 1: o(j, 1) = .Cells(r, 1).Value: o(j, 2) = .Cells(r, 2).Value
      o(j, 3) = .Cells(r, 3).Value: o(j, 4) = .Cells(r, 4).Value
      o(j, 5) = .Cells(r + n - 1, 4).Value
    End If
    r = r + n - 1
  Next r
End With
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Sheets("Results")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns("A:E").AutoFit
End With
End Sub
Function CountUnique(ByVal Rng As Range) As Long
' Juan Pablo González, MrExcel MVP, 05/09/2003
' http://www.mrexcel.com/forum/excel-questions/48385-need-count-unique-items-column-visual-basic-applications.html
Dim St As String
Set Rng = Intersect(Rng, Rng.Parent.UsedRange)
St = "'" & Rng.Parent.Name & "'!" & Rng.Address(False, False)
CountUnique = Evaluate("SUM(IF(LEN(" & St & "),1/COUNTIF(" & St & "," & St & ")))")
End Function

Before you use the macro, and, function, with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgData macro.
 
Last edited:
Upvote 0
mk1502,

Sorry, the number formats are not correct in my previous reply.

Here is an updated macro solution, and, function, for you to consider, based on your posted raw data where the Account #'s are grouped/sorted per your example.

You can change the raw data worksheet name in the macro.

Sample raw data:


Excel 2007
ABCD
1Account #LNameFNameO/Amount
211111DylanBob100.00
311111DylanBob100.00
422222DeanJames200.00
522222DeanJames50.00
633333PresleyElvis300.00
744444TylerSteven400.00
8
Sheet1


After the macro (using one array in memory) in a new worksheet Results:


Excel 2007
ABCDE
1Account #LNameFNameO/AmountU/Amount
211111DylanBob100.00100.00
322222DeanJames200.0050.00
433333PresleyElvis300.00
544444TylerSteven400.00
6
Results


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below macro code, and, function
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgDataV2()
' hiker95, 05/27/2015, ME857532
Dim w1 As Worksheet, wr As Worksheet
Dim lr As Long, r As Long, nlr As Long, n As Long
Dim o As Variant, j As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
With w1
  lr = .Cells(.Rows.Count, 1).End(xlUp).Row
  nlr = CountUnique(.Range("A1:A" & lr))
  ReDim o(1 To nlr, 1 To 5)
  j = j + 1: o(j, 1) = "Account #": o(j, 2) = "LName"
  o(j, 3) = "FName": o(j, 4) = "O/Amount": o(j, 5) = "U/Amount"
  For r = 2 To lr
    n = Application.CountIf(w1.Columns(1), w1.Cells(r, 1).Value)
    If n = 1 Then
      j = j + 1: o(j, 1) = .Cells(r, 1).Value: o(j, 2) = .Cells(r, 2).Value
      o(j, 3) = .Cells(r, 3).Value: o(j, 4) = .Cells(r, 4).Value
    ElseIf n > 1 Then
      j = j + 1: o(j, 1) = .Cells(r, 1).Value: o(j, 2) = .Cells(r, 2).Value
      o(j, 3) = .Cells(r, 3).Value: o(j, 4) = .Cells(r, 4).Value
      o(j, 5) = .Cells(r + n - 1, 4).Value
    End If
    r = r + n - 1
  Next r
End With
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Sheets("Results")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Range("D2:E" & UBound(o, 1)).NumberFormat = "#,##0.00"
  .Columns("A:E").AutoFit
End With
End Sub
Function CountUnique(ByVal Rng As Range) As Long
' Juan Pablo González, MrExcel MVP, 05/09/2003
' http://www.mrexcel.com/forum/excel-questions/48385-need-count-unique-items-column-visual-basic-applications.html
Dim St As String
Set Rng = Intersect(Rng, Rng.Parent.UsedRange)
St = "'" & Rng.Parent.Name & "'!" & Rng.Address(False, False)
CountUnique = Evaluate("SUM(IF(LEN(" & St & "),1/COUNTIF(" & St & "," & St & ")))")
End Function

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgDataV2 macro.
 
Upvote 0

Forum statistics

Threads
1,203,198
Messages
6,054,065
Members
444,700
Latest member
Support required please

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