+ Reply to Thread
Results 1 to 9 of 9

automatically insert rows in excel 2013

  1. #1
    Registered User
    Join Date
    01-18-2016
    Location
    VN
    MS-Off Ver
    2013 & 2016
    Posts
    17

    automatically insert rows in excel 2013

    My dear!

    Please help me some codes that can insert rows automatically following a certain division (as the attached sample, division is 12 cartons per row).

    Thanks for your help.
    Best regards,
    Doco

    how to insert row automatically.xlsx

  2. #2
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: automatically insert rows in excel 2013

    Have a look at Sheet2
    1. Is this how sheet1 should be after rows inserted?
    2. What happens if column "D" does not divide by 12
    eg row 17 , column D = 81. How many rows? 81/12 = 6 PLUS 1 = 7 rows?

    If different please attach workbook showing how sheet1 should look
    thanks
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    01-18-2016
    Location
    VN
    MS-Off Ver
    2013 & 2016
    Posts
    17

    Re: automatically insert rows in excel 2013

    Thanks Kevin,

    81 carton will have 7 rows.
    Please see the resend sheet 1 for your ref. if 112 cartons will have 10 rows inserted (9rows+1=10rows)

    Thanks
    Last edited by docodao; 01-20-2016 at 02:54 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: automatically insert rows in excel 2013

    Run this and see if sheet 2 is what you are looking for.
    (if not, please amend sheet2 to how you want it to look)

    Correct number of rows being inserted
    (but unsure if you want it to do anything else)

    LastRow number hard coded because of extra data below the range operating in.
    Various tricks like UsedRange.rows.count would give the incorrect answer.

    When adding and deleting rows it is often better to start at the bottom of the range and work up
    (avoids VBA re-numbering rows ahead of itself)


    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-18-2016
    Location
    VN
    MS-Off Ver
    2013 & 2016
    Posts
    17

    Re: automatically insert rows in excel 2013

    Quote Originally Posted by Kevin# View Post
    Run this and see if sheet 2 is what you are looking for.
    (if not, please amend sheet2 to how you want it to look)

    Correct number of rows being inserted
    (but unsure if you want it to do anything else)

    LastRow number hard coded because of extra data below the range operating in.
    Various tricks like UsedRange.rows.count would give the incorrect answer.

    When adding and deleting rows it is often better to start at the bottom of the range and work up
    (avoids VBA re-numbering rows ahead of itself)


    Please Login or Register  to view this content.
    Thank you Kevin!
    I am trying to test your codes.
    The rows of records are not always 68. each lot have various row number. So I need to count the rows firstly.

    Thanks brother

  6. #6
    Registered User
    Join Date
    01-18-2016
    Location
    VN
    MS-Off Ver
    2013 & 2016
    Posts
    17

    Re: automatically insert rows in excel 2013

    Dear Kevin,

    I add "- 1" on this code row (if not, it will be excess of 1 row)
    i = Application.WorksheetFunction.RoundUp(ws.Cells(r, 4).Value / 12, 0) - 1

    I don't know where is he problem while it have wrong result on the rows from 53 to row 56 as follows:
    row 53 : 54 cartons (insert 7 rows), 5 rows is correct.
    row 54 : 9 cartons (insert 3 rows), 1 rows is correct
    row 55 : 6 cartons (insert 3 rows), 1 rows is correct
    row 56 : 12 cartons (insert 3 rows), 1 rows is correct

    Rows 42, 62, 65, 67 have wrong result also.

    Your codes help me much in cutting time. Thanks bro
    Please also help me to identify the last row number.
    Last edited by docodao; 01-20-2016 at 10:47 PM.

  7. #7
    Registered User
    Join Date
    01-18-2016
    Location
    VN
    MS-Off Ver
    2013 & 2016
    Posts
    17

    Re: automatically insert rows in excel 2013

    I modify a little, then it work perfectly. Thank you Kevin!

    Sub InsertRows()
    Dim ws As Worksheet
    Dim r, j As Long
    Dim i As Integer
    Set ws = ActiveSheet
    firstrow = 3
    LastRow = 68 '(please help me to count the row automatically and start inserting from the last record)
    r = LastRow
    keepTrying:
    If r = 2 Then Exit Sub
    j = ws.Cells(r, 4).Value
    If j > 12 Then
    i = Application.WorksheetFunction.RoundUp(j / 12, 0) - 1
    ws.Rows(r + 1 & ":" & r + i).Insert Shift:=xlDown
    End If
    r = r - 1
    GoTo keepTrying:
    End Sub

  8. #8
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: automatically insert rows in excel 2013

    You said
    " How many rows? 81/12 = 6 PLUS 1 = 7 rows?"
    Your amended the formula for "i" now will result in 6
    Were you including the original row in your"How many rows?"


    To fix LastRow calculation
    Replace:
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-18-2016
    Location
    VN
    MS-Off Ver
    2013 & 2016
    Posts
    17

    Re: automatically insert rows in excel 2013

    Thanks for your respond.
    _ yes, 81/12 = 7 rows (1 original row + 6 inserted rows = 7)

    _ Your suggestion codes for defining last row is miracle. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Insert pdf in excel 2013
    By santbiju1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-03-2015, 06:40 PM
  2. Can't insert and delete rows and columns in Excel 2013
    By Rabenja in forum Excel General
    Replies: 4
    Last Post: 01-31-2014, 09:44 AM
  3. Excel 2013 Automatically insert a row when the last formatted row is used
    By lisakay in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-21-2013, 02:46 PM
  4. Replies: 16
    Last Post: 07-22-2013, 10:01 PM
  5. How can I set excel to automatically insert rows
    By Tima in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 08-13-2006, 12:25 PM
  6. how do you have rows automatically insert in excel?
    By Shelvonne in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-09-2005, 08:40 PM
  7. Replies: 1
    Last Post: 10-10-2005, 07:05 AM

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