Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 26 of 26

Thread: Unmerge Cells & Copy Cells Down to Next Line of Data

  1. #21
    VBAX Regular
    Joined
    Dec 2021
    Posts
    13
    Location
    Paul, that is exactly it. Just trying to wrap my head around the change here.

    Thanks again!

  2. #22
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    I used the file in your first post:

    This is all you need for the first 4 columns:

    Sub M_snb()
       Sheet1.Cells.UnMerge
    
       For Each it In sheet1.Columns(1).Resize(, 4).SpecialCells(4).Areas
          it.Value = it.Offset(-1).Cells(1).Resize(, it.Columns.Count).Value
       Next
    End Sub
    For the first 4 columns in all sheets in the active workbook:
    Sub M_snb()
       For Each sh In Sheets
        sh.Cells.UnMerge
        
        If sh.Name <> "Instructions" Then
            For Each it In sh.Columns(1).Resize(, 4).SpecialCells(4).Areas
               it.Value = it.Offset(-1).Cells(1).Resize(, it.Columns.Count).Value
            Next
        End If
      Next
    End Sub
    NB. You should never use merged cells.
    Last edited by snb; 03-21-2022 at 03:47 AM.

  3. #23
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    @snb --

    1. I like your more efficient way, but this below eliminates the .Areas loop

    Option Explicit
    
    
    Sub UnmergeAndFill()
        Dim rData As Range, rBlanks As Range
        Dim ws As Worksheet
        
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name = "Instructions" Then GoTo NextSheet
            
            ws.Range("A:F").UnMerge
            
            On Error GoTo NextSheet
            Set rData = ws.Cells(1, 1).CurrentRegion
            Set rBlanks = rData.SpecialCells(xlCellTypeBlanks)
            rBlanks.FormulaR1C1 = "=R[-1]C"
            rData.Value = rData.Value
            On Error GoTo 0
                
            If Not ws.AutoFilterMode Then ws.Rows(1).AutoFilter
    
    
    NextSheet:
        Next
    
    
        MsgBox "Done"
    End Sub
    2.
    NB. You should never use merged cells.
    IMVHO, 'Never' is mostly correct, but I'd agree that 97% of the time merged cells only cause trouble
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #24
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    @PH

    Yes it does eliminate.
    But it takes more calculations.
    I am not an adversary to loops, which I am to 'GoTo'-statements.
    And almost equally to unnecessary Object variables:

     with ws.Cells(1, 1).CurrentRegion.SpecialCells(4)
       .Value = "=R[-1]C"
       .Value = .Value
     end with

  5. #25
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by snb View Post
    @PH

    Yes it does eliminate.
    But it takes more calculations.
    I am not an adversary to loops, which I am to 'GoTo'-statements.
    And almost equally to unnecessary Object variables:

     with ws.Cells(1, 1).CurrentRegion.SpecialCells(4)
       .Value = "=R[-1]C"
       .Value = .Value
     end with

    1. Well, the only advantage I see to my 'unnecessary Object variables' is that my way works without generating a lot of #N/A errors

    Capture.JPG

    Using the original Test_Unmerge2.xlsx as input, and adding your .Value snippet to the Unmerging and error checking and "Instructions" test



    2. I don't see any significant increase in calculations, and I've found that sometimes, and in very specific circumstances, a GoTo can make code more readable without turning it into a plate of spaghetti


    Sub UnmergeAndFill_snb()
        Dim ws As Worksheet
        
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name = "Instructions" Then GoTo NextSheet
            
            ws.Range("A:F").UnMerge
            
            On Error GoTo NextSheet
            
            'snb -----------------------------------------------------------
            With ws.Cells(1, 1).CurrentRegion.SpecialCells(4)
                .Value = "=R[-1]C"
                .Value = .Value
            End With
            'snb -----------------------------------------------------------
            
            On Error GoTo 0
                
            If Not ws.AutoFilterMode Then ws.Rows(1).AutoFilter
    NextSheet:
        Next
        
        MsgBox "Done"
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 03-22-2022 at 03:26 AM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #26
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    Here you go:

    Sub M_snb()
      For Each it In Sheets
        If it.Name <> "Instructions" Then
          it.Cells.UnMerge
            
          With it.Cells(1).CurrentRegion.Resize(, 4)
            .SpecialCells(4) = "=R[-1]C"
            .Copy
            .PasteSpecial -4163
          End With
        End If
      Next
    
      Application.CutCopyMode = False
    End Sub
    No pasta, rigatoni, farfalle, tagliatelle or spaghetti.

Posting Permissions

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