Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's one way.</p> <pre><code>Option Explicit Sub Test() Dim sheetNames: sheetNames = Array("Sheet1", "Sheet2", "Sheet3") Dim target As Worksheet: Set target = Worksheets("Sheet4") Dim accounts As New Dictionary Dim balances As New Dictionary Dim source As Range Dim row As Range Dim id As String Dim account As String Dim balance As Double Dim sheetName: For Each sheetName In sheetNames Set source = Worksheets(sheetName).Range("A1").CurrentRegion Set source = source.Offset(1, 0).Resize(source.Rows.Count - 1, source.Columns.Count) For Each row In source.Rows id = row.Cells(1).Value account = row.Cells(2).Value balance = row.Cells(3).Value accounts(id) = account If balances.Exists(id) Then balances(id) = balances(id) + balance Else balances(id) = balance End If Next row Next sheetName Call target.Range("A2:A65536").EntireRow.Delete Dim rowIndex As Long: rowIndex = 1 Dim key For Each key In accounts.Keys rowIndex = rowIndex + 1 target.Cells(rowIndex, 1).Value = key target.Cells(rowIndex, 2).Value = accounts(key) target.Cells(rowIndex, 3).Value = balances(key) Next key End Sub </code></pre> <ol> <li><p>Create a new module (VBA editor -> Insert -> Module) and paste the above code into it.</p></li> <li><p>Add a reference to Microsoft Scripting Runtime (VBA editor -> Tools -> References -> Check 'Microsoft Scripting Runtime').</p></li> <li><p>Run it by placing the cursor within the code and pressing F5.</p></li> </ol> <p>Obviously the sheets will have to be named Sheet1, Sheet2, Sheet3 and Sheet4. It won't paste the column headers into Sheet4 but presumably they are static so you can just set them up yourself beforehand.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload