You are welcome. I have to apologize to you though... I meant to put the
worksheet names in section you fill in so you wouldn't have had to search
for them in the actual code. In case you (or other readers of this thread)
might find it useful, here is that code (with it, you only have to change
values in the marked-off section and nowhere else) ...
Sub ConsolidateData()
Dim CurrentID As String
Dim LastCellInRow As Long
Dim DataHeaderRow As Long
Dim ReportHeaderRow As Long
' *********************************************
' * Set your worksheet values in this section *
' *********************************************
Const DataSheetName = "Data"
Const DataIDCol = "A"
Const DataCountryCol = "B"
Const DataNumberCol = "C"
DataHeaderRow = 1 '0 if no header
Const ReportSheetName = "Report"
Const ReportIDCol = "A"
Const ReportRelatedMattersCol = "B"
ReportHeaderRow = 1 '0 if no header
' *********************************************
With Worksheets(DataSheetName)
CurrentID = .Cells(2, DataIDCol).Value
LastCellInRow = .Cells(.Rows.Count, DataIDCol).End(xlUp).Row
Do While DataHeaderRow <= LastCellInRow
DataHeaderRow = DataHeaderRow + 1
If .Cells(DataHeaderRow, DataIDCol).Value <> "" Then
ReportHeaderRow = ReportHeaderRow + 1
CurrentID = .Cells(DataHeaderRow, DataIDCol).Value
Worksheets(ReportSheetName).Cells(ReportHeaderRow, _
ReportIDCol).Value = CurrentID
Worksheets(ReportSheetName).Cells(ReportHeaderRow, _
ReportRelatedMattersCol).Value = _
.Cells(DataHeaderRow, DataCountryCol).Value & " " & _
.Cells(DataHeaderRow, DataNumberCol).Value
Else
Worksheets(ReportSheetName).Cells(ReportHeaderRow, _
ReportRelatedMattersCol).Value = _
Worksheets(ReportSheetName).Cells(ReportHeaderRow, _
ReportRelatedMattersCol).Value & "; " & _
.Cells(DataHeaderRow, DataCountryCol).Value & " " & _
.Cells(DataHeaderRow, DataNumberCol).Value
End If
If DataHeaderRow = LastCellInRow Then Exit Do
Loop
End With
End Sub
Rick
Dawn said:
Oh, that is a thing of great beauty Rick, thank you very much, works
perfectly!
Rick Rothstein (MVP - VB) said:
I hope my terminology doesn't confuse you (it made it easier for me to
visualize), but I refer to your existing data (the data you get from your
"report" CSV file consisting of the 3 column ID, Country and Number) as
"Data" and the resulting output as "Report". In the code below, simply
set
the appropriate values for your worksheets in the section I have marked
off
and then run the macro...
Sub ConsolidateData()
Dim CurrentID As String
Dim LastCellInRow As Long
Dim DataHeaderRow As Long
Dim ReportHeaderRow As Long
' *********************************************
' * Set your worksheet values in this section *
' *********************************************
Const DataIDCol = "A"
Const DataCountryCol = "B"
Const DataNumberCol = "C"
DataHeaderRow = 1 '0 if no header
Const ReportIDCol = "A"
Const ReportRelatedMattersCol = "B"
ReportHeaderRow = 1 '0 if no header
' *********************************************
With Worksheets("Data")
CurrentID = .Cells(2, DataIDCol).Value
LastCellInRow = .Cells(.Rows.Count, DataIDCol).End(xlUp).Row
Do While DataHeaderRow <= LastCellInRow
DataHeaderRow = DataHeaderRow + 1
If .Cells(DataHeaderRow, DataIDCol).Value <> "" Then
ReportHeaderRow = ReportHeaderRow + 1
CurrentID = .Cells(DataHeaderRow, DataIDCol).Value
Worksheets("Report").Cells(ReportHeaderRow, _
ReportIDCol).Value = CurrentID
Worksheets("Report").Cells(ReportHeaderRow, _
ReportRelatedMattersCol).Value = _
.Cells(DataHeaderRow, DataCountryCol).Value & " " & _
.Cells(DataHeaderRow, DataNumberCol).Value
Else
Worksheets("Report").Cells(ReportHeaderRow, _
ReportRelatedMattersCol).Value = _
Worksheets("Report").Cells(ReportHeaderRow, _
ReportRelatedMattersCol).Value & "; " & _
.Cells(DataHeaderRow, DataCountryCol).Value & " " & _
.Cells(DataHeaderRow, DataNumberCol).Value
End If
If DataHeaderRow = LastCellInRow Then Exit Do
Loop
End With
End Sub
Rick
I have a report that returns multiple rows per ID number. The columns
always
stay the same. The number of rows per ID number is variable. I would
like
to group the info per ID number into one cell. If the number of rows
was
constant I could concatenate easily. But each ID number could have one
row
or a hundred rows, it is completely variable. Can VBA be used to
concatenate?
The way the report currently looks, it gives the ID number in the first
row,
then each subsequent row has the ID number blank until there is a new
ID
number to show. This report comes out of a database.
Sample data:
ID Country Number
1234-5678 US 123456
[blank] GB 789123
[blank] IT 456789
4567-8912 US 23456
8912-3456 EP 1234567
[blank] US 8912345
[blank] JP 456789
[blank] CN 123456789
2345-6789 [blank]
So in this example what I want to end up with is two columns, on row
per
ID
number:
ID Related Matters (one cell)
1234-5678 US 123456; GB 789123; IT 456789
4567-8912 US 23456
8912-3456 EP 1234567; US 8912345; JP 456789; CN 123456789
2345-6789 [blank]
Thanks in advance for any help/ideas anyone can give!