Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9300871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:53:13+00:00 2026-06-18T22:53:13+00:00

I’m running a dataimport macro, and I want to merge all rows in a

  • 0

I’m running a dataimport macro, and I want to merge all rows in a dataset that have equal values in column x, and then I want to get a row that represents the average of group x[y] x being the column, and y being the value of the column x for that particular grouping.

Is there a simple function to do this, or must I create an extensive loop with a lot of spare cycles?

Explicitation:
So my dataset looks like

 A    |    B     |    C
 1         2          4
 1         2          5
 2         7          3
 2         5          1
 3         2          1
 1         5          6  

Now I want to merge rows by column A value, so all A’s with equal value get the rest of their rows averaged so I would get somethin that looked like:

 A    |    B     |    C
 1         3          5
 2         6          2
 3         2          1

So far I’ve been trying to loop over the possible values of column A (1 to 10) manually by this function, but it keeps crashing excel, and I can’t figure out why, I must have an endless loop somewhere in this function:

Function MergeRows(sheet, column, value)
Dim LastRow
Dim LastCol
Dim numRowsMerged
Dim totalValue

numRowsMerged = 1
LastRow = sheet.UsedRange.Rows.Count
LastCol = sheet.UsedRange.Columns.Count
With Application.WorksheetFunction
    For iRow = LastRow - 1 To 1 Step -1
        'enter loop if the cell value matches what we're trying to merge
        Do While Cells(iRow, column) = value
               For iCol = 1 To LastCol
                'skip the column that we're going to use as merging value, and skip the column if it contains 3 (ikke relevant)
                If Not (iCol = column) And Not (Cells(iRow, iCol) = 3) Then
                    Cells(iRow, iCol) = (Cells(iRow, iCol) * numRowsMerged + Cells(iRow + 1, iCol)) / (numRowsMerged + 1)
                End If
               Next iCol
            'delete the row merged
            Rows(iRow + 1).Delete
        Loop

        'add one to the total number of rows merged
        numRowsMerged = numRowsMerged + 1
    Next iRow
End With

End Function

solution
I ended up creating a range that I would gradually extend using Union, like this:

  Function GetRowRange(sheet, column, value) As range
Dim LastRow
Dim LastCol
Dim numRowsMerged
Dim totalValue
Dim rowRng As range
Dim tempRng As range
Dim sheetRange As range

numRowsMerged = 1
Set sheetRange = sheet.UsedRange
LastRow = sheet.UsedRange.Rows.Count
LastCol = sheet.UsedRange.Columns.Count
With Application.WorksheetFunction
    For iRow = 1 To LastRow Step 1
        'enter loop if the cell value matches what we're trying to merge
        If (sheetRange.Cells(iRow, column) = value) Then
            Set tempRng = range(sheetRange.Cells(iRow, 1), sheetRange.Cells(iRow, LastCol))
            If (rowRng Is Nothing) Then
                Set rowRng = tempRng
            Else
                Set rowRng = Union(rowRng, tempRng)
            End If
        End If

        'add one to the total number of rows merged
        numRowsMerged = numRowsMerged + 1
    Next iRow
End With
Set GetRowRange = rowRng
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:53:14+00:00Added an answer on June 18, 2026 at 10:53 pm

    Is this what you are trying? Since you wanted VBA code, I have not used Pivots but used a simpler option; formulas to calculate your average.

    Option Explicit
    
    Sub Sample()
        Dim col As New Collection
        Dim wsI As Worksheet, wsO As Worksheet
        Dim wsIlRow As Long, wsOlRow As Long, r As Long, i As Long
        Dim itm
    
        '~~> Chnage this to the relevant sheets
        Set wsI = ThisWorkbook.Sheets("Sheet1")
        Set wsO = ThisWorkbook.Sheets("Sheet2")
    
        '~~> Work with the input sheet
        With wsI
            wsIlRow = .Range("A" & .Rows.Count).End(xlUp).Row
            '~~> get unique values from Col A
            For i = 1 To wsIlRow
                On Error Resume Next
                col.Add .Range("A" & i).Value, """" & .Range("A" & i).Value & """"
                On Error GoTo 0
            Next i
        End With
    
        r = 1
    
        '~~> Write unique values to Col A
        With wsO
            For Each itm In col
                .Cells(r, 1).Value = itm
                r = r + 1
            Next
    
            wsOlRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
            '~~> Use a simple formula to find the average
            For i = 1 To wsOlRow
                .Range("B" & i).Value = Application.Evaluate("=AVERAGE(IF(" & wsI.Name & _
                                        "!A1:A" & wsIlRow & "=" & .Range("A" & i).Value & _
                                        "," & wsI.Name & "!B1:B" & wsIlRow & "))")
    
                .Range("C" & i).Value = Application.Evaluate("=AVERAGE(IF(" & wsI.Name & _
                                        "!A1:A" & wsIlRow & "=" & .Range("A" & i).Value & _
                                        "," & wsI.Name & "!C1:C" & wsIlRow & "))")
            Next i
        End With
    End Sub
    

    SCREENSHOT

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.