+ Reply to Thread
Results 1 to 6 of 6

Insert rows for multiple selected rows

Hybrid View

  1. #1
    Registered User
    Join Date
    05-24-2018
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    3

    Insert rows for multiple selected rows

    Hello all,

    Can someone modify this code? It does not insert the specified rows in the input box for each selected row, just for one.

    Sub test()
      Dim rowcount As Integer
      zeilenanzahl = InputBox("how many rows?")
      Range(ActiveCell, ActiveCell.Offset((rowcount - 1), 0)).EntireRow.Insert
      Cancel = True
    End Sub
    Best,
    JN
    Last edited by jeffreybrown; 05-25-2018 at 05:06 PM. Reason: Please use code tags

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,301

    Re: Insert rows for multiple selected rows

    Quote Originally Posted by JN12345 View Post
    1. ... for each selected row ...
    2. Dim rowcount As Integer
    3. zeilenanzahl = InputBox ...
    4. Cancel = True
    1. Do you want to insert rows in the selected range, or starting from the active cell ? These are two different things.
    If you select the range from "A4" to "A1" the active cell will be "A4", if from "A1" to "A4" the active cell will be "A1".
    2. Where is "rowcount" calculated ? No calculations.
    3. Where is the "zeilenanzahl" declaration and what for is it ?
    4. Where is it from ?

    Try:
    (rows will be inserted starting from the active cell - two variants)
    Option Explicit
    
    Sub test()
    Dim riadok As Long
    
        On Error Resume Next
        riadok = Abs(CLng(InputBox("Number of rows", "how many rows ?", "1")))
        If Err.Number <> 0 Then MsgBox "An error occured": Exit Sub
        On Error GoTo 0
        'Rows(ActiveCell.Row + 1 & ":" & ActiveCell.Row + riadok).Insert Shift:=xlDown
        'or
        Rows(ActiveCell.Row & ":" & ActiveCell.Row + riadok - 1).Insert Shift:=xlDown
    End Sub

  3. #3
    Registered User
    Join Date
    05-24-2018
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    3

    Re: Insert rows for multiple selected rows

    Hello Porucha,

    Thank you for your reply. I really appreciate it. I'll try your code asap.

    Unfortunately, when I translated the code I made a typo, sorry for that. The code looks actually like this:

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
      Dim rowcount As Integer
      rowcount = InputBox("how many rows?")
      Range(ActiveCell, ActiveCell.Offset((rowcount - 1), 0)).EntireRow.Insert
      Cancel = True
    End Sub
    This code inserts the specified amount of rows after a selected cell. It works perfectly fine. However, I need a bit something else.
    1. I would like to select several cells in column A at the same time (strg + select). Those cells can be anywhere in column A, e.g. A21, A34, A45 and so on
    2. After selecting the cells I would like to insert after each selected cell a specific amount of rows. The number of rows I would put into the message box. As mentioned before, with the code above I can only insert rows after one selected cell, which would make my doing very time consuming. I read that "activecell" can only be just one row. But is there a workaround?

    Let me know if you need further clarifications.

    Best,
    JN
    Last edited by JN12345; 05-24-2018 at 04:59 PM. Reason: Typo

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,301

    Re: Insert rows for multiple selected rows

    Quote Originally Posted by JN12345 View Post
    1a. ...Worksheet_BeforeDoubleClick ...
    1b. ... to select several cells ... at the same time ...
    2. After selecting the cells... to insert after ... selected cell a specific amount of rows ...
    1a,b.
    How do you want to do this with this procedure ?
    If you select some cells, two ... three ... four cells etc. and then you double-click, then you haven't multiple-selected cells but only one-selected cell, the one you double click. One action excludes this other action.

    2. Inserting rows starting from the first cell under the active cell:
        Rows(ActiveCell.Row + 1 & ":" & ActiveCell.Row + rows_from_InputBox).Insert Shift:=xlDown

  5. #5
    Registered User
    Join Date
    05-24-2018
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    3

    Re: Insert rows for multiple selected rows

    Ok, thank you. I understand that it only works with one selected cell. But is there a workaround to insert the cells right after all selected cells?

    In the example below I would like to insert the rows I specified in the input box for all highlighted cells and not only for "UT20". Is this possible with vba?

    Untitled.png

  6. #6
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,301

    Re: Insert rows for multiple selected rows

    Using the menu from the right mouse button:

    1. ThisWorkbook module
    Option Explicit
    
    Private Sub Workbook_Deactivate()
    Dim ctrl As CommandBarControl
    
        On Error Resume Next
        For Each ctrl In Application.CommandBars("Cell").Controls
            If ctrl.Tag = "Delete_after" Then ctrl.Delete
        Next
    End Sub
     
    Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Dim ctrl As CommandBarControl
    Dim myPopUp As CommandBarButton
    
        If TypeName(Sh) = "Worksheet" Then
            On Error Resume Next
            For Each ctrl In Application.CommandBars("Cell").Controls
                If ctrl.Tag = "Delete_after" Then ctrl.Delete
            Next
            On Error GoTo the_end
            Set myPopUp = Application.CommandBars("Cell").Controls.Add(Type:=msoControlButton, Before:=1, Temporary:=True)
            With myPopUp
                .Caption = "Inserting rows in selection areas"
                .OnAction = "insert_rows"
                .FaceId = 133
                .Tag = "Delete_after"
            End With
            Set myPopUp = Nothing
            Application.CommandBars("Cell").Controls(2).BeginGroup = True
        End If
        
    Exit Sub
    the_end: MsgBox "Error - End", vbOKOnly, "Info"
    End Sub
    2. Standard module, Module1:
    Option Explicit
    Option Private Module
    
    Sub insert_rows()
    Dim nmbrrws As Long, i As Long, nmbrArea As Long
    
        On Error Resume Next
        nmbrrws = Abs(CLng(InputBox("Enter the number of rows:", "How many rows to add ?", "1")))
        If Err.Number <> 0 Then MsgBox "An error occured", vbOKOnly, "Info": Exit Sub
        On Error GoTo the_end
        
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.Calculation = xlManual
        
        nmbrArea = Selection.Areas.Count
        
        For i = nmbrArea To 1 Step -1
            Rows(Selection.Areas(i).Cells(1).Row + 1 & ":" & Selection.Areas(i).Cells(1).Row + nmbrrws).Insert Shift:=xlDown
        Next
        
        Application.Calculation = xlAutomatic
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        
    Exit Sub
    the_end: MsgBox "Error - End", vbOKOnly, "Info"
    End Sub
    Attached Images Attached Images

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Insert a value into a certain column of selected rows
    By machinerydlr in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 02-25-2017, 08:18 PM
  2. Insert multiple rows each specific number of rows
    By salmasaied in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 10-24-2016, 09:58 AM
  3. [SOLVED] Insert multiple rows each specific number of rows
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 05-21-2016, 10:36 AM
  4. Return indexes of rows when multiple rows of a table are selected.
    By samkumar in forum Word Programming / VBA / Macros
    Replies: 0
    Last Post: 08-12-2013, 04:54 AM
  5. Code to insert rows between selected cells
    By DeepakS in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-07-2013, 08:18 AM
  6. Replies: 0
    Last Post: 03-06-2013, 05:44 PM
  7. [SOLVED] Insert Multiple Rows Based Off Number in Cell and Copy Data From Above New Rows
    By tstell1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-02-2012, 04:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1