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
Insert new row depending on condition
-
- 2StarLounger
- Posts: 153
- Joined: 31 Jan 2021, 09:12
Insert new row depending on condition
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 12577
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Insert new row depending on condition
It would be much better to describe your issue in the post, rather than in an attached word document
StuartR
-
- 2StarLounger
- Posts: 153
- Joined: 31 Jan 2021, 09:12
Re: Insert new row depending on condition
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.
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.
-
- Administrator
- Posts: 78235
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Insert new row depending on condition
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
Hans
-
- 2StarLounger
- Posts: 153
- Joined: 31 Jan 2021, 09:12
Re: Insert new row depending on condition
Great Hans
Thanks again
You are star
Thanks again
You are star