Rymare
New Member
- Joined
- Apr 20, 2018
- Messages
- 37
I have a macro that runs through my source sheet "CleanData" and copies any row in that sheet to the active sheet provided the active sheet's name is in column A. It skips over rows that do not meet this criteria. So far, so good. The problem I'm having is that when you run the macro again it does not recheck the active sheet to see if the same row is already recorded. So it gives me duplicates.
I'm using fake data as the actual data is protected info, but needless to say the list under the Inspector heading is extremely long which is why I'm trying to avoid fixed ranges (like "A5:A32" etc) and fixed criteria (having the macro say things like
for example would cause problems because what if a new inspector gets added on and his name is not Harry? Someone has to go in and change the macro each time a new inspector is added. Instead I use things like
since each inspector gets his/her own sheet).
Here's the flow
You run the macro from the inspectors sheet, in this example, the Sheet named Harry. Harry comes in and runs the macro to see what new work he needs to do:
So far so good:
Well, what if Harry comes in one day and wants to see if Voldemort has given him new work? He needs to update his sheet again to check right?
So he runs the macro again, but now the same work orders are getting copied in with the new ones, and he doesn't need the same info twice:
Here's the code (which was hobbled together from different places--I think even from here--and I made changes) :
I tried to filter out the duplicates using:
in this line of code:
And I tried to define rowname as a range of all previous rows in the sheet. It did not work.
Based on a suggestion from stackoverflow I made this:
It also did not work.
I initially had formulas within each sheets' cells that gathered all this data but it severely slowed down an already bogged down excel file.
Here's a link to where I asked this question on stackoverflow
https://stackoverflow.com/questions/49947378/macro-to-skip-rows-already-copied
I'm using fake data as the actual data is protected info, but needless to say the list under the Inspector heading is extremely long which is why I'm trying to avoid fixed ranges (like "A5:A32" etc) and fixed criteria (having the macro say things like
Code:
rngcel.value = "Harry"
Code:
rngcel.value = Activesheet.name
Here's the flow
You run the macro from the inspectors sheet, in this example, the Sheet named Harry. Harry comes in and runs the macro to see what new work he needs to do:
So far so good:
Well, what if Harry comes in one day and wants to see if Voldemort has given him new work? He needs to update his sheet again to check right?
So he runs the macro again, but now the same work orders are getting copied in with the new ones, and he doesn't need the same info twice:
Here's the code (which was hobbled together from different places--I think even from here--and I made changes) :
Code:
Sub CopyColumns()
Dim wsSource As Worksheet
Dim wsDestin As Worksheet
Dim lngDestinRow As Long
Dim rngSource As Range
Dim rngCel As Range
Set wsSource = Sheets("CleanData") 'Edit "Sheet1" to your source sheet name
Set wsDestin = ActiveSheet
With wsSource
'Following line assumes column headers in Source worksheet so starts at row2
Set rngSource = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))
End With
For Each rngCel In rngSource
If rngCel.Value = ActiveSheet.name Then
With wsDestin
'Following line assumes column headers in Destination worksheet
lngDestinRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
rngCel.EntireRow.copy Destination:=wsDestin.Cells(lngDestinRow, "A")
End With
End If
Next rngCel
End Sub
I tried to filter out the duplicates using:
Code:
And IsError(Application.Match(rngCel.Value, rowname, 0))
in this line of code:
Code:
For Each rngCel In rngSource
If rngCel.Value = ActiveSheet.name Then
And I tried to define rowname as a range of all previous rows in the sheet. It did not work.
Based on a suggestion from stackoverflow I made this:
Code:
With wsDestin Set destrng = .Range(.Cells(2, "G"), .Cells(.Rows.Count, "G").End(x1Up))
End With
For Each rngCel In rngSource
If rngCel.Value = ActiveSheet.name And rngCel.Value <> destrng Then
It also did not work.
I initially had formulas within each sheets' cells that gathered all this data but it severely slowed down an already bogged down excel file.
Here's a link to where I asked this question on stackoverflow
https://stackoverflow.com/questions/49947378/macro-to-skip-rows-already-copied
Last edited by a moderator: