+ Reply to Thread
Results 1 to 5 of 5

copy range of formulas when cell is empty

Hybrid View

  1. #1
    Registered User
    Join Date
    03-28-2012
    Location
    uk
    MS-Off Ver
    Mac 2011
    Posts
    35

    copy range of formulas when cell is empty

    Dear all,

    I am trying to fill up a range with the range row-1 where one or several cells are empty.

    When running a macro in Staff Listing, it updates the Report sheet with new employees details range(A:J). The range(K:T) pull datas through formula. I need to copy range(K:T) for each new rows containing information in range(A:A)
    The file attached gives a better idea of what I am after.

    Thanks.
    Attached Files Attached Files

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: copy range of formulas when cell is empty

    Use this code
     
    Sub copy_formulae()
    Dim lrow As Long
    Dim i As Long
    
    With Worksheets("Sheet1")
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    
        For i = 6 To lrow
            If .Range("A" & i).Value <> "" Then
                .Range("K" & i & ":T" & i).Formula = .Range("K" & i - 1 & ":T" & i - 1).Formula
            End If
        Next i
    End With
    
    End Sub
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    03-28-2012
    Location
    uk
    MS-Off Ver
    Mac 2011
    Posts
    35

    Re: copy range of formulas when cell is empty

    Thanks Arlette,
    It works well but I would like to modify it as the formula uses indirect and The reference cell needs to be incremented accordingly.

    Cheers

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: copy range of formulas when cell is empty

    After changing the references, i realised that the references still dont get incremented with the earlier code. Use this instead:
    Option Explicit
    
    Sub copy_formulae()
    Dim lrow As Long
    Dim i As Long
    
    With Worksheets("Sheet1")
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        
        For i = 6 To lrow
            If .Range("A" & i).Value <> "" Then
                .Range("K" & i-1 & ":T" & i).FillDown
            End If
        Next i
    End With
    
    End Sub

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: copy range of formulas when cell is empty

    If you need the references to increment, you will need to change the absolute referencing to relative referencing in your formula. For e.g. you have month($W$22) as a small part of your formula. Change it to $W22.

+ 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