Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2014, 12:32 PM
babs babs is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2010 64bit
Novice
Combining Rows in Excel
 
Join Date: Jul 2014
Posts: 3
babs is on a distinguished road
Default Combining Rows in Excel

I have seen similar posts to this, but none quite like what I need help with...

I have a sheet where each row is an agreement, but not every cell in that row has a value and information for one agreement could be spread out onto multiple rows. I want each unique agreement to have one row. In the attached sample you will see there are a lot of blank cells on each row and information for one agreement number could be spread out between about six lines. If I could get everything associated with the same agreement/sub number on one line and have the other ones removed that would be great! I feel like when I go through options in my head it becomes much more complicated than it should be! The current sheet I'm working from has about 23,000 lines in it, so this will be a huge help!
Attached Files
File Type: xlsx SampleData.xlsx (10.7 KB, 13 views)
Reply With Quote
  #2  
Old 07-10-2014, 04:02 PM
charlesdh charlesdh is offline Combining Rows in Excel Windows 7 32bit Combining Rows in Excel Office 2010 32bit
Expert
 
Join Date: Apr 2014
Location: Mississippi
Posts: 382
charlesdh is on a distinguished road
Default

Hi,

Can you provide a example of the result you are looking for?
Reply With Quote
  #3  
Old 07-12-2014, 08:48 AM
excelledsoftware excelledsoftware is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Hi Babs,

I am working on this code for you. Basically you need something that will sort the data, look through the groups, grab each bit of data in the columns and then export them to a new worksheet on their own row. Then go to the next group. If each group has about 8 rows each for estimation, your results should be around 3000 rows. I plan on having it written today.
Reply With Quote
  #4  
Old 07-13-2014, 09:22 PM
excelledsoftware excelledsoftware is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Hmmn still no reply? well in that case I may get this code done (or I may not) and post it for others to see but I am guessing this question was probably posted on multiple forums. Very unfortunate, sure glad I didn't waste my time on it yet.
Reply With Quote
  #5  
Old 07-14-2014, 09:03 AM
babs babs is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2010 64bit
Novice
Combining Rows in Excel
 
Join Date: Jul 2014
Posts: 3
babs is on a distinguished road
Default Output Example

Sorry, I was off Friday and didn't check my email over the weekend. This isn't posted on any other forum though. I have attached an example of what I would be looking for in the output. I made a loop that basically copy and pastes, then deletes the line it came from. However, that takes a while to run through all the lines and I know there has to be a better way! I appreciate you taking the time to look at it!
Attached Files
File Type: xlsx SampleDataOutput.xlsx (9.7 KB, 10 views)
Reply With Quote
  #6  
Old 07-14-2014, 05:15 PM
excelledsoftware excelledsoftware is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Then I am in error here. I sincerely apologize for jumping to conclusions. I will write this code for you and post it soon. Again I am sorry please accept my apologies.
Reply With Quote
  #7  
Old 07-14-2014, 06:42 PM
excelledsoftware excelledsoftware is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

OK. Thanks for posting the result I actually had it all coded to column C and then I saw column D was the way you wanted to go. Now I have hard coded column D but we can always change that up if needed. This program works with the default sheet names so if you have changed those we will need to change it in the code as well. I ran a test on your sample sheet and it looks like it worked. Please save a back up copy before running as this code will process a sort on your original data. With your example everything ran fine but there could be bugs with a bigger data set. Give it a shot and let me know if you have any questions. Thanks for your patience.

Code:
Option Explicit
Sub ParseAgreements()
  'searches through all sorted records of a table
  'and exports the results in a 1 record per row
  'result.
  
  Dim EndRngRow As Long, CheckRow As Long
  Dim ResultRow As Long
  Dim FoundEndRow As Boolean
  Dim FindCol As Integer, StartRngRow As Integer, BegRow As Integer
  Dim Col As Integer
  Dim ValString As String, AgreementNum As String, ColData(11) As String
  Dim ws As Worksheet, rws As Worksheet
  Dim wb As Workbook
  
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet1") 'worksheet with data.
    Set rws = wb.Worksheets("Sheet2") 'worksheet for result.
  
    StartRngRow = 2 'Change if your data starts below row 2
    ResultRow = 2
    BegRow = StartRngRow - 1
    On Error GoTo 0 'Put errors back to normal
    'Convert the found column to a string
    If Range("D" & StartRngRow - 1).Value <> "Agreement/Subs Num" Then
      MsgBox "The Agreement Sub column is not in D" & _
      StartRngRow - 1 & vbLf & _
      "This program is coded to work with column D" & vbLf & _
      "Program Ending"
      End
    End If
    
    'Set up the results headers
    rws.Range("A1:Z30000").ClearContents
    rws.Range("A1").Value = "Legacy Number"
    rws.Range("B1").Value = "Alternative Lease Number"
    rws.Range("C1").Value = "Agreement Number"
    rws.Range("D1").Value = "Agreement/Subs Num"
    rws.Range("E1").Value = "Property Status"
    rws.Range("F1").Value = "Full Lessor Name"
    rws.Range("G1").Value = "Immediate Predecessor"
    rws.Range("H1").Value = "Book"
    rws.Range("I1").Value = "Term Length (Months)"
    rws.Range("J1").Value = "Total Bonus Amount"
    rws.Range("K1").Value = "Lease Memo"

    CheckRow = StartRngRow
    ws.Activate
    ws.Range("D" & CheckRow).Select
    Selection.CurrentRegion.Select
    Selection.Sort Key1:=Range("D" & CheckRow), Order1:=xlAscending, _
    Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    CheckRow = StartRngRow
    Do
      AgreementNum = ws.Range("D" & StartRngRow).Value
      FoundEndRow = False
        Do Until FoundEndRow = True
          If ws.Range("D" & CheckRow).Value <> AgreementNum Then
            EndRngRow = CheckRow - 1
            FoundEndRow = True
          End If
          If ws.Range("D" & CheckRow).Value = "" Then
            FoundEndRow = True 'end the loop
          End If
          CheckRow = CheckRow + 1
        Loop
        For CheckRow = StartRngRow To EndRngRow
          For Col = 1 To 11
            If ws.Cells(CheckRow, Col).Value <> "" Then
              ColData(Col) = ws.Cells(CheckRow, Col).Value
            End If
          Next Col
        Next CheckRow
        'export the results
        For Col = 1 To 11
          rws.Cells(ResultRow, Col).Value = ColData(Col)
        Next Col
        'get ready for the next batch
        For Col = 0 To 11
          ColData(Col) = ""
        Next Col
        ResultRow = ResultRow + 1
        StartRngRow = CheckRow
    Loop Until ws.Range("D" & CheckRow).Value = ""
    rws.Activate
     
End Sub
Reply With Quote
  #8  
Old 07-15-2014, 07:14 AM
babs babs is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2010 64bit
Novice
Combining Rows in Excel
 
Join Date: Jul 2014
Posts: 3
babs is on a distinguished road
Default Thank you!

Great! Thank you so much! This is a great help!
Reply With Quote
  #9  
Old 07-15-2014, 10:24 AM
charlesdh charlesdh is offline Combining Rows in Excel Windows 7 32bit Combining Rows in Excel Office 2010 32bit
Expert
 
Join Date: Apr 2014
Location: Mississippi
Posts: 382
charlesdh is on a distinguished road
Default Modified code

HI,

My 2 cents...I modified excelledsoftware. All I did was shorten the code by using an "Array". This is just another way to code.

Code:
Sub ParseAgreements()
  'searches through all sorted records of a table
  'and exports the results in a 1 record per row
  'result.
  
  Dim EndRngRow As Long, CheckRow As Long
  Dim ResultRow As Long
  Dim FoundEndRow As Boolean
  Dim FindCol As Integer, StartRngRow As Integer, BegRow As Integer
  Dim Col As Integer
  Dim ValString As String, AgreementNum As String, ColData(11) As String
  Dim ws As Worksheet, rws As Worksheet
  Dim wb As Workbook
  
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet1") 'worksheet with data.
    Set rws = wb.Worksheets("Sheet2") 'worksheet for result.
  
    StartRngRow = 2 'Change if your data starts below row 2
    ResultRow = 2
    BegRow = StartRngRow - 1
    On Error GoTo 0 'Put errors back to normal
    'Convert the found column to a string
    If Range("D" & StartRngRow - 1).Value <> "Agreement/Subs Num" Then
      MsgBox "The Agreement Sub column is not in D" & _
      StartRngRow - 1 & vbLf & _
      "This program is coded to work with column D" & vbLf & _
      "Program Ending"
      End
    End If
    
    'Set up the results headers
    rws.Range("A1:Z30000").ClearContents
   '''' Modified '''
    rws.Range("A1:K1").Value = Array("Legacy Number", "Alternative Lease Number", _
        "Agreement Number", "Agreement/Subs Num", "Property Status", "Immediate Predecessor", "Book", "Term Length (Months)", "Total Bonus Amount", "Lease Memo")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    CheckRow = StartRngRow
    ws.Activate
    ws.Range("D" & CheckRow).Select
    Selection.CurrentRegion.Select
    Selection.Sort Key1:=Range("D" & CheckRow), Order1:=xlAscending, _
    Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    CheckRow = StartRngRow
    Do
      AgreementNum = ws.Range("D" & StartRngRow).Value
      FoundEndRow = False
        Do Until FoundEndRow = True
          If ws.Range("D" & CheckRow).Value <> AgreementNum Then
            EndRngRow = CheckRow - 1
            FoundEndRow = True
          End If
          If ws.Range("D" & CheckRow).Value = "" Then
            FoundEndRow = True 'end the loop
          End If
          CheckRow = CheckRow + 1
        Loop
        For CheckRow = StartRngRow To EndRngRow
          For Col = 1 To 11
            If ws.Cells(CheckRow, Col).Value <> "" Then
              ColData(Col) = ws.Cells(CheckRow, Col).Value
            End If
          Next Col
        Next CheckRow
        'export the results
        For Col = 1 To 11
          rws.Cells(ResultRow, Col).Value = ColData(Col)
        Next Col
        'get ready for the next batch
        For Col = 0 To 11
          ColData(Col) = ""
        Next Col
        ResultRow = ResultRow + 1
        StartRngRow = CheckRow
    Loop Until ws.Range("D" & CheckRow).Value = ""
    rws.Activate
     
End Sub
Reply With Quote
  #10  
Old 07-15-2014, 04:54 PM
excelledsoftware excelledsoftware is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Wow Charles! I didn't know you could use an array like that. I really like how it cleans it up. that will make so many of the tasks I do easier. I always thought you would have to loop through the cells with an array like that, but this works great! Thanks!!!
Reply With Quote
  #11  
Old 07-15-2014, 04:59 PM
charlesdh charlesdh is offline Combining Rows in Excel Windows 7 32bit Combining Rows in Excel Office 2010 32bit
Expert
 
Join Date: Apr 2014
Location: Mississippi
Posts: 382
charlesdh is on a distinguished road
Default

excelledsoftware,

I too learned from your post.
Reply With Quote
  #12  
Old 07-15-2014, 05:00 PM
excelledsoftware excelledsoftware is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Awesome Just Awesome!!
Reply With Quote
  #13  
Old 07-15-2014, 05:20 PM
BobBridges's Avatar
BobBridges BobBridges is offline Combining Rows in Excel Windows 7 64bit Combining Rows in Excel Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

I use arrays to populate ranges not so much because my program looks neater but because it requires a tiny fraction of the time. If we pretend it takes 15 seconds to fill in 5000 rows and 10 columns, it happens almost as fast as I can think about it if I fill up an array first and then put the array into a range.

<hijack>But there's something I've been thinking about for a while. My statement for populating from the array looks like this:
Code:
Range(ows.Cells(2, 1), ows.Cells(rz, 10)).Value = arr
But after looking at this I wonder whether it's possible to do it with other properties as well, for example:
Code:
Range(ows.Cells(2, 1), ows.Cells(rz, 10)).FormulaR1C1 = arr
This might allow me to do complex things with other than values. I've never tried it, but I give it about 60% that it'll work just fine.</hijack>
Reply With Quote
Reply

Tags
combine rows, vba, vba excel



Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Multiple Rows Merged into Word (Vertical Format) ats1025 Mail Merge 3 11-19-2013 02:21 PM
Combining Rows in Excel Add multiple rows to Excel 2010 table at cursor position C J Squibb Excel Programming 12 11-07-2013 07:35 AM
Excel Pivot - Do not display rows with zero value patidallas22 Excel 1 03-08-2013 01:35 PM
Combining Rows in Excel Find Results in excel copy the rows to another sheet khalidfazeli Excel 2 02-06-2013 09:38 AM
Drag and drop rows in Excel fireman0174 Excel 0 11-14-2011 05:26 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:21 AM.


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