Add a row and fill it with different values based on certain variables

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I need add a row under any row that contains certain keywords, based on these keywords, the new row will be populated with different values.
(note: value in column A and B always copies from the row above)

column E contains "4'dia" and "Invert", column N does not contain "Toho", the new row should look like below:
1666744097978.png


column E contains "5'dia" and "Invert", column N does not contain "Toho", the new row should look like below:
1666744228372.png


column E contains "4'dia" and "Invert", column N contains "Toho", the new row should look like below:
1666744291933.png


The first part is working like a charm, no issues at all, but the "5'dia", and the '4'dia" with "Toho" are not adding a row to the excel file at all, any ideas?
(All the files used for testing do contain the proper keywords) any help is appreciated

code posted below

VBA Code:
Public rng2 As Range, lr4 As Long, i As Long
Sub Flowline() 
    Set rng2 = Range("A1").CurrentRegion
    lr4 = rng2.Cells(Rows.Count, "K").End(3).Row

    For i = lr4 To 2 Step -1    
        If rng2.Cells(i, 11) Like "*4'dia**Invert*" And _ 'THIS PART IS WORKING JUST FINE
           Not rng2.Cells(i, 14) Like "*Toho*" Then
            rng2.Cells(i, 11).Offset(1).EntireRow.Insert  
            rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
            Array("1", "F14050J", "Yes", "", "", "185", "Production", "500", "FLOWLINE,4' Diameter")
            rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
        If rng2.Cells(i, 11) Like "*4'dia**Invert*" And _ 'THIS PART IS NOT ADDING A ROW
            rng2.Cells(i, 14) Like "*Toho*" Then
                rng2.Cells(i, 11).Offset(1).EntireRow.Insert  
                rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
                Array("1", "F14050XJ", "Yes", "", "", "185", "Production", "500", "FLOWLINE,4' Diameter")
                rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
        ElseIf rng2.Cells(i, 11) Like "*5'dia**Invert*" And _ 'THIS PART IS NOT ADDING A ROW
               Not rng2.Cells(i, 14) Like "*Toho*" Then
                rng2.Cells(i, 11).Offset(1).EntireRow.Insert  
                rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
                Array("1", "F15050J", "Yes", "", "", "150", "Production", "500", "FLOWLINE,5' Diameter")
                rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value 
        End If
      End If
    Next i
End Sub
 

Attachments

  • 1666744105328.png
    1666744105328.png
    5.2 KB · Views: 6

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
It may be that you are never satisfying some of your "If" conditions. You could use the VBA debugger to set breakpoints at each row insert statement, then single-step (F8) from that point to observe what happens.

BTW, if you post a graphic image of your data, the number of people willing to manually type in your data to experiment to help you will be limited. Consider using this free tool instead to post some sample data.

 
Upvote 0
See if this works for you.
I think it is a simpler structure and easier to follow:

I assume you have the variables set to Public for a reason, so I haven't changed that.
VBA Code:
Sub Flowline()
    Set rng2 = Range("A1").CurrentRegion
    lr4 = rng2.Cells(Rows.Count, "K").End(3).Row
       
    For i = lr4 To 2 Step -1
        If rng2.Cells(i, 11) Like "*4'dia**Invert*" Then
            If Not rng2.Cells(i, 14) Like "*Toho*" Then
                rng2.Cells(i, 11).Offset(1).EntireRow.Insert
                rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
                    Array("1", "F14050J", "Yes", "", "", "185", "Production", "500", "FLOWLINE,4' Diameter")
                rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
            Else
                rng2.Cells(i, 11).Offset(1).EntireRow.Insert
                rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
                    Array("1", "F14050XJ", "Yes", "", "", "185", "Production", "500", "FLOWLINE,4' Diameter")
                rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
            End If
        End If
       
        If rng2.Cells(i, 11) Like "*5'dia**Invert*" And _
            Not rng2.Cells(i, 14) Like "*Toho*" Then
                rng2.Cells(i, 11).Offset(1).EntireRow.Insert
                rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
                    Array("1", "F15050J", "Yes", "", "", "150", "Production", "500", "FLOWLINE,5' Diameter")
                rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
        End If
    Next i
End Sub
 
Last edited:
Upvote 0
Solution
See if this works for you.
I think it is a simpler structure and easier to follow:

I assume you the variables set to Public for a reason, so I haven't changed that.
VBA Code:
Sub Flowline()
    Set rng2 = Range("A1").CurrentRegion
    lr4 = rng2.Cells(Rows.Count, "K").End(3).Row
       
    For i = lr4 To 2 Step -1
        If rng2.Cells(i, 11) Like "*4'dia**Invert*" Then
            If Not rng2.Cells(i, 14) Like "*Toho*" Then
                rng2.Cells(i, 11).Offset(1).EntireRow.Insert
                rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
                    Array("1", "F14050J", "Yes", "", "", "185", "Production", "500", "FLOWLINE,4' Diameter")
                rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
            Else
                rng2.Cells(i, 11).Offset(1).EntireRow.Insert
                rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
                    Array("1", "F14050XJ", "Yes", "", "", "185", "Production", "500", "FLOWLINE,4' Diameter")
                rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
            End If
        End If
       
        If rng2.Cells(i, 11) Like "*5'dia**Invert*" And _
            Not rng2.Cells(i, 14) Like "*Toho*" Then
                rng2.Cells(i, 11).Offset(1).EntireRow.Insert
                rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
                    Array("1", "F15050J", "Yes", "", "", "150", "Production", "500", "FLOWLINE,5' Diameter")
                rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
        End If
    Next i
End Sub
Alex, thank you.
I've tested this on 3 seperate excel files, it works beautifully ! very awesome !
 
Upvote 0
It may be that you are never satisfying some of your "If" conditions. You could use the VBA debugger to set breakpoints at each row insert statement, then single-step (F8) from that point to observe what happens.

BTW, if you post a graphic image of your data, the number of people willing to manually type in your data to experiment to help you will be limited. Consider using this free tool instead to post some sample data.

thank you riv for the reply, I've uploaded a test file for others to see. You can download it from the dropbox link

 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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