Consulting

Results 1 to 6 of 6

Thread: To unmerge cells and fill down blank cells with data from above

  1. #1
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    2
    Location

    To unmerge cells and fill down blank cells with data from above

    Hello,
    I have found a VBA code on the internet for unmerging the cells and then filling down the blank cells with the cell on above but the problem which i have with this VBA code is that i have to run it for every sheet in the excel file.I mean if i have 7 sheets i have to run it 7 times.is there anybody here to help me to generalize the VBA code for whole of worksheets into workbook by running the macro only once and it's done for all the sheet entirely.


    Public Sub ProcessData() 
    Dim cell As Range 
    Dim MergedCell As Range 
    Dim NumRows As Long 
    Dim NumCols As Long 
    
    With Application 
    
    .ScreenUpdating = False 
    .Calculation = xlCalculationManual 
    End With 
    
    With ActiveSheet 
    
    For Each cell In Range("A1").Resize( _ 
    .Cells.SpecialCells(xlCellTypeLastCell).Row, _ 
    .Cells.SpecialCells(xlCellTypeLastCell).Column) 
    
    If cell.MergeCells Then 
    
    NumRows = cell.MergeArea.Rows.Count 
    NumCols = cell.MergeArea.Columns.Count 
    cell.UnMerge 
    If NumRows > 1 Then cell.AutoFill cell.Resize(NumRows) 
    If NumCols > 1 Then cell.Resize(NumRows).AutoFill cell.Resize(NumRows, NumCols) 
    End If 
    Next cell 
    End With 
    
    With Application 
    
    .Calculation = xlCalculationAutomatic 
    .ScreenUpdating = True 
    End With 
    
    End Sub

    Many thanks if somebody could help me.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Public Sub ProcessData()
        Dim cell As Range
        Dim MergedCell As Range
        Dim NumRows As Long
        Dim NumCols As Long
        Dim Sh As Worksheet
         
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
        End With
        For Each Sh In Worksheets
        With Sh
            For Each cell In Sh.UsedRange.Cells
                If cell.MergeCells Then
                    NumRows = cell.MergeArea.Rows.Count
                    NumCols = cell.MergeArea.Columns.Count
                    cell.UnMerge
                    If NumRows > 1 Then cell.AutoFill cell.Resize(NumRows)
                    If NumCols > 1 Then cell.Resize(NumRows).AutoFill cell.Resize(NumRows, NumCols)
                End If
            Next cell
        End With
        Next Sh
        With Application
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    2
    Location
    Thanks a lot for your quick answer. It's working without any problem

  4. #4
    VBAX Regular
    Joined
    Apr 2019
    Posts
    6
    Location
    Wow that works great but I am having a minor problem. The problem is that my data has addresses which have pin code at the end and floor no in the beginning, so when it unmerge and autofills it down it increases the numbers like. The 1st floor becomes 2nd floor,3rd floor and like that at the beginning and the pin code increases from 400028 to 400029 etc.

    The data is large so I can not do it manually. The below code is otherwise working perfectly the only thing I want is that I don't want the autofill to be incremental.

    So is there any way I can make modification to the above code to just fill down anything without increasing the values in autofill. Please help.

  5. #5
    VBAX Regular
    Joined
    Apr 2019
    Posts
    6
    Location
    Hey it's fine.I found the solution. Thanks to the forum
    If NumRows > 1 Then cell.AutoFill Destination:=cell.Resize(NumRows), Type:=xlFillCopy
    If NumCols > 1 Then cell.Resize(NumRows).AutoFill Destination:=cell.Resize(NumRows, NumCols), Type:=xlFillCopy

  6. #6
    VBAX Regular
    Joined
    Apr 2019
    Posts
    6
    Location
    Hey it's fine.I found the solution. Thanks to the forum

    If NumRows > 1 Then cell.AutoFill Destination:=cell.Resize(NumRows), Type:=xlFillCopy
    If NumCols > 1 Then cell.Resize(NumRows).AutoFill Destination:=cell.Resize(NumRows, NumCols), Type:=xlFillCopy

Posting Permissions

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