Shift cells down based on corresponding cell content

chcoder

New Member
Joined
Nov 30, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi
I'm trying to loop through my sheet and say that if the cell in row J says "segment missing doc", then shift down the corresponding lines L-S cells.
(and then recalculate the formula in column K)

I tried something like this..

sub test()

Dim Lastrow As Long
Dim cell As Long

For cell = Range("J" & Rows.Count).End(xlUp).row

If cell.Value = "segment missing doc" Then

Range("L:S").Insert Shift:=xlDown

Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K" & Lastrow)

Range("V2").Select
Selection.AutoFill Destination:=Range("V2:V" & Lastrow)


End If
Next

End sub ()


1638284230908.png
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi & welcome to MrExcel.
how about
VBA Code:
Sub test()

   Dim Lastrow As Long, i As Long

   Lastrow = Range("J" & Rows.Count).End(xlUp).Row
   For i = Lastrow To 2 Step -1
      If Range("J" & i).Value = "segment missing doc" Then
         Range("L" & i).Resize(, 8).Insert xlDown
         Lastrow = Lastrow + 1
         Range("K2").AutoFill Destination:=Range("K2:K" & Lastrow)
         Range("V2").AutoFill Destination:=Range("V2:V" & Lastrow)
      End If
   Next i
End Sub
 
Upvote 0
Solution
not sure what i'm doing wrong but nothing happens when i run this..

also this line was in my original code but is it a mistake..

Lastrow = Range("J" & Rows.Count).End(xlUp).Row

because you made another statement

Lastrow = Lastrow + 1
 
Upvote 0
not sure what i'm doing wrong but nothing happens when i run this..

also this line was in my original code but is it a mistake..

Lastrow = Range("J" & Rows.Count).End(xlUp).Row

because you made another statement

Lastrow = Lastrow + 1
sorry, actually got it to run i'll look through this.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0
So I just tried it but I don't want to insert a line, I want to shift everything down..
so I think this line needs adjusting..?
Range("L" & i).Resize(, 8).Insert xlDown
 
Upvote 0
If you want to shift everything down, then you are inserting rows.
You said
if the cell in row J says "segment missing doc", then shift down the corresponding lines L-S cells.
Is that not what you want?
 
Upvote 0
Is the code doing what you wanted?
 
Upvote 0
It worked for the first occurrence, but then for some reason in the next occurences, it just inserted a line below the line noted "segment missing doc"
also sometimes there will be a couple of "segment missing doc" in a row i would need everything shifted down until the next cell that does not say "segment missing doc.

1638290295197.png
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,674
Members
448,977
Latest member
moonlight6

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