Enter lines below a specific line

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have code to enter new lines in a table at the bottom. I might have 5 lines already in the table and I want to enter a line/s in the middle at row 4. I can select row 4 and press add lines but it will still add the lines to the bottom. What do I need to change in my code so that I can select the row and have the lines entered below it?

VBA Code:
Sub Addlines()
    Application.EnableEvents = False
    'ActiveSheet.Unprotect Password:="CSSadmin"
    Dim ws As Worksheet, x As Long, tbl As ListObject, n As Long

        On Error GoTo cancelled:
        n = InputBox("How many lines would you like to add ?")
        Set ws = ActiveSheet
        Set tbl = ws.ListObjects("CSS_quote")
        'add 5 rows
        For x = 1 To n
            'add a row at the end of the table
            tbl.ListRows.Add
           ' ActiveSheet.ListObjects("CSS_quote").DataBodyRange.Columns(12).Value = 1 - 0.1 * ActiveSheet.chkIncrease.Value
            Range("A:A").NumberFormat = "dd/mm/yyyy"
        Next x
    tbl.Range.Cells(tbl.ListRows.Count - n + 2, 1).Select
    'ActiveSheet.Protect Password:=""
    Application.EnableEvents = True
cancelled:
    Exit Sub
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi there. Try this (you need the activecell to be in the row where you want to insert after):
VBA Code:
Sub Addlines()
    Application.EnableEvents = False
    'ActiveSheet.Unprotect Password:="CSSadmin"
    Dim ws As Worksheet, x As Long, tbl As ListObject, n As Long

        On Error GoTo cancelled:
        n = InputBox("How many lines would you like to add ?")
        Set ws = ActiveSheet
        Set tbl = ws.ListObjects("CSS_quote")
            'add n rows after the active cell
            ActiveCell.EntireRow.Offset(1).Resize(n).Insert Shift:=xlShiftDown
            Range("A:A").NumberFormat = "dd/mm/yyyy"
    tbl.Range.Cells(tbl.ListRows.Count - n + 2, 1).Select
    'ActiveSheet.Protect Password:=""
    Application.EnableEvents = True
cancelled:
    Exit Sub
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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