Insert row on base on cell value

chua_sb

New Member
Joined
Mar 30, 2023
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi Eveyone and Expert.
I need help.. I want to insert row base on the cell value. Can anyone help me out here?


VBA Code:
Sub BlankLine()
'Updateby20150203
Dim Rng As Range
Dim WorkRng As Range
Dim xInsertNum As Long

' On Error Resume Next
xTitleId = "Kutools for Excel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
If WorkRng Is Nothing Then Exit Sub
xInsertNum = Application.InputBox("The number of blank rows you want to insert", xTitleId, Type:=1)
If xInsertNum = False Then
MsgBox " The number of blank rows you want to insert ", vbInformation, xTitleId
Exit Sub
End If
Set WorkRng = WorkRng.Columns(1)
xLastRow = WorkRng.Rows.Count
Application.ScreenUpdating = False
For xRowIndex = xLastRow To 1 Step -1
Set Rng = WorkRng.Range("A" & xRowIndex)


If InStr(Rng.Value, "ALLEN") = 0 And InStr(Rng.Value, "ATC") = 0 And IsNumeric(Rng.Value) And Rng.Value > 0 Then
Rng.Offset(1, 0).Resize(xInsertNum).EntireRow.Insert Shift:=xlDown


End If
Next
Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:
I typically do not like to try to amend/change code that I didn't write, especially if it is not working in the first place!
If it was working except for one minor piece, or it was working and you wanted to add a little something extra, that would be one thing.
But I don't want to start working with code that is not working and may be far down the wrong path. I prefer to write my own code from scratch, rather than try to deal with that.

Regarding the column to run this against, do you want a prompt to ask for the column letter, or do you just want it to run on the column of the active cell that is selected when you call the macro?

Also, rather than me trying to recreate that table from scratch, can you post it in a manner which allows me to copy it?
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
Thank you so much.. The code is working.. I only can choose the number of line to be insert which this will apply to all "number" cells. Ie:
I want it to run on the column of the active cells when selected..
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
REQUESTED P/NDESCRIPTIONQTYAAR QTE PNQTY
QTE
PRICE
(EA)
EXT'D
SALES
CONDLEADTIME
(Days)
TRACECERTIFICATESNALTERNATE
PN
ALLEN
ATC
G4000VSVA01-1G4000VSVA011--CHECK-----6
1708897-117088971--CHECK-----1
01N65920ELT (1)101N659201--OHCSTOCK @BRUSSELAVIA 10/202217353331-1 - in ALLEN
3880938-1FILTER ELEMENT13880938-11$1,829.79$1,829.79FNSTOCKOEMOEM---
60-755100-5MOTOR IMPELLER SUBASSY, FUEL BOOST PUMP160-755100-51--SVCSSTOCK @BRUSSELCFM INTERNATIONAL INC.NOVO 1/2023030634004-1 - in ALLEN
CH31900-6-1518888-11$1,252.43$1,252.43FNSTOCKOEMOEM-518888-1-
99-000125-794-0BOUCHON DE SECURITE199-000125-794-0MOQ:10$52.87$528.74FN218OEMOEM---
9072215-1IGNITER PLUG (IRID)19072215-11$1,283.48$1,283.48FNSTOCKOEMOEM-518888-1-
8061-865M.E.C (11)18061-8651--CHECK-----2 - in ATC
1B6503-01LPC DISK (11)11B6503-011--CHECK-----1 - in ATC
54L064AIRSEAL (11)154L0641--CHECK-----2 - in ATC
3522W000-001-13522W000-0011---------
52B121-01DISK Y (11)152B121-011--CHECK-----1 - in ATC
C13200HA02FMGEC (1)1C13200HA021--CHECK-----2 - in ALLEN
822-1710-312RECEIVER (1)1822-1710-3121--SVCSSTOCK @BRUSSELROCKWELL 2/202045C9GT-1 - in ALLEN
C12850BC03-1C12850BC031--CHECK-----3
20790-03AC-120790-03AC1--CHECK-----3
801A50-0005-A-1801A50-0005A1$28.82$28.82FNSTOCKOEMOEM-801A50-0005A-
C12848EA01-1C12848EA011---------
790425A6STARTER (1)1790425A91$85,000.00$85,000.00FN1100OEMOEM-790425A9-
9057067SEAL190570671$2,046.44$2,046.44FNSTOCKOEMOEM---
34000055-7-134000055-71$27,425.82$27,425.82FN406OEMOEM---
TY2091-01APRIMARY (1)1TY2091-01A1--CHECK-----2 - in ALLEN
7588429-175884291$31.00$31.00FNSTOCKOEMOEM---
1027-2-011-8006-11027-2-011-80061---------
4291011MOTOR142910111$2,450.00$2,450.00SVCSSTOCK @BRUSSELFIRST 11/2017100062-1 - in ALLEN
 
