Macro duplicating results

ddasilva

New Member
Joined
Feb 27, 2019
Messages
8
Hi All,

I wrote this macro but it seems to be duplicating the data. Could I get another pair of eyes to help me find the problem please? Thank you.

The output in column B is correct.
The output in column C is correct.
Column D is a duplicate of column C and what should be in Column D ends up in Column I.
Column E is correct but F through H are duplicates.
Column J should be in column F.


Example
A 1/2/2019 1/2/2019 424046 424046 1/2/2019 A 230924 $1,044.10
B 1/3/2019 1/3/2019 424089 424089 1/3/2019 B 230936 $717.13
C 1/3/2019 1/3/2019 424135 424135 1/3/2019 C 230937 $1,489.11


Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub RangeCopyPaste()
Dim cell As Range
Dim NewRange As Range
Dim MyCount As Long
MyCount = 1

For Each cell In Worksheets("GL").Range("D1:D2000")
    If cell.Value = "Computer Checks" Then
        If MyCount = 1 Then Set NewRange = cell.Offset(0, 1)
        Set NewRange = Application.Union(NewRange, cell.Offset(0, 1))
        MyCount = MyCount + 1
    End If
Next cell

'--> Copy NewRange from inactive sheet into active sheet
NewRange.Copy Destination:=ActiveSheet.Range("B3")

'--> Remove Duplicates
ActiveSheet.Range("B3:B2000").RemoveDuplicates

For Each cell In Worksheets("GL").Range("D1:D2000")
    If cell.Value = "Computer Checks" Then
        If MyCount = 1 Then Set NewRange = cell.Offset(0, -2)
        Set NewRange = Application.Union(NewRange, cell.Offset(0, -2))
        MyCount = MyCount + 1
    End If
Next cell

'--> Copy NewRange from inactive sheet into active sheet
NewRange.Copy Destination:=ActiveSheet.Range("C3")

'--> Remove Duplicates
ActiveSheet.Range("C3:C2000").RemoveDuplicates

For Each cell In Worksheets("GL").Range("D1:D2000")
    If cell.Value = "Computer Checks" Then
        If MyCount = 1 Then Set NewRange = cell.Offset(0, 2)
        Set NewRange = Application.Union(NewRange, cell.Offset(0, 2))
        MyCount = MyCount + 1
    End If
Next cell

'--> Copy NewRange from inactive sheet into active sheet
NewRange.Copy Destination:=ActiveSheet.Range("D3")

'--> Remove Duplicates
ActiveSheet.Range("D3:D2000").RemoveDuplicates

For Each cell In Worksheets("GL").Range("D1:D2000")
    If cell.Value = "Computer Checks" Then
        If MyCount = 1 Then Set NewRange = cell.Offset(0, -3)
        Set NewRange = Application.Union(NewRange, cell.Offset(0, -3))
        MyCount = MyCount + 1
    End If
Next cell

'--> Copy NewRange from inactive sheet into active sheet
NewRange.Copy Destination:=ActiveSheet.Range("E3")

'--> Remove Duplicates
ActiveSheet.Range("E3:E2000").RemoveDuplicates

For Each cell In Worksheets("GL").Range("D1:D2000")
    If cell.Value = "Computer Checks" Then
        If MyCount = 1 Then Set NewRange = cell.Offset(0, 4)
        Set NewRange = Application.Union(NewRange, cell.Offset(0, 4))
        MyCount = MyCount + 1
    End If
Next cell

'--> Copy NewRange from inactive sheet into active sheet
NewRange.Copy Destination:=ActiveSheet.Range("F3")

'--> Remove Duplicates
ActiveSheet.Range("F3:2000").RemoveDuplicates

End Sub</code>
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try adding
Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">MyCount = 1</code>
before each loop.
 
Upvote 0
Cross posted https://www.excelforum.com/excel-programming-vba-macros/1266625-vba-macro-duplicating-results.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,214,531
Messages
6,120,073
Members
448,943
Latest member
sharmarick

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