Insert new row depending on condition

jimpatel1993
2StarLounger
Posts: 153
Joined: 31 Jan 2021, 09:12

Insert new row depending on condition

Post by jimpatel1993 »

Hi
First of all really thanks for this forum as this is one of the best forum I have ever used.
Thanks for solving queries.
I have attached my query in this attachment.
Any help will be appreciated

Thanks
You do not have the required permissions to view the files attached to this post.

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Insert new row depending on condition

Post by StuartR »

It would be much better to describe your issue in the post, rather than in an attached word document
StuartR


jimpatel1993
2StarLounger
Posts: 153
Joined: 31 Jan 2021, 09:12

Re: Insert new row depending on condition

Post by jimpatel1993 »

Hi,
Thanks a lot for looking at my post.
Greatly appreciate your help in this topic ☺
Special thanks to Hans for sorting out my previous post.
I have workbook called source with sheets called “sheet1” and “sheet2”
In sheet1, if there is anything called “Class 2” from column C then I need to check if the same row UniqueID is available in the previous row. If yes then don’t do anything, if no then insert new row above class 2 with same unique ID. Example is provided in “sheet2”

Example: Row 5 have uniqueID as 23 and class2. Row 4 does not contain same unique id and therefore new row will be inserted in row 4 with unique id as 23. (Example in sheet 2)
Secondary example, row 20 in sheet 1 have unique id 12 with class2 and one above that row have same unique id as 12 , therefore don’t do anything condition.

Any idea please?

Really thanks and appreciate your effort.

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert new row depending on condition

Post by HansV »

Here you go:

Code: Select all

Sub InsertRows()
    Dim wss As Worksheet
    Dim s As Long
    Dim m As Long
    Application.ScreenUpdating = False
    Set wss = Worksheets("Sheet1")
    m = wss.Range("A" & wss.Rows.Count).End(xlUp).Row
    For s = m To 2 Step -1
        If wss.Range("C" & s).Value = "Class 2" Then
            If wss.Range("A" & s - 1).Value <> wss.Range("A" & s).Value Then
                wss.Range("A" & s).EntireRow.Insert
                wss.Range("A" & s).Value = wss.Range("A" & s + 1).Value
            End If
        End If
    Next s
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

jimpatel1993
2StarLounger
Posts: 153
Joined: 31 Jan 2021, 09:12

Re: Insert new row depending on condition

Post by jimpatel1993 »

Great Hans

Thanks again
You are star