Insert new row in table below current

ManUBlueJay

Active Member
Joined
Aug 30, 2012
Messages
302
Office Version
  1. 2016
Platform
  1. Windows
I have a table 2 cols wide like a diary with a date for every day of the year, the second Col would be for an entry.
Cell A1 is the header row named date, Cell B1 is notes , Cell A2 =Jan 1 2021, cell A3 to A366 =the cell above plus 1. Cell B2 to B366 is blank.
So A3 would be Jan 2 2021...........
All looks good.
There are days where I would like to make another entry on the same date.
I would like some code that would insert a line below the active row but keep the same date.
When I use the code "ActiveCell.EntireRow.Insert" it inserts a row below but because the table col A is the above row +1 it will put tomorrows date in vs the active cell date.
Any help would be appreciated.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Copy the column A range of dates and paste it back over itself as values.
 
Upvote 0
How do I do that dynamically.
Lets say I have put an entry in B17, I would need to copy A17:B366 and insert it just below. But my B17 entry would also be copied.
If I copy only the dates then I could be shifting some entries to the wrong dates
 
Upvote 0
What do you mean dynamically when you have a stated A2 to A366 are the formulas for the dates?

When you create this table, copy A2:A366 and paste it back over itself as values.
Now when you insert a row the date cell in the new row is blank and all the other dates have shifted down.
Isn't this what you've asked for?
 
Upvote 0
Dynamically is It needs to work wherever I am If I am in B8 it needs to insert a line in A9 where A9 now equals A8.
But it also has to owrk if I come back and add a line so I also have to copy B as you suggest.
I will now end up with a duplicate line.
I think I have come up with something that works by inserting vs copying

Sub InsertRow()

ActiveCell.Offset(1, 0).EntireRow.Insert
Range(Cells(Selection.Row + 1, 1) = Range(Cells(Selection.Row, 1).Value


End Sub
 
Upvote 0
OK
VBA Code:
Sub InsertRow()
    ActiveCell.Offset(1, 0).EntireRow.Insert
    Cells(Selection.Row + 1, 1) = Cells(Selection.Row, 1).Value
End Sub
 
Upvote 0
A variation :​
VBA Code:
Sub InsertRow()
    With Rows(ActiveCell(2).Row)
        .Insert
        .Cells(1)(0) = .Cells(1)(-1)
    End With
End Sub
 
Upvote 0
Or to deal with the table only:
VBA Code:
Sub InsertRowInTable()
    Dim TBL As Object, i As Long
Set TBL = ActiveSheet.ListObjects(1)    'first table on sheet
With TBL
    If Not Intersect(Selection, .DataBodyRange) Is Nothing Then
        i = ActiveCell.Row - .HeaderRowRange.Row
        .ListRows.Add (i + 1)
        .DataBodyRange.Cells(i + 1, 1) = .DataBodyRange.Cells(i, 1)
        .DataBodyRange.Cells(i + 1, 2).Select
    End If
End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,391
Messages
6,119,239
Members
448,879
Latest member
VanGirl

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