+ Reply to Thread
Results 1 to 26 of 26

New Row

Hybrid View

  1. #1
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    New Row

    Hi. This is the macro that I currently have. As you can see it starts w/ rows(21:21), however, if row 21 is enter and you run the macro again, it starts again at row 21 hence copying down whatever is currently in row 21 to 22. I do not want this macro to do that. i want it to start at the next empty row if previous rows are occupied. how do i do that? Thanks.
    Sub NewJob()
    '
    ' NewJob Macro
    ' To insert new jobs.
    '
    
    '
        Rows("21:21").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("I20").Select
        Selection.Copy
        Range("I21").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("K20").Select
        Selection.Copy
        Range("K21").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("O20").Select
        Selection.Copy
        Range("O21").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("A26").Select
    End Sub
    Last edited by davesexcel; 12-28-2010 at 03:05 PM. Reason: code tags required please read the forum rules

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,493

    Re: New Row

    Dim Rws As Long
    
    
    Rws = Cells(Rows.Count, "A").End(xlUp).Row
    
    Cells(Rws, 1).Select
    This selects the first empty cell after the last entry in column A

    Change the column letter and column number to suite your requirements

  3. #3
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Quote Originally Posted by davesexcel View Post
    Dim Rws As Long
    
    
    Rws = Cells(Rows.Count, "A").End(xlUp).Row
    
    Cells(Rws, 1).Select
    This selects the first empty cell after the last entry in column A

    Change the column letter and column number to suite your requirements
    I do not understand. Does this replace the 1st line of the formula? How would it look like?

  4. #4
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Quote Originally Posted by ruckuz View Post
    I do not understand. Does this replace the 1st line of the formula? How would it look like?
    Can someone help, please?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: New Row

    Hello Ruckuz,

    This version of the macro will copy the values from the previous row to the next empty row.
    Sub NewJob()
    '
    ' NewJob Macro
    ' To insert new jobs.
    '
    
    'http://www.excelforum.com/excel-programming/758088-new-row.html
    
      Dim RngEnd As Range
      
        Set RngEnd = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False)
        If RngEnd Is Nothing Then Exit Sub Else Set RngEnd = RngEnd.EntireRow.Offset(1, 0)
        
        With RngEnd
          .Cells(1, "I") = .Cells(1, "I").Offset(-1, 0)
          .Cells(1, "K") = .Cells(1, "K").Offset(-1, 0)
          .Cells(1, "O") = .Cells(1, "O").Offset(-1, 0)
        End With
           
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Quote Originally Posted by Leith Ross View Post
    Hello Ruckuz,

    This version of the macro will copy the values from the previous row to the next empty row.
    Sub NewJob()
    '
    ' NewJob Macro
    ' To insert new jobs.
    '
    
    'http://www.excelforum.com/excel-programming/758088-new-row.html
    
      Dim RngEnd As Range
      
        Set RngEnd = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False)
        If RngEnd Is Nothing Then Exit Sub Else Set RngEnd = RngEnd.EntireRow.Offset(1, 0)
        
        With RngEnd
          .Cells(1, "I") = .Cells(1, "I").Offset(-1, 0)
          .Cells(1, "K") = .Cells(1, "K").Offset(-1, 0)
          .Cells(1, "O") = .Cells(1, "O").Offset(-1, 0)
        End With
           
    End Sub

    Hi,
    The code above doesn't work. I'm not too familiar w/ macros so hence the dumb questioning.

  7. #7
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Hi Leith,
    I know how to copy the macro to VB. The macro itself is not working like it's suppose to. Perhaps I can send you my spreadsheet so you can see what I mean?

  8. #8
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,671

    Re: New Row

    You didn't attach a sample workbook, so I don't know what you're up to.

    You seem to be saying that you want the macro to copy certain data from row 20 to the next available row on the sheet every time the macro is run.

    davesexcel's line:
    Rws = Cells(Rows.Count, "A").End(xlUp).Row
    loads the number of the last used row into Rws; the next available blank row is just Rws +1.
    Then you need to copy the data values from row 20 to that row:
          Cells(Rws, "I").Value = Cells(20, "I").Value
          Cells(Rws, "K").Value= Cells(20, "K").Value
          Cells(Rws, "O").Value = Cells(20, "O").Value
    So that the macro becomes:
    Option Explicit
    Sub insert()
        Dim Rws As Long
        
        'get next row
        Rws = Cells(Rows.Count, "A").End(xlUp).Row + 1
        
        'copy data
        Cells(Rws, "I").Value = Cells(20, "I").Value
        Cells(Rws, "K").Value = Cells(20, "K").Value
        Cells(Rws, "O").Value = Cells(20, "O").Value
    End Sub
    Though I'm curious why, if data in row 20 is changing, it just can't be put into the next row to begin with?
    Ben Van Johnson

  9. #9
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Please take a look at my spreadsheet.

    as you can see, row 22 only includes columns that have formulas and row 23 is blank.
    i'm looking for a macro that will copy those formulas down every time i insert a new row.

    for instance. i press a macro button to insert a new row. formulas from row 22 will copy down to row 23 and then row 24 will now become a new blank row. am i making sense? Thanks and happy new year!
    Attached Files Attached Files

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: New Row

    Hello ruckuz,

    Now that I can see the layout, it is clear what you want to do.

  11. #11
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Quote Originally Posted by Leith Ross View Post
    Hello ruckuz,

    Now that I can see the layout, it is clear what you want to do.
    Thanks Leith,
    Guess I should of upload the spreadsheet earlier and make life easier for everyone. Lesson learned. The code above was close to what I'm looking for, had to change values to formula, but the formula is not totally correct and it doesnt automatically insert a new row.

    Can you help now that you see what I'm talking about? Thanks.

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: New Row

    Hello ruckuz,

    I don't have Excel 2007 but I have a converter to read the 2007 format. Sometimes I can change the files and save them and the changes work. In this case, it isn't working.

  13. #13
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Sorry Leith,
    Didn't know Mr. Excel is connected w/ this forum. The question has not been answered. Sorry about that, won't happen again.

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: New Row

    Hello ruckuz,

    Not a problem. You're new here. I don't expect you to have read all the rues about posting yet. I am still working on the code and just about have it working. I copied the data over to a 2003 workbook so I could tell the difference between my code problems and those raised by converting the workbook.

  15. #15
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Quote Originally Posted by Leith Ross View Post
    Hello ruckuz,

    Not a problem. You're new here. I don't expect you to have read all the rues about posting yet. I am still working on the code and just about have it working. I copied the data over to a 2003 workbook so I could tell the difference between my code problems and those raised by converting the workbook.
    Thank you Leith. This forum has been very helpful. I will contribute my novice expertise as much as I can.

  16. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: New Row

    Hello Ruckuz,

    Here is the working macro. Copy this into Module2. You can then add a button to the worksheet (somewhere) to run the macro or do it manually.

    EDIT: I attached the workbook with the macro and the button.
    Sub InsertNewRow()
    
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Jobs")
        
        Set Rng = Range("A3:W3")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column + 1).End(xlUp)
        If RngEnd.Row < Rng.Row Then Exit Sub
        
          Set RngEnd = RngEnd.Offset(0, -1).Resize(1, Rng.Columns.Count)
          RngEnd.insert Shift:=xlShiftDown
          
            For Each Cell In RngEnd.Offset(-2, 0)
              If Not Cell.Formula Like "=*" Then
                 Cell.Offset(1, 0).Value = ""
              Else
                 Cell.Resize(RowSize:=2).FillDown
              End If
            Next Cell
        
    End Sub
    Attached Files Attached Files
    Last edited by Leith Ross; 12-30-2010 at 03:26 AM. Reason: Attached workbook

  17. #17
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    [SOLVED] Thanks!! Happy New Year!

  18. #18
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: New Row

    Hello Ruckuz,

    Happy New Year !

  19. #19
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Leith,
    Sorry to be of bother yet again. I noticed that when ever i add a line, my subtotal does not include the new line that is added for instance. subtotal(9,c1:c21), line 21 is the last line before i press the macro to insert a new line. i press the macro to insert new line and instead of subtotal being subtotal(9,c1:c22), which 22 is the new line, it stays subtotal(9,c1:c21), therefore not calculating the numbers in the new line, 22.

  20. #20
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: New Row

    Hello Ruckuz,

    Thanks for pointing that out. I will work on it.

  21. #21
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    Quote Originally Posted by Leith Ross View Post
    Hello Ruckuz,

    Thanks for pointing that out. I will work on it.
    Thanks Leith.

  22. #22
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: New Row

    Hello Ruckuz,

    I replaced the Subtotal function with a Sum since that is what the Subtotal was doing. The ranges are now automatically sized and summed. The Subtotal functions in row 23 are still there. Here is the formula I used and a copy of the workbook.

    Row 21:
    =SUM(OFFSET(C$3,0,0,ROW()-3, 1))
    Attached Files Attached Files

  23. #23
    Registered User
    Join Date
    07-27-2009
    Location
    New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: New Row

    i think it's finally [SOLVED] but will test to make sure. Thanks Leith!!!!

+ 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.6.0 RC 1