Hoppa till huvudinnehåll

Hur automatiskt infoga rad baserat på cellvärde i Excel?

doc-insert-row-based-on-value-1
Antag att du har en rad data och att du automatiskt vill infoga tomma rader ovanför eller under ett visst värde i Excel, till exempel automatiskt infoga rader under nollvärde enligt nedanstående skärmdump. I Excel finns det inget direkt sätt att lösa denna uppgift, men jag kan införa en makrokod för dig att automatiskt infoga rader baserat på ett visst värde i Excel.
Infoga raden nedan baserat på cellvärde med VBA

Infoga raden ovan baserat på cellvärde med Kutools för Excel bra idé3

Så här infogar du rad baserat på cellvärde genom att köra VBA:

1. Tryck Alt + F11 tangenter samtidigt, och a Microsoft Visual Basic för applikationer fönstret dyker upp.

2. klick Insert > Modulernaoch klistra sedan in under VBA-koden till poppningen Modulerna fönster.

VBA: Infoga raden nedan baserat på cellvärdet.

Sub BlankLine()
	'Updateby20150203
	Dim Rng As Range
	Dim WorkRng As Range
	On Error Resume Next
	xTitleId                   = "KutoolsforExcel"
	Set WorkRng                = Application.Selection
	Set WorkRng                = Application.InputBox("Range", xTitleId, WorkRng.Address, Type: = 8)
	Set WorkRng                = WorkRng.Columns(1)
	xLastRow                   = WorkRng.Rows.Count
	Application.ScreenUpdating = False
	For xRowIndex = xLastRow To 1 Step - 1
		Set Rng                   = WorkRng.Range("A" & xRowIndex)
		If Rng.Value = "0" Then
			Rng.Offset(1, 0).EntireRow.Insert Shift: = xlDown
		End If
	Next
	Application.ScreenUpdating = True
End Sub

3. klick F5 nyckeln eller Körning -knappen dyker en dialogruta ut och välj kolumnen innehåller noll. Se skärmdump:
doc-insert-row-based-on-value-2

4. klick OK. Därefter infogas tomma rader under nollvärdet.
doc-insert-row-based-on-value-3

Tips:

1. Om du vill infoga rader baserat på annat värde kan du ändra 0 till vilket värde du vill ha i VBA: Om Rng.Value = "0" Då.

2. Om du vill infoga rader över noll eller annat värde kan du använda nedanstående vba-kod.

VBA: Infoga rad över nollvärde:

Sub BlankLine()
	'Updateby20150203
	Dim Rng As Range
	Dim WorkRng As Range
	On Error Resume Next
	xTitleId                   = "KutoolsforExcel"
	Set WorkRng                = Application.Selection
	Set WorkRng                = Application.InputBox("Range", xTitleId, WorkRng.Address, Type: = 8)
	Set WorkRng                = WorkRng.Columns(1)
	xLastRow                   = WorkRng.Rows.Count
	Application.ScreenUpdating = False
	For xRowIndex = xLastRow To 1 Step - 1
		Set Rng                   = WorkRng.Range("A" & xRowIndex)
		If Rng.Value = "0" Then
			Rng.EntireRow.Insert Shift: = xlDown
		End If
	Next
	Application.ScreenUpdating = True
End Sub

doc-insert-row-based-on-value-4


Om du inte känner till VBA kan du prova Kutools för Excel's Välj specifika celler och sätt sedan in rader ovan.

Kutools för Excel, med mer än 300 praktiska funktioner, gör dina jobb enklare. 

När du har installerat Kutools för Excel, gör så här:(Gratis nedladdning Kutools för Excel nu!)

1. Välj listan du vill ta reda på de specifika cellerna från och klicka på Kutools > Välja > Välj specifika celler. Se skärmdump:
doc infoga rad baserat på värde 9

2. Kontrollera i poppdialogen Hela raden och välj sedan lika från Specifik typ och ange sedan det värde du vill hitta i rätt textruta. Se skärmdump:
doc infoga rad baserat på värde 6

3. klick Ok, och en dialogruta dyker upp för att påminna dig om antalet valda rader, bara stäng den.

4. Placera markören på en markerad rad och högerklicka för att välja Insert från snabbmenyn. Se skärmdump:
doc infoga rad baserat på värde 7

