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
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
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
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.
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.
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.
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
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:
withPlease Login or Register to view this content.
Please Login or Register to view this content.
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks