• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Looking to restrict EntireRow.Insert

Excelnoub

Member
Good day,

How would I change the EntireRow.Insert to only insert a new row from Column A to Column D.
Explanation:

I have to have restriction to a range (Column A to column D) as I will have more code in my Column F and up.

I have code running exactly what I need but I need to change the way my insert functions.
I have a sheet acting as some sort of table of content that can have 3 sets of layers.
I have the title, sub title, positions and sub positions. The way my sheet is set up is that the information from a userform is generated with double clicking in a cell (A3 to D1000). Within this double click, a userform pop’s up. On column select, meaning the user will have in view different information based on which column he has chosen. Column A will have the title and so on and so on. In each selection it will add the information on the next available row from A3. But, if the user selects a cell beside the main title, it will insert a new row:

Code:
Private Sub ComboBox2_Click()
ActiveCell.Offset(1).EntireRow.Insert
ActiveCell.Offset(1).Value = Me.ComboBox2
Unload Me
End Sub

I cannot use the EntireRow.Insert as in column F has effect and cannot interfere with the code from column A to D.

How can I change to only add a new row from A to D... Making it something like:

ActiveCell.Offset(1).Range(A:D).Insert
 
I have an example worksheet:

If you click A3:A1000 it will add the title to the next available row
If you click Column A beside the title in Column A, it will offset and add that title to the inserted row, so on and so on until column D...

Making this view kind of a list of Table of Content. But need to figure out to restrict the range of insert row to only Column A to Column D.
 

Attachments

  • Book1.xlsm
    29 KB · Views: 2
This should point you in the right direction.
Code:
Sub InsertCells()
Dim myRow As Long
myRow = ActiveCell.Row
'The arguments for shift can be xlShiftDown or xlShiftToRight
Range(Cells(myRow, "A"), Cells(myRow, "D")).Offset(1).Insert shift:=xlShiftDown
End Sub
 
Final code thanks to you Luke M :)

Code:
Dim myRow As Long
myRow = ActiveCell.Row
Range(Cells(myRow, "A"), Cells(myRow, "D")).Offset(1).Insert shift:=xlShiftDown
ActiveCell.Offset(1).Value = Me.ListBox2
Unload Me

Works like a charm,

Thanks again :)
 
Another approach..
Replace all
ActiveCell.Offset(1).EntireRow.Insert
with
Cells(ActiveCell.Row+1, 1).Resize(, 4).Insert
 
Back
Top