Nu infogas raderna ovan baserat på ett specifikt värde.
doc infoga rad baserat på värde 8


Relativa artiklar:

Bästa kontorsproduktivitetsverktyg

🤖 Kutools AI Aide: Revolutionera dataanalys baserat på: Intelligent utförande   |  Generera kod  |  Skapa anpassade formler  |  Analysera data och generera diagram  |  Anropa Kutools funktioner.
Populära funktioner: Hitta, markera eller identifiera dubbletter   |  Ta bort tomma rader   |  Kombinera kolumner eller celler utan att förlora data   |   Rund utan formel ...
Superuppslag: Flera kriterier VLookup    VLookup med flera värden  |   VSök över flera ark   |   Fuzzy Lookup ....
Avancerad rullgardinslista: Skapa snabbt en rullgardinslista   |  Beroende rullgardinslista   |  Flervals-rullgardinslista ....
Kolumnhanterare: Lägg till ett specifikt antal kolumner  |  Flytta kolumner  |  Växla synlighetsstatus för dolda kolumner  |  Jämför intervall och kolumner ...
Utvalda funktioner: Rutnätsfokus   |  Designvy   |   Stor formelbar    Arbetsbok & Bladhanterare   |  Resursbibliotek (Automatisk text)   |  Datumväljare   |  Kombinera arbetsblad   |  Kryptera/Dekryptera celler    Skicka e-postmeddelanden efter lista   |  Superfilter   |   Specialfilter (filtrera fet/kursiv/genomstruken...) ...
Topp 15 verktygssatser12 text verktyg (lägga till text, Ta bort tecken, ...)   |   50+ Diagram Typer (Gantt Chart, ...)   |   40+ Praktiskt Formler (Beräkna ålder baserat på födelsedag, ...)   |   19 Införande verktyg (Infoga QR-kod, Infoga bild från sökväg, ...)   |   12 Konvertering verktyg (Siffror till ord, Valutaväxling, ...)   |   7 Slå ihop och dela verktyg (Avancerade kombinera rader, Dela celler, ...)   |   ... och mer

Uppgradera dina Excel-färdigheter med Kutools för Excel och upplev effektivitet som aldrig förr. Kutools för Excel erbjuder över 300 avancerade funktioner för att öka produktiviteten och spara tid.  Klicka här för att få den funktion du behöver mest...

Beskrivning


Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50 % och minskar hundratals musklick för dig varje dag!
Comments (43)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hola mira necesito agregar filas en blanco automaticamente cuando se me produce desbordamiento en diferentes filas y probe varias formas y no lo toma.
me podrias ayudar?desde ya gracias
This comment was minimized by the moderator on the site
need help with a code. I am trying insert rows based on value in another cell on another worksheet (same workbook). then automatically update that number if blank cell if filled. so basically worksheet 1 has, column A all time starts from 4 am to 10 pm. Column B is employee name. Worksheet 2 has time starts in column A and available shifts for that time start in column B. I need to find a way to automatically insert blank row at last time start ie 4 am in worksheet 1 if worksheet 2 has ie. 1 available shift to pick up for 4 am. then update the number in worksheet 2 if the cell if fill at a later time. Is this possible?
This comment was minimized by the moderator on the site
Gracias por tu macro, he utilizado la primera y me funciona perfectamente.
Necesitaría a la vez de generar una fila en blanco debajo de otra dependiendo de un valor, que esa linea se copie en la que se ha generado en blanco.
Me seria de gran ayuda.
This comment was minimized by the moderator on the site
Can anyone suggest how to do this with adding 1 row for any number grater than 0?
This comment was minimized by the moderator on the site
Hi, Jordan, change the script: If Rng.Value = "0" Then to If Rng.Value > "0" Then
in the vba of the article.
This comment was minimized by the moderator on the site
Instead of a dialogue box that pops up asking which cells it should look at, how can I instead just require a preset range? In this case I want it to always run it for E3 to E800. Thanks!
This comment was minimized by the moderator on the site
Hi, Christopher M, try below code, it take A1:A10 as an example, just change it as you need.
Sub BlankLine()
'ByExtendoffice
Dim Rng As Range

Dim WorkRng As Range

Dim xRngI As Range