Upvote 0
Do you think the above is good enough? I'm not sure if the above can be upload to the link as i'm using computer laptop.
 
Upvote 0
I think the code can be simplified a bit. This should do what you want (there really aren't all that many lines there when you take out all my comments):
VBA Code:
Sub MyInsertLines()

    Dim c As Long
    Dim lr As Long
    Dim r As Long
    Dim n As Long
   
    Application.ScreenUpdating = False
   
'   Get column of active cell
    c = ActiveCell.Column
   
'   First last row in column with data
    lr = Cells(Rows.Count, "C").End(xlUp).Row
   
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       See if entry in column is numeric
        If IsNumeric(Cells(r, c)) Then
'           Get number of rows to insert
            n = Cells(r, c)
'           Insert rows
            Cells(r + 1, c).Resize(n).EntireRow.Insert Shift:=xlDown
        End If
    Next r
   
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
I think the code can be simplified a bit. This should do what you want (there really aren't all that many lines there when you take out all my comments):
VBA Code:
Sub MyInsertLines()

    Dim c As Long
    Dim lr As Long
    Dim r As Long
    Dim n As Long
  
    Application.ScreenUpdating = False
  
'   Get column of active cell
    c = ActiveCell.Column
  
'   First last row in column with data
    lr = Cells(Rows.Count, "C").End(xlUp).Row
  
'   Loop through all rows backwards
    For r = lr To 2 Step -1
'       See if entry in column is numeric
        If IsNumeric(Cells(r, c)) Then
'           Get number of rows to insert
            n = Cells(r, c)
'           Insert rows
            Cells(r + 1, c).Resize(n).EntireRow.Insert Shift:=xlDown
        End If
    Next r
  
    Application.ScreenUpdating = True

End Sub
WOW WOW WOW.. It work Joe!
OMG!!!!!! You just done it in like 5 mins? Seriously? I spend hours trying to figure out.. :(

Is it normal to have the below error message? I mean the code is working but just seeing this error.



1680221882580.png
 
Upvote 0
WOW WOW WOW.. It work Joe!
OMG!!!!!! You just done it in like 5 mins? Seriously? I spend hours trying to figure out.. :(

Is it normal to have the below error message? I mean the code is working but just seeing this error.



View attachment 88744
If you hit the "Debug" button, which line of code does it highlight?

What is your first actual row with data? If it is not row 2, then change the value here to match your first data row number:
Rich (BB code):
    For r = lr To 2 Step -1

Also, things like any errors in the column we are looping through, or merged or protected cells in the data could cause issues as well.
 
Upvote 0
If you hit the "Debug" button, which line of code does it highlight?

What is your first actual row with data? If it is not row 2, then change the value here to match your first data row number:
Rich (BB code):
    For r = lr To 2 Step -1
It's done Joe! OMG.. I can't thank you enough!

You my saviour!!!!!!!!!!!!!

A thousand Thank you Joe.. :):):):)
 
Upvote 0
You are welcome.
Glad I was able to help.
 
Upvote 0
That's a crazy big help to a stranger! I can't thank you enough.. Have a nice day Sir.
You too!

Note that I updated the post marked as the solution. You typically want to mark the post that contains the solution.
That way if someone were to view the thread, they would see the original question, and then the solution right beneath it.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top