Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2015, 11:23 AM
brunssl2 brunssl2 is offline Macro to Combine Multiple Rows into One Row Windows XP Macro to Combine Multiple Rows into One Row Office 2003
Novice
Macro to Combine Multiple Rows into One Row
 
Join Date: Apr 2014
Posts: 9
brunssl2 is on a distinguished road
Default Macro to Combine Multiple Rows into One Row

Hello - I have a scenario of multiple rows of data being created for a single data identifier. The goal is to combine these multiple rows for a single data identifier into one single row. In my example, I have 17 columns of data attributes (Column A thru Q) and 1 column of data identifier (Column R).



In the attached, you'll noticed the greyed examples that I am trying to combine into one row. I'm looking for a macro to be able to export the result into a new tab.

Any help would be immensely appreciated!
Reply With Quote
  #2  
Old 01-10-2015, 04:51 PM
BobBridges's Avatar
BobBridges BobBridges is offline Macro to Combine Multiple Rows into One Row Windows 7 64bit Macro to Combine Multiple Rows into One Row Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Brunssl2, it sounds as though you want a macro to do the following:

1) Create a new target worksheet.
2) Sort the source worksheet by column 18, if it isn't already. (Col Q, I take it?)
3) Go through the source worksheet row by row, collecting the dates for a single event code. When you come to the end of an event code, write out the combined row in the target worksheet and continue.

You're on the right track; this is a perfect use for a VBA program. But what part of the above do you need help with? Have you ever written in VBA before? Where are you starting?
Reply With Quote
  #3  
Old 01-12-2015, 06:49 AM
brunssl2 brunssl2 is offline Macro to Combine Multiple Rows into One Row Windows XP Macro to Combine Multiple Rows into One Row Office 2003
Novice
Macro to Combine Multiple Rows into One Row
 
Join Date: Apr 2014
Posts: 9
brunssl2 is on a distinguished road
Default

Hi BobBridges - that's exactly what I'm looking for (sort by column R - Event Code).

I have no experience writing VBA. When I have language to look at, I'm able to decipher it and adjust to meet any changes, but I've never learned.
Reply With Quote
  #4  
Old 01-12-2015, 08:01 AM
BobBridges's Avatar
BobBridges BobBridges is offline Macro to Combine Multiple Rows into One Row Windows 7 64bit Macro to Combine Multiple Rows into One Row Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Well, are you read to learn?