On Error Resume Next

xTitleId = "KutoolsforExcel"

Set WorkRng = Application.Range("A1:A10") 'Change the range as you need

Set WorkRng = WorkRng.Columns(1)

xLastRow = WorkRng.Rows.Count

Application.ScreenUpdating = False

For xRowIndex = xLastRow To 1 Step -1

  Set Rng = WorkRng.Range("A" & xRowIndex)

  If Rng.Value = "Node1" Then

    Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown

    Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown

    Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown

    Rng.Offset(1, 0).Value = "Scanner"

    Rng.Offset(2, 0).Value = "Printer"

    Rng.Offset(3, 0).Value = "CD"

  End If

Next

Application.ScreenUpdating = True

End Sub
This comment was minimized by the moderator on the site
I need to add particular rows with values in them for specific cell contents, but not sure how to do so without having to manually do for over 3800+ lines

Ex: A1 = Node1
I need to have a row inserted and it to enter the value Scanner
Enter another row and enter the value Printer
another row with value CD.
Etc
This comment was minimized by the moderator on the site
Hi, Chris, here is a VBA can help you to automatically insert three rows (Scanner, Printer, CD) when the value equal to Node1.
Sub BlankLine()
'ByExtendoffice
Dim Rng As Range

Dim WorkRng As Range

Dim xRngI As Range

On Error Resume Next

xTitleId = "KutoolsforExcel"

Set WorkRng = Application.Selection

Set WorkRng = Application.InputBox("Select a range", xTitleId, WorkRng.Address, Type:=8)

Set WorkRng = WorkRng.Columns(1)

xLastRow = WorkRng.Rows.Count

Application.ScreenUpdating = False

For xRowIndex = xLastRow To 1 Step -1

  Set Rng = WorkRng.Range("A" & xRowIndex)

  If Rng.Value = "Node1" Then

    Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown

    Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown

    Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown

    Rng.Offset(1, 0).Value = "Scanner"

    Rng.Offset(2, 0).Value = "Printer"

    Rng.Offset(3, 0).Value = "CD"

  End If

Next

Application.ScreenUpdating = True

End Sub

Please let me know if works for you.
This comment was minimized by the moderator on the site
Hi,
Can this marco be used/altered for colored cells?
I need to insert at least 10 rows above end of the each series which are colored.
Thx!
This comment was minimized by the moderator on the site
please help mei have a data.i have one month time data, in that i have to insert a empty rows according to the escape time
This comment was minimized by the moderator on the site
Hi, hr.babu08, sorry the reply is late. I guess you want to insert blank rows or make a mark for the missing sequence data, if so, you can try Kutools for Excel's Find Missing Sequence Number feature.Here is the tutorial about the feature: https://www.extendoffice.com/product/kutools-for-excel/excel-find-missing-numbers-in-sequence.htmlIf you want other methods on inserting blank rows for missing sequence, please visit:https://www.extendoffice.com/documents/excel/3522-excel-find-missing-dates.html</div>;
This comment was minimized by the moderator on the site
Hello. The macro works for me but is there a way to always have the range/parameters set always to column J without the input box coming up at all? I would like it to skip the step of the input box coming up. Also, I have made sure that directly before this macro that the last line of my previous macro is Range("J:J").Select to make sure the whole J column is already selected.
This is what I have been using so far.

Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "Click OK to Continue"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set WorkRng = WorkRng.Columns(1)
SendKeys "~"
xLastRow = WorkRng.Rows.Count
Application.ScreenUpdating = False
For xRowIndex = xLastRow To 1 Step -1
Set Rng = WorkRng.Range("A" & xRowIndex)
If Rng.Value = "New GMS Line" Then
Rng.EntireRow.Insert Shift:=xlDown
End If
Next


I Have tried experimenting using the SendKeys "~" command between some of the steps to try and get it to press enter automatically when the input box comes up but that hasn't worked either. I wasn't sure where exactly to use the SendKeys command in the macro or if it would even work with an input box!
This comment was minimized by the moderator on the site
Many thanks for this, really a tremendous time saver. Would it be possible to add some code which allows me to insert some text in the new row. For example i'm inserting new rows based on target value 'x', then i want to add text value 'y' in the cell below target value 'x'. Is this possible?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations