+ Reply to Thread
Results 1 to 7 of 7

Thread: VBA Help - Row number not updated while copying formula

  1. #1
    Registered User
    Join Date
    06-08-2011
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    4

    VBA Help - Row number not updated while copying formula

    Hi,

    I am new to VBA excel. I am trying to copy the forumla from the previous row to the current row in a loop.

    While doing so the formula gets copied but the row Number is not updated.i.e the row number in the Forumla of NEW Row still points to the previous row.

    Kindly let me know how do I update the rownumber in the formula.

    ----
    wks.Cells(LastRow, "I").Formula = wks.Cells((LastRow - 1), "I").Formula
    -----

    Any help/suggestions are highly appreciated.

    Thanks,
    Ravindra

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: VBA Help - Row number not updated while copying formula

    When populating a range with a formula you don't normally need to increment the row number like that. Are you populating the initial cell's formula earlier on in you code?

    Also please remember to use code tags when posting code to the forum per the forum rules.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    06-08-2011
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA Help - Row number not updated while copying formula

    Hi,

    Thanks for your reply.

    I am using an excel where the forumla is already populated. I am just trying to add new rows and copy the formula from the previous rows.

    loop start
    wks.Cells(LastRow, "I").Formula = wks.Cells((LastRow - 1), "I").Formula
    
    LastRow = wks.Cells.Find(what:="*", after:=wks.Cells(1, 1), searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
    loop end

  4. #4
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: VBA Help - Row number not updated while copying formula

    I think it would help to see a sample workbook with your full code showing what you are trying to achieve.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Registered User
    Join Date
    06-08-2011
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA Help - Row number not updated while copying formula

    Hi,

    Attached is the sheet with sample copy.

    In this attachment, I will copy a new ROW i.e no 7 with new data. I would like to copy the same formula from ROW-6 for columns I to Columns- T.

    Hope this is clear.

    Thanks,
    Ravindra
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    06-08-2011
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA Help - Row number not updated while copying formula

    Hi,

    Sorry, below is the code snippet. Here I am trying to copy formula for only One column J for testing.

       
        'Count the number of rows to be copied from the current week sheet
        Dim cntRowsToCopy
        cntRowsToCopy = Application.CountA(Range("A:A"))
        Debug.Print cntRowsToCopy
     
        Dim i
        For i = 1 To cntRowsToCopy Step 1
        wks.Cells(LastRow, "A").Value = ws.Cells(i, "A").Value
        wks.Cells(LastRow, "B").Value = ws.Cells(i, "B").Value
        wks.Cells(LastRow, "C").Value = ws.Cells(i, "C").Value
        wks.Cells(LastRow, "D").Value = ws.Cells(i, "D").Value
        wks.Cells(LastRow, "E").Value = ws.Cells(i, "E").Value
        wks.Cells(LastRow, "F").Value = ws.Cells(i, "F").Value
        wks.Cells(LastRow, "G").Value = ws.Cells(i, "G").Value
        wks.Cells(LastRow, "H").Value = ws.Cells(i, "H").Value
        wks.Cells(LastRow, "I").Formula = wks.Cells((LastRow - 1), "I").Formula
        LastRow = wks.Cells.Find(what:="*", after:=wks.Cells(1, 1), searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
        Next i
        'Find the last row with data for formatting purpose and adding Week Ending comment for identification.
        LastRow = wks.Cells.Find(what:="*", after:=wks.Cells(1, 1), searchorder:=xlByRows, searchdirection:=xlPrevious).Row
        Debug.Print LastRow

  7. #7
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: VBA Help - Row number not updated while copying formula

    If you paste extra data into columns A:H and then run this:

    Sub Test()
    
    Dim lngDataLastRow As Long
    Dim lngFormulaLastRow As Long
    
    lngDataLastRow = Cells(Rows.Count, 1).End(xlUp).Row
    lngFormulaLastRow = Cells(Rows.Count, 9).End(xlUp).Row
    
    Range("I" & lngFormulaLastRow).Resize(1, 12).AutoFill Destination:=Range("I" & lngFormulaLastRow & ":T" & lngDataLastRow), Type:=xlFillDefault
     
    End Sub

    It will then populate columns I:T by copying the formula down.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0