Is it possible to insert rows based on criteria?

isaacv22

New Member
Joined
Sep 30, 2021
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm currently looking to see if it's possible to have a spreadsheet automatically insert (x) rows below based on answering a question?

For instances while filling a cell it'll ask me if this a multi track conveyor y or n?

If Y then it'd ask how many tracks? I'd be able to input a number and it'd add that many rows below.

Is this possible?

Thanks,

Isaac Vasquez
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hello,​
yes but depending on the worksheet design …​
 
Upvote 0
We need more details on your structure.
Are these questions just cell entries, or are you envisioning pop-up questions?

If cell entries, what columns are these values being entered into?
If pop-up questions, what determines what row to insert these new rows beneath?
 
Upvote 0
SAP Autofill Ver_2.1.xlsm
ACFH
1
2ConveyorTrackMaster TagTrack Length
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Formula Sheet



So I would like to have the pop up question show up after filling a cell in the conveyor column. Which would in turn change the track column based on my answers to the question.
 
Upvote 0
How exactly would it be changing the "Track" column?
I thought you just wanted rows inserted under that cell?

Remember, while the issue is familiar to you, all we have to go on is the information that you provide.
So be as descriptive as possible. Maybe even walk us through an example with data.
 
Upvote 0
So to simplify, when I select a cell in the track column Id like a pop-up question asking if there a multiple tracks (y or n). If yes, then a pop up list would show up ranging from 2-12. Based on the number you select it'd automatically insert cells below corresponding to the number selected.
 
Upvote 0
Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this code in the resulting VB Editor window:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim chk
    Dim tracks As Long
    
'   Exit if more than one cell selected
    If Target.CountLarge > 1 Then Exit Sub
    
'   Exit if selection not made to column A after row 2
    If Target.Column <> 1 Or Target.Row < 3 Then Exit Sub

'   Prompt user for how many tracks to insert
    chk = MsgBox("Are there multiple tracks?", vbYesNo)
    If chk = vbYes Then
        On Error GoTo err_chk
        tracks = InputBox("How many tracks are there (2-12)?")
        On Error GoTo 0
        If tracks >= 2 And tracks <= 12 Then
'           Insert rows
            Application.EnableEvents = False
            Rows(Target.Row + 1 & ":" & Target.Row + tracks).Insert
            Application.EnableEvents = True
        Else
            MsgBox "You have not entered in a valid number of tracks.", vbOKOnly, "ENTRY ERROR!"
        End If
        
    End If

    Exit Sub
    
err_chk:
    MsgBox "You have not entered a valid number.", vbOKOnly, "ENTRY ERROR!"
    
End Sub
This code should run automatically whenever you select a cell in column A below row 2.
 
Upvote 0
Joe,

Thank you very much, worked out very well. I do have a a slight issue. When inserting cells it's not copying formulas on the newly inserted rows throughout the sheet. Would it be possible to just add rows in the to columns A & C for the x amount of tracks?
 
Upvote 0
Joe,

Thank you very much, worked out very well. I do have a a slight issue. When inserting cells it's not copying formulas on the newly inserted rows throughout the sheet. Would it be possible to just add rows in the to columns A & C for the x amount of tracks?
I am not quite sure I completely understand what you are saying.
If you set this up as a Data Table, when you insert new rows into the middle of that table, it should automatically copy those formulas down.
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,841
Members
449,193
Latest member
MikeVol

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