Joining similar rows macro

J

John Wright

I get an excel sheet from our accounting department that I import into a
database for reporting. The data is straight forward in most cases, but
today I noticed that there are a lot of rows that are duplicated except for
two columns. Is there a macro or a way to run a script that would look at
these rows and compare them and if all the columns in the row match except
for these two, combine the the columns (these are number columns so I would
like to add the numbers) and create a single row? If so this would really
help me get the reports they need. Any help is appreciated. Here is an
example of the rows I would like combined

1-Jul-07 FIRSTENERGY 605894 0 Region 21 Fuel DLGQ 32334 FA_17RFA
602381 MPFIR US MULTIPLE PLANTS FIRSTENERGY 17x17STD ENG 6028 0 ENGSVCS
Engineering Services US US - -
1-Jul-07 FIRSTENERGY 605894 0 Region 21 Fuel DLGQ 32334 FA_17RFA
602381 MPFIR US MULTIPLE PLANTS FIRSTENERGY 17x17STD ENG 0 61.68 ENGSVCS
Engineering Services US US - -
1-Jul-07 FIRSTENERGY 605894 0 Region 21 Fuel DLGQ 32334 FA_17RFA
602381 MPFIR US MULTIPLE PLANTS FIRSTENERGY 17x17STD ENG 0 4210 ENGSVCS
Engineering Services US US - -


This is what I would like to see

1-Jul-07 FIRSTENERGY 605894 0 Region 21 Fuel DLGQ 32334 FA_17RFA
602381 MPFIR US MULTIPLE PLANTS FIRSTENERGY 17x17STD ENG 6028 4271.68
ENGSVCS Engineering Services US US - -



Thanks.

John
 
G

Guest

It is hard to tell what columns the data is from your posting. Modify the
three constants to match you worksheet. this should work

Sub combinerows()

Const LastCol = "W"
Const CombineCol1 = "R"
Const CombineCol2 = "S"

RowCount = 1
Do While Cells(RowCount + 1, "A") <> ""
Match = True
For ColCount = 1 To Cells(1, "W").Column
If (ColCount <> Cells(1, CombineCol1).Column) And _
(ColCount <> Cells(1, CombineCol2).Column) Then

If Cells(RowCount, ColCount).Value <> _
Cells(RowCount + 1, ColCount).Value Then

Match = False
Exit For
End If

End If
Next ColCount

If Match = True Then
Cells(RowCount, CombineCol1) = _
Cells(RowCount, CombineCol1) + _
Cells(RowCount + 1, CombineCol1)
Cells(RowCount, CombineCol2) = _
Cells(RowCount, CombineCol2) + _
Cells(RowCount + 1, CombineCol2)
Rows(RowCount + 1).Delete
Else
RowCount = RowCount + 1
End If
Loop


End Sub
 
J

John Wright

Sorry about the confusion let me clear it up. If I have the following rows of data (made a comma seperated string):

1-Jul-07, FIRSTENERGY, 605894, 0, Region 21, Fuel, DLGQ 32334, FA_17RFA,602381, MPFIR US MULTIPLE PLANTS, FIRSTENERGY,17x17,STD, ENG, 6028, 0, ENGSVCS,Engineering Services US, US,-,-

1-Jul-07, FIRSTENERGY, 605894, 0, Region 21, Fuel, DLGQ 32334, FA_17RFA,602381, MPFIR US MULTIPLE PLANTS, FIRSTENERGY,17x17,STD, ENG, 0,61.68, ENGSVCS,Engineering Services US, US,-,-

1-Jul-07, FIRSTENERGY, 605894, 0, Region 21, Fuel, DLGQ 32334, FA_17RFA,602381, MPFIR US MULTIPLE PLANTS, FIRSTENERGY,17x17,STD, ENG, 0,4271.68, ENGSVCS,Engineering Services US, US,-,-

I want to get the following:

1-Jul-07, FIRSTENERGY, 605894, 0, Region 21, Fuel, DLGQ 32334, FA_17RFA,602381, MPFIR US MULTIPLE PLANTS, FIRSTENERGY,17x17,STD, ENG, 6028, 4271.68, ENGSVCS,Engineering Services US, US,-,-


So I want to check 17 columns for a match. If all 17 columns match, then I want to combine the rows and add the numberic values (see the bold items). I am not an excel guru so how would I run the script below for the page to get a new scrubbed worksheet?

John
 
G

Guest

there seems to be a prblem still with the number of columns. I belive your
comma seperate datta has 21 columns, not 17. Doesn't matter the code is
generic. Just change the statements below to fix the problems.

Chage from
Const LastCol = "W"
Const CombineCol1 = "R"
Const CombineCol2 = "S"


to
Const LastCol = "Q"
Const CombineCol1 = "N"
Const CombineCol2 = "O"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Merge Rows of like data 1

Top