Consulting

Results 1 to 7 of 7

Thread: Need Help in making minor modification to an excel vba code.

  1. #1
    VBAX Regular
    Joined
    Apr 2019
    Posts
    6
    Location

    Need Help in making minor modification to an excel vba code.

    Hello all,
    I found the below vba code on the same forum where one can unmerge and fill down the duplicates values. 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 below code to just fill down anything without increasing the values in autofill. Please help.

    Thanks in advance.
    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
    Last edited by Nick G; 04-11-2019 at 10:12 PM. Reason: I am new here so I don't have much ideas for posting. So made a error and rectified it here.

  2. #2
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi Nick G!
    Welcome to vbax forum.
    Not sure
    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

  3. #3
    VBAX Regular
    Joined
    Apr 2019
    Posts
    6
    Location
    Thanks for the welcome and for the prompt reply. Oh God bless your soul. It's working like a charm now. I can't thank you enough.

  4. #4
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    @Nick. Can you post an attachment for my test?

  5. #5
    VBAX Regular
    Joined
    Apr 2019
    Posts
    6
    Location
    Sorry, As it was the file from the work I can't share the exact file. But when I go home today I could try to replicate the same and if it comes out that way, I will share it with you. Thanks again

  6. #6
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    I made an example and did not find problem what you said.

    Sorry, I didn't see your update, please ignore this reply.


    --Okami
    Attached Files Attached Files
    Last edited by 大灰狼1976; 04-12-2019 at 12:38 AM.

  7. #7
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    I think it's better to change it to this way.
        If cell.MergeCells Then
          NumRows = cell.MergeArea.Rows.Count
          NumCols = cell.MergeArea.Columns.Count
          cell.UnMerge
          cell.Resize(NumRows, NumCols) = cell
        End If

Posting Permissions

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