Here's the thing: If you're looking for someone to just write the program for you, experience suggests that someone here will be willing. But I'm a teach-a-man-to-fish kind of guy, myself; I'll help you learn how to write one for yourself (after which you'll be able to modify it to handle new situations, and write new ones of your own), but not just give you the fish. Which do you prefer? As I said, some folks here don't mind just writing a simple macro for the new guys.
Reply With Quote
  #5  
Old 01-12-2015, 12:26 PM
brunssl2 brunssl2 is offline Macro to Combine Multiple Rows into One Row Windows XP Macro to Combine Multiple Rows into One Row Office 2003
Novice
Macro to Combine Multiple Rows into One Row
 
Join Date: Apr 2014
Posts: 9
brunssl2 is on a distinguished road
Default

I appreciate the desire to teach, but a request like this occurs once or twice a year for me so chances are that I will forget how to do this again in 6-8 months from now anyway .... if you're not willing to compile something, then I can understand that as I'm usually in your camp for teaching a man to fish.
Reply With Quote
  #6  
Old 01-12-2015, 10:04 PM
macropod's Avatar
macropod macropod is offline Macro to Combine Multiple Rows into One Row Windows 7 64bit Macro to Combine Multiple Rows into One Row Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try the following macro:
Code:
Sub Demo()
Dim lRow As Long, lCol As Long, i As Long, j As Long, SBar As Boolean
With Application
  SBar = .DisplayStatusBar
  .DisplayStatusBar = True
  .ScreenUpdating = False
  .Calculation = xlManual
End With
ThisWorkbook.Worksheets("Sheet1").UsedRange.Copy
ThisWorkbook.Worksheets("Sheet2").Paste Destination:=ThisWorkbook.Worksheets("Sheet2").Range("A1")
With ThisWorkbook.Worksheets("Sheet2").UsedRange
  lRow = .Cells.SpecialCells(xlCellTypeLastCell).Row - 1
  lCol = .Cells.SpecialCells(xlCellTypeLastCell).Column
  For i = lRow To 1 Step -1
    Application.StatusBar = "Processing row " & i
    If .Cells(i, lCol).Value = .Cells(i + 1, lCol).Value Then
      For j = 1 To lCol - 1
        If Len(Trim(.Cells(i, j).Value)) > 0 Then
          .Cells(i + 1, j).Value = .Cells(i, j).Text
          Exit For
        End If
      Next
      .Rows(i).EntireRow.Delete
    End If
  Next
End With
With Application
  .Calculation = xlAutomatic
  .StatusBar = False
  .DisplayStatusBar = SBar
  .ScreenUpdating = True
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 01-12-2015 at 10:09 PM. Reason: Omitted a couple of lines
Reply With Quote
  #7  
Old 01-13-2015, 01:00 PM
brunssl2 brunssl2 is offline Macro to Combine Multiple Rows into One Row Windows XP Macro to Combine Multiple Rows into One Row Office 2003
Novice
Macro to Combine Multiple Rows into One Row
 
Join Date: Apr 2014
Posts: 9
brunssl2 is on a distinguished road
Default

I've added this macro into my file and it doesn't seem to do anything when I run it
Reply With Quote
  #8  
Old 01-13-2015, 01:31 PM
charlesdh charlesdh is offline Macro to Combine Multiple Rows into One Row Windows 7 32bit Macro to Combine Multiple Rows into One Row Office 2010 32bit
Expert
 
Join Date: Apr 2014
Location: Mississippi
Posts: 382
charlesdh is on a distinguished road
Default

Hi,

I modified Macopod code see if this works.

Code:
Sub Demo()
Dim lRow As Long, lCol As Long, i As Long, j As Long, SBar As Boolean
With Application
  SBar = .DisplayStatusBar
  .DisplayStatusBar = True
  .ScreenUpdating = False
  .Calculation = xlManual
End With
''' changed to this '''
ThisWorkbook.Worksheets("Sheet1").UsedRange.Copy Destination:=ThisWorkbook.Worksheets("Sheet2").Range("A1")
''''''''''''''''''''''''''''''
With ThisWorkbook.Worksheets("Sheet2").UsedRange
  lRow = .Range("R65536").End(xlUp).Row ''' changed this '''
  lCol = .Cells.SpecialCells(xlCellTypeLastCell).Column
  For i = lRow To 1 Step -1
    Application.StatusBar = "Processing row " & i
    If .Cells(i, lCol).Value = .Cells(i + 1, lCol).Value Then
      For j = 1 To lCol - 1
        If Len(Trim(.Cells(i, j).Value)) > 0 Then
          .Cells(i + 1, j).Value = .Cells(i, j).Text
          Exit For
        End If
      Next
      .Rows(i).EntireRow.Delete
    End If
  Next
End With
With Application
  .Calculation = xlAutomatic
  .StatusBar = False
  .DisplayStatusBar = SBar
  .ScreenUpdating = True
End With
End Sub
Reply With Quote
  #9  
Old 01-13-2015, 01:43 PM
macropod's Avatar
macropod macropod is offline Macro to Combine Multiple Rows into One Row Windows 7 64bit Macro to Combine Multiple Rows into One Row Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The code I posted works fine as is. Do note that I updated it after posting, as I'd initially omitted a couple of lines. If you check Sheet 2 after running the macro, you should see the transformed data there.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 01-13-2015, 01:43 PM
brunssl2 brunssl2 is offline Macro to Combine Multiple Rows into One Row Windows XP Macro to Combine Multiple Rows into One Row Office 2003
Novice
Macro to Combine Multiple Rows into One Row
 
Join Date: Apr 2014
Posts: 9
brunssl2 is on a distinguished road
Default

Worked perfectly! Thank you so much!
Reply With Quote
  #11  
Old 01-13-2015, 01:56 PM
charlesdh charlesdh is offline Macro to Combine Multiple Rows into One Row Windows 7 32bit Macro to Combine Multiple Rows into One Row Office 2010 32bit
Expert
 
Join Date: Apr 2014
Location: Mississippi
Posts: 382
charlesdh is on a distinguished road
Default

macropod,

Unless I missed something it did not work for me.
The data was not copied to sheet2.
Reply With Quote
Reply

Tags
combine rows, macro, multiple rows



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to Combine Multiple Rows into One Row Combine multiple presentations stevevrabel PowerPoint 27 11-23-2014 07:42 AM
Macro to Combine Multiple Rows into One Row Copy Multiple Rows to new workbook when multiple criteria is met. flds Excel Programming 5 09-30-2014 09:58 AM
Combine or merge multiple worksheets into one worksheet timomaha Excel 1 07-21-2014 01:02 PM
Cross-referencing in multiple documents that will combine to make one report razberri Word 1 01-20-2014 01:00 AM
combine multiple documents word starter 2010 bribelge Word 3 12-19-2012 09:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:31 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft