?
Solved

VBA : Aggregating Excel cells whose rows contain certain text

Posted on 2004-11-03
2
Medium Priority
?
296 Views
Last Modified: 2010-04-17
In VBA, I want to write a Macro that aggregates the values of a column depending on the text values of another column.
For eaxmple, I have data like this:


Name   Salary
RON         5
TOM         6
JO            4
RON          3
RON         2
JO           10

I want to write something in VBA where I take this date and run a macro to generate:

Name      Salary
RON       10
TOM        6
JO           14

In reality, the raw file contains about 800 lines of data and the cells will not always be in the same place.

Any ideas?
0
Comment
Question by:ronanm1
2 Comments
 
LVL 6

Accepted Solution

by:
david_barker earned 600 total points
ID: 12485296
Assuming data is in the first 2 columns, and that the result should go to the third and fourth column :

Public Sub Salary()
  Dim aNames(1 To 100) As String
  Dim aSalary(1 To 100) As Integer
  Dim iNext  As Integer
  Dim bFound As Boolean
 
  iNext = 1
  For iRow = 1 To 800
    sName = ActiveSheet.Cells(iRow, 1)
    If sName = "" Then Exit For
    iSalary = ActiveSheet.Cells(iRow, 2)
    bFound = False
    For iLoop = 1 To iNext
      If aNames(iLoop) = sName Then
        aSalary(iLoop) = aSalary(iLoop) + iSalary
        bFound = True
        Exit For
      End If
    Next iLoop
    If Not bFound Then
      aNames(iNext) = sName
      aSalary(iNext) = iSalary
      iNext = iNext + 1
    End If
  Next iRow
 
  For iRow = 1 To iNext - 1
    ActiveSheet.Cells(iRow, 3) = aNames(iRow)
    ActiveSheet.Cells(iRow, 4) = aSalary(iRow)
  Next iRow
 
End Sub
0
 
LVL 11

Expert Comment

by:huntersvcs
ID: 12486833
This might be easier:

ActiveCell.FormulaR1C1 = "=SUMIF(R[-8]C[1]:R[-2]C[1],""Ron."",R[-8]C:R[-2]C)"
ActiveCell.FormulaR1C1 = "=SUMIF(R[-8]C[1]:R[-2]C[1],""Tom."",R[-8]C:R[-2]C)"
ActiveCell.FormulaR1C1 = "=SUMIF(R[-8]C[1]:R[-2]C[1],""Jo."",R[-8]C:R[-2]C)"

You just have to set the variables for the rows and columns, but that's easy!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question