Duplicating and amending rows in VBA based on text in a column

K0D54

New Member
Joined
Aug 28, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hey everyone! Does anyone know how I would search a column for a particular value and duplicate the row in which the value is found in VBA? I also want to amend a cell in the duplicated row if possible. I would like to take the top table and make it look like the bottom table in the included example. I want to duplicate any row with salary and add “- Other” to the employee’s name.



I tried doing something like this:

For i = 11 To lastrow
ws.Select
If UCase(Cells(i, "C")) = "SALARY" And UCase(Cells(i, "A")) <> "" Then
Range(("A" & i) & ":" & ("R" & i)).Select
Range(("A" & i) & ":" & ("R" & i)).Copy
Selection.Insert Shift:=xlDown

However, it created a never-ending loop, and I am unsure how to amend the cell names for a duplicate row. Any help would be appreciated. Thank you!
 

Attachments

  • Screenshot 2022-08-25 184100.png
    Screenshot 2022-08-25 184100.png
    28.5 KB · Views: 7

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi,
The following code is written based on the data on your screenshot.

VBA Code:
Sub Sample()
    Dim i As Long
    For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        If UCase(Cells(i, "B").Value) = "SALARY" And UCase(Cells(i, "A").Value) <> "" Then
            Rows(i).Copy
            Rows(i + 1).Insert Shift:=xlDown
            Cells(i + 1, "A").Value = Cells(i, "A").Value & " - Other"
        End If
    Next
    Application.CutCopyMode = False
End Sub
End Sub
 
Upvote 0
Solution
Hi,
The following code is written based on the data on your screenshot.

VBA Code:
Sub Sample()
    Dim i As Long
    For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        If UCase(Cells(i, "B").Value) = "SALARY" And UCase(Cells(i, "A").Value) <> "" Then
            Rows(i).Copy
            Rows(i + 1).Insert Shift:=xlDown
            Cells(i + 1, "A").Value = Cells(i, "A").Value & " - Other"
        End If
    Next
    Application.CutCopyMode = False
End Sub
End Sub
This worked perfectly. Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,044
Messages
6,122,827
Members
449,096
Latest member
Erald

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