Here I dynamically take 2 values "StartRng" --> where to add the rows & "RowCount" --> No of rows to be added.
Now I have starting range as "Beta" & RowCount as 5 which is static now. I need to actually find the value "Beta" and assign this as startRng without using the inputBox which I'm using now. Then where "Beta" is present I need to add 5 rows below and focus on the first added row instead of the last.
Below is the code which I use.Is there any simple adjustments to be done so that I can get my desired output.
Sub BlankLine()
'Updateby20200123
'Author David Nithin Rajan
Dim Rng As Range
Dim WorkRng As Range
Dim StartRng As String
Dim RowCount As Integer
On Error Resume Next
xTitleId = "AddRows"
StartRng = Range("G1").Value --> use this code for dynamic value setting
RowCount = Range("I1").Value --> use this code for dynamic value setting
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", AddRows, WorkRng.Address, Type:=8)
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 LCase(Rng.Value) = LCase(StartRng) Then
Rng.Offset(0, 0).EntireRow.Offset(1).Resize(RowCount).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove_ ActiveCell.EntireRow.Copy
ActiveCell.Offset(0).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End If
If LCase(Rng.Value) <> LCase(StartRng) Then
MsgBox ("Selected cell value ( " + Rng.Value + " ) must be same as 'Insert Row After' column data (" + StartRng + ")!!!")
End If
Next
Application.ScreenUpdating = True
End Sub
I'm a novice in excel VBA. The code above is courtesy of google search and basic output oriented editions.