+ Reply to Thread
Results 1 to 25 of 25

Macro to Merge Sequential Dates in Rows

Hybrid View

MSmithson Macro to Merge Sequential... 03-22-2011, 02:06 PM
Richard Buttrey Re: Macro to Merge Sequential... 03-22-2011, 02:56 PM
MSmithson Re: Macro to Merge Sequential... 03-22-2011, 03:16 PM
Richard Buttrey Re: Macro to Merge Sequential... 03-22-2011, 07:45 PM
MSmithson Re: Macro to Merge Sequential... 03-22-2011, 07:58 PM
MSmithson Re: Macro to Merge Sequential... 03-22-2011, 08:03 PM
Richard Buttrey Re: Macro to Merge Sequential... 03-22-2011, 08:21 PM
MSmithson Re: Macro to Merge Sequential... 03-22-2011, 08:46 PM
Richard Buttrey Re: Macro to Merge Sequential... 03-23-2011, 06:30 AM
snb Re: Macro to Merge Sequential... 03-23-2011, 07:02 AM
MSmithson Re: Macro to Merge Sequential... 03-23-2011, 12:25 PM
MSmithson Re: Macro to Merge Sequential... 03-23-2011, 12:30 PM
Richard Buttrey Re: Macro to Merge Sequential... 03-23-2011, 12:56 PM
  1. #1
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to Merge Sequential Dates in Rows

    Hi,

    I'd be inclined not to try and change a range which you're currently working with. That seems to me fraught with potential problems.

    However you could achieve the same result, finishing the macro with a deletion of the original data and then cutting and pasting the resultant data back in place of the original.

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  2. #2
    Registered User
    Join Date
    09-21-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Macro to Merge Sequential Dates in Rows

    True, the copy, change, then delete old data could work.

    Or..is there any way we could move this data onto a new worksheet? As in, open up a new tab and paste the new adjusted info there?

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to Merge Sequential Dates in Rows

    Hi,

    Yes indeed.

    Just add the following code after the last 'Loop' instruction

    Cells(1, lColStart + 10).CurrentRegion.Offset(0, 1).Copy
    Sheets.Add
    ActiveSheet.Paste
    Regards

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Macro to Merge Sequential Dates in Rows

    or
    Sub snb()
      sq = Sheets(1).Cells(1).CurrentRegion
      For j = 2 To UBound(sq) - 1
        If sq(j, 1) & sq(j, 5) & sq(j, 6) & sq(j, 7) = sq(j + 1, 1) & sq(j + 1, 5) & sq(j + 1, 6) & sq(j + 1, 7) And sq(j + 1, 2) - sq(j, 3) = 1 Then
           sq(j, 3) = sq(j + 1, 3)
           sq(j, 4) = sq(j, 4) & " " & sq(j + 1, 4)
           sq(j, 8) = sq(j, 8) & " " & sq(j + 1, 8)
           sq(j + 1, 1) = ""
           j = j + 1
        End If
      Next
    
      With Sheets.Add
        .Cells(1).Resize(UBound(sq), UBound(sq, 2)) = sq
        .Columns(1).SpecialCells(4).EntireRow.Delete
     End With
    End Sub



  5. #5
    Registered User
    Join Date
    09-21-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Macro to Merge Sequential Dates in Rows

    Thanks SNB, your code seems to do all the matching and merging correctly, but it also gets rid of all formatting such as fonts and colors. See attached file (your code is added). It removes all formatting from the header and notes columns. Any way to keep this? Thanks for your help.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    09-21-2010
    Location
    Boston, MA
    MS-Off Ver
    Excel 2003
    Posts
    81

    Re: Macro to Merge Sequential Dates in Rows

    Thanks Richard, however your code does not seem to do anything...the macro just stops after it merges the cells and copys the info into the same sheet. I also tried a few variations on the code and couldn't get it to delete the old rows - are the new cells somehow locked or something that prevents deleting the old cells? Thanks for the help.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to Merge Sequential Dates in Rows

    Hi,

    Sorry, I'd forgotten that the macro finishes as soon as the If Test just after the Do While....is satisfied. This is where the extra lines are needed. Hence add the following 6 lines immediately after the Do While...

    Do While stCurrent = stNext
            If Cells(lCount, lColStart) = "" Then
                Cells(1, lColStart + 10).CurrentRegion.Offset(0, 1).Copy
                Sheets.Add
                ActiveSheet.Paste
                End
            End If

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Macro to Merge Sequential Dates in Rows

    Have you any ideas yourself?
    For instance copying the 'fieldnames' from one sheet to another ?
    I have no clue on the formatting of cells and the relation between the formatting and the content.

    As you have undoubtedly noticed analysing my suggestion, it only handles the content of the cells.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1