Change duplicates with VBA

Cinsault

New Member
Joined
Apr 30, 2018
Messages
2
dh48uH
Hi all!

In excel I have created a standard order template that should be used to create standard read-in files. I have managed to set up the standard read-in file with VBA, but I am stuck on one of the steps that must be taken in the preparation of the data.

Sample: https://ibb.co/dh48uH
dh48uH


From line 8, columns A through E will always be filled. However, the number of lines differs per file. What I try to achieve through VBA is:

  1. All lines that have the text "A + B" in column C must be duplicated.
  2. Of all duplicated lines, the text "A + B" in column C should be replaced with "B".
  3. Of the remaining (actually original) lines with the text "A + B" in column C, it should be replaced with "A".
The end result would then be that in column C only the values ​​A or B occur.

I, as a beginner in VBA, have succeeded in the first step (although probably clumsy in the eyes of experts) but have no idea how to proceed. The things I tried based on google/YouTube doesn't work. I really hope someone can help me with this.

Code:
Sub CreateDuplicates()

    a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    For i = 8 To a

    If Worksheets("Sheet1").Cells(i, 3).Value = "A + B" Then
        Worksheets("Sheet1").Rows(i).Copy
        Worksheets("Sheet1").Activate
        a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
        Worksheets("Sheet1").Cells(a + 1, 1).Select
        ActiveSheet.Paste
        Worksheets("Sheet1").Activate
    End If
    Next i

Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub

Thanks in advance for your time & help!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi & welcome to MrExcel.
How about
Code:
Sub CreateDuplicates()
   Dim a As Long, i As Long
   a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
   With Worksheets("Sheet1")
      For i = a To 8 Step -1
         If .Cells(i, 3).Value = "A + B" Then
            .Rows(i).Copy
            .Rows(i).Insert
            .Cells(i, 3).Value = "A"
            .Cells(i + 1, 3).Value = "B"
         End If
      Next i
   End With

Application.CutCopyMode = False
ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub
 
Upvote 0
Hi Fluff,


I immediately tried the code and it seems to work perfectly. Thank you so much for the quick response. I'm going to take the time to understand the code step by step - I enjoy working with vba. There's so much to learn!
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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