Insert multiple rows with fill number in a specific row

Unexpc

Active Member
Joined
Nov 12, 2020
Messages
496
Office Version
  1. 2019
Platform
  1. Windows
Hi guys
i have several customer in a sheet that have separator (a gray row) between every customer, now, when i write a number in this rows, insert rows up with that amount
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
First, place this code in a standard module:
VBA Code:
Sub CheckInteriorColourIndex()
    MsgBox Selection.Interior.ColorIndex
End Sub
Then select a separator cell coloured in grey and run the macro.
Colour index will pop up, so remember that number.

Then try the code below in a sheet module (replace the number 15 with the number you saw in the message box):
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Interior.ColorIndex = 15 And Not IsEmpty(Target) Then
        On Error GoTo errHandler
        Rows(Target.Row & ":" & Target.Row + CInt(Target) - 1).Insert Shift:=xlDown
        On Error GoTo 0
    End If
Exit Sub
errHandler:
    MsgBox "Row insertion failed: Number invalid", vbExclamation, "Error"
End Sub
 
Upvote 0
First, place this code in a standard module:
VBA Code:
Sub CheckInteriorColourIndex()
    MsgBox Selection.Interior.ColorIndex
End Sub
Then select a separator cell coloured in grey and run the macro.
Colour index will pop up, so remember that number.

Then try the code below in a sheet module (replace the number 15 with the number you saw in the message box):
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Interior.ColorIndex = 15 And Not IsEmpty(Target) Then
        On Error GoTo errHandler
        Rows(Target.Row & ":" & Target.Row + CInt(Target) - 1).Insert Shift:=xlDown
        On Error GoTo 0
    End If
Exit Sub
errHandler:
    MsgBox "Row insertion failed: Number invalid", vbExclamation, "Error"
End Sub
i test it, when i run fisrt code, that is show break mode error, and how does it work?
 
Upvote 0
You have to put it into a standard module.
one question, i don't know about that, but i first insert first code at module 1 and run that macro and show error, after that i copy/paste second code in that module and run macro but not doing anything, anyway doing automatically macro?(i write for example number 2 in gray row and after that insert two rows in up of that row)
 
Upvote 0
What is column name that you input Number? A, B or ....
 
Upvote 0
Combined Code for Work Sheet with Previous Worksheet Change Event isThis:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Lr1 As Long, Lr2 As Long, Cr As Long, Lr3 As Long, Lr4 As Long, K As Long, CrR As String
Lr1 = Sheets("Dashboard").Range("I" & Rows.Count).End(xlUp).Row
Lr2 = Sheets("Dashboard").Range("N" & Rows.Count).End(xlUp).Row
Lr3 = Sheets("Work").Range("A" & Rows.Count).End(xlUp).Row
Lr4 = Sheets("Paper").Range("A" & Rows.Count).End(xlUp).Row

If Intersect(Target, Union(Range("A1:A" & Lr3), Range("E3:E1048576,G3:G1048576"))) Is Nothing Then Exit Sub
If Not Intersect(Target, Range("E3:E1048576,G3:G1048576")) Is Nothing Then
If Target.Count = 1 Then
If Target.Value > 0 Then Target = Target.Value * -1
End If
End If
On Error Resume Next
If Intersect(Target, Range("A1:A" & Lr3)) Is Nothing Then Exit Sub
Application.EnableEvents = False
If Target.Interior.Color = 14277081 And Target.Value <> "" Then
Range("A" & Target.Row & ":G" & Target.Row + Target.Value).Insert Shift:=xlDown
Target.Value = ""
End If
  For i = 3 To Lr1 - 1
    Cr = Application.WorksheetFunction.Match(Sheets("Dashboard").Range("I" & i).Value, Sheets("Work").Range("A1:A" & Lr3), 0)
    CrR = Range("A" & Cr).Address
    Sheets("Dashboard").Range("I" & i).Hyperlinks.Delete
    Sheets("Dashboard").Hyperlinks.Add Anchor:=Sheets("Dashboard").Range("I" & i), Address:="", SubAddress:="'" & Sheets("Work").Name & "'!" & CrR, TextToDisplay:=Sheets("Dashboard").Range("I" & i).Value
       With Sheets("Dashboard").Range("I" & i).Font
                .Underline = xlUnderlineStyleNone
                .ColorIndex = xlColorIndexAutomatic
                .Name = "Arial"
                .Size = 14
     End With
Next i
Application.EnableEvents = True
End Sub
 
Upvote 0
Combined Code for Work Sheet with Previous Worksheet Change Event isThis:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Lr1 As Long, Lr2 As Long, Cr As Long, Lr3 As Long, Lr4 As Long, K As Long, CrR As String
Lr1 = Sheets("Dashboard").Range("I" & Rows.Count).End(xlUp).Row
Lr2 = Sheets("Dashboard").Range("N" & Rows.Count).End(xlUp).Row
Lr3 = Sheets("Work").Range("A" & Rows.Count).End(xlUp).Row
Lr4 = Sheets("Paper").Range("A" & Rows.Count).End(xlUp).Row

If Intersect(Target, Union(Range("A1:A" & Lr3), Range("E3:E1048576,G3:G1048576"))) Is Nothing Then Exit Sub
If Not Intersect(Target, Range("E3:E1048576,G3:G1048576")) Is Nothing Then
If Target.Count = 1 Then
If Target.Value > 0 Then Target = Target.Value * -1
End If
End If
On Error Resume Next
If Intersect(Target, Range("A1:A" & Lr3)) Is Nothing Then Exit Sub
Application.EnableEvents = False
If Target.Interior.Color = 14277081 And Target.Value <> "" Then
Range("A" & Target.Row & ":G" & Target.Row + Target.Value).Insert Shift:=xlDown
Target.Value = ""
End If
  For i = 3 To Lr1 - 1
    Cr = Application.WorksheetFunction.Match(Sheets("Dashboard").Range("I" & i).Value, Sheets("Work").Range("A1:A" & Lr3), 0)
    CrR = Range("A" & Cr).Address
    Sheets("Dashboard").Range("I" & i).Hyperlinks.Delete
    Sheets("Dashboard").Hyperlinks.Add Anchor:=Sheets("Dashboard").Range("I" & i), Address:="", SubAddress:="'" & Sheets("Work").Name & "'!" & CrR, TextToDisplay:=Sheets("Dashboard").Range("I" & i).Value
       With Sheets("Dashboard").Range("I" & i).Font
                .Underline = xlUnderlineStyleNone
                .ColorIndex = xlColorIndexAutomatic
                .Name = "Arial"
                .Size = 14
     End With
Next i
Application.EnableEvents = True
End Sub
yes, thats it, but two problem
1.this is add 1 more row than number i write
2.when i delete a count multiple rows, delete that count multiple data below of last delete multiple rows
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, j As Long, Lr1 As Long, Lr2 As Long, Cr As Long, CrR As String
Dim Lr3 As Long, Lr4 As Long, K As Long
Lr1 = Sheets("Dashboard").Range("I" & Rows.Count).End(xlUp).Row
Lr2 = Sheets("Dashboard").Range("N" & Rows.Count).End(xlUp).Row
Lr3 = Sheets("Work").Range("A" & Rows.Count).End(xlUp).Row
Lr4 = Sheets("Paper").Range("A" & Rows.Count).End(xlUp).Row

  If Intersect(Target, Union(Range("A1:A" & Lr3), Range("E3:E1048576,G3:G1048576"))) Is Nothing Then Exit Sub
   If Not Intersect(Target, Range("E3:E1048576,G3:G1048576")) Is Nothing Then
   If Target.Count = 1 Then
   If Target.Value > 0 Then Target = Target.Value * -1
   End If
   End If
   On Error Resume Next
  If Intersect(Target, Range("A1:A" & Lr3)) Is Nothing Then Exit Sub
  Application.EnableEvents = False
  If Target.Interior.Color = 14277081 Then
  If Target.Value <> "" Then
  Range("A" & Target.Row & ":G" & Target.Row + Target.Value - 1).Insert Shift:=xlDown
  Target.Value = ""
  End If
  End If
  For i = 3 To Lr1 - 1
   Cr = Application.WorksheetFunction.Match(Sheets("Dashboard").Range("I" & i).Value, Sheets("Work").Range("A1:A" & Lr3), 0)
   CrR = Range("A" & Cr).Address
     Sheets("Dashboard").Range("I" & i).Hyperlinks.Delete
     Sheets("Dashboard").Hyperlinks.Add Anchor:=Sheets("Dashboard").Range("I" & i), Address:="", SubAddress:="'" & Sheets("Work").Name & "'!" & CrR, TextToDisplay:=Sheets("Dashboard").Range("I" & i).Value
     With Sheets("Dashboard").Range("I" & i)
                .Font.Underline = xlUnderlineStyleNone
                .Font.ColorIndex = xlColorIndexAutomatic
                .Font.Name = "Arial"
                .Font.Size = 14
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
     End With
  Next i
  Application.EnableEvents = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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