Copy row data between sheets when duplicate value is inserted in other sheet

Tuggles

New Member
Joined
Jun 22, 2010
Messages
5
Hi all. Have been trying to get my head around vba but unsuccessful so far. Hopefully somebody can help.
I am using excel 2003 on XP & have attached sample workbook. I’m trying to setup up a document to track stock. There are two sheets “Stock” & “Allocation” which both will go from row 2 - 999. I’m trying to get it so that when you type a number into Allocation “Roll Number”, the macro will scan Stock “Roll Number” to find the same number.
Then it will copy the adjacent cell “Roll Ref Nr” to Allocation Sheet,
Move ” Weight”” Size”” Thickness”” to Allocation Sheet
And delete “Remaining Length” from “Stock” all in the same row.

Also if the number is typed twice in “allocation” it will do nothing and not copy blank spaces from “Stock” when I run it a second time.
This may be too awkward but any help would be much appreciated. Cheers.



Excel Workbook
ABCDEFG
1Roll NumberRoll Ref NrWeightSizeThicknessRemaining LengthCoresponding Profile
21Sample Ref no 112001871.2068190mm
32Sample Ref no 220023611.50471250mm
43Sample Ref no 319883611.50468250mm
54Sample Ref no 421883611.50515250mm
66Sample Ref no 521303611.50501250mm
77Sample Ref no 620263611.50477250mm
88Sample Ref no 721803611.50513250mm
99Sample Ref no 821383611.50503250mm
1011Sample Ref no 921583611.50508250mm
1112Sample Ref no 1020183611.50475250mm
1213Sample Ref no 1137103111.501013200mm
1314Sample Ref no 1237103111.501013200mm
1415Sample Ref no 1323303111.50636200mm
1516Sample Ref no 1422303111.50609200mm
1617Sample Ref no 1522003111.50601200mm
1718Sample Ref no 16****-
1819Sample Ref no 1723253111.50635200mm
1921Sample Ref no 1823253111.50635200mm
2023Sample Ref no 19****-
2124Sample Ref no 2022673111.50619200mm
2226Sample Ref no 2122003111.50601200mm
2327Sample Ref no 2223253111.50635200mm
2428Sample Ref no 2319573111.50534200mm
2529Sample Ref no 2427802550.951462140mm
2630Sample Ref no 25****-
2732Sample Ref no 2613301871.5060490mm
2833Sample Ref no 2713301871.5060490mm
2935Sample Ref no 2813301871.5060490mm
3036Sample Ref no 2912541871.5057090mm
3137Sample Ref no 3019552551.50651140mm
3238Sample Ref no 3119552551.50651140mm
3339Sample Ref no 3219552551.50651140mm
3440Sample Ref no 33****-
3541Sample Ref no 3419002551.50633140mm
3642Sample Ref no 3519002551.50633140mm
3743Sample Ref no 3619002551.50633140mm
3844Sample Ref no 3719552551.50651140mm
3945Sample Ref no 38****-
4046Sample Ref no 39****-
4147Sample Ref no 4027063611.50637250mm
4248Sample Ref no 4127063611.50637250mm
4349Sample Ref no 4228203611.50663250mm
4450Sample Ref no 4328201551.50154570mm
4551Sample Ref no 4425831551.50141570mm
4652Sample Ref no 455961551.5032770mm
4753Sample Ref no 4611571871.5052590mm
4854Sample Ref no 4711571871.5052590mm
4955Sample Ref no 48****-
5056Sample Ref no 49****-
5157Sample Ref no 5018802551.20783140mm
5258Sample Ref no 5118802551.20783140mm
5359Sample Ref no 5218802551.20783140mm
5460Sample Ref no 5318802551.20783140mm
5561Sample Ref no 5418802551.20783140mm
5662Sample Ref no 5513901871.2078990mm
5763Sample Ref no 5613901871.2078990mm
5864Sample Ref no 5712501871.2071090mm
5965Sample Ref no 5812501871.2071090mm
6066Sample Ref no 5912501871.2071090mm
6167Sample Ref no 60****-
6268Sample Ref no 61****-
6369Sample Ref no 62****-
6470Sample Ref no 63****-
Stock





Excel Workbook
ABCDEFGH
1Roll NumberRoll Ref NrWeight [kg]Size [mm]Thickness [mm]Contract No.Contract DescriptionUse [%]
216Sample Ref no 1422303111.50C000113 Backward Street100
323Sample Ref no 1922673111.50C0002Snap100
440Sample Ref no 3319552551.50C000113 Backward Street100
555Sample Ref no 4816183611.50C0002Snap100
656Sample Ref no 4918802551.20C000113 Backward Street100
730Sample Ref no 2513301871.50C000113 Backward Street100
845Sample Ref no 3821962551.50C0004Seaside100
946Sample Ref no 3928203611.50C0005The Grassy Knoll100
1067Sample Ref no 6012501871.20C0015The Grassy Knoll100
1168Sample Ref no 6113501871.20C0004Seaside100
1269Sample Ref no 6213501871.20C0002Snap100
1370Sample Ref no 6313501871.20C000113 Backward Street100
14********
15********
16********
17********
18********
19********
20********
Allocation
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Right-click on the sheet tab "Allocation" and select View Code. Paste the macro below in the VBA edit window.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim r As Range
    
    If Target.Count = 1 Then
        If Not Intersect(Target, Range("A2:A999")) Is Nothing Then
        
            Application.EnableEvents = False
            On Error Resume Next
            
            If Application.CountIf(Range("A2:A999"), Target.Value) > 1 Then
                Target.ClearContents        'Duplicate entry
                
            Else
                Set r = Sheets("Stock").Range("A2:A999").Find(Target.Value, LookAt:=xlWhole, MatchCase:=False)
                
                If r Is Nothing Then
                    Target.ClearContents    'No match found
                Else
                    Target.Offset(, 1).Resize(, 4).Value = r.Offset(, 1).Resize(, 4).Value
                    r.Offset(, 2).Resize(, 5).ClearContents 'Clear cells on Stock sheet?"
                End If
                
            End If
            
            On Error GoTo 0
            Application.EnableEvents = True
            
        End If
    End If
End Sub
 
Upvote 0
AlfaFrog you are a legend. Thanks for getting back so quick. Does exactly what I asked and works perfectly but have realized that might need to type duplicates in the allocation sheet, “roll number” as sometimes only half a roll is used. So still need it to scan the stock sheet but then not delete duplicates in the Allocation Sheet. Thanks again and sorry for buggin!
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim r As Range
    
    If Target.Count = 1 Then
        If Not Intersect(Target, Range("A2:A999")) Is Nothing Then
        
            Application.EnableEvents = False
            On Error Resume Next
            
                Set r = Sheets("Stock").Range("A2:A999").Find(Target.Value, LookAt:=xlWhole, MatchCase:=False)
                
                If r Is Nothing Then
                    Target.ClearContents    'No match found
                Else
                    Target.Offset(, 1).Resize(, 4).Value = r.Offset(, 1).Resize(, 4).Value
                    
                End If
                
            End If
            
            On Error GoTo 0
            Application.EnableEvents = True
            
        End If
    End If
End Sub
 
Upvote 0
Have put code into document but it throws up an error
Compile error
End If without Block If
Tried editing but not able for it. Took out the last End If which was highlighted but then Macro just deleted each number i typed into Allocation sheet. Cheers Tuggles.
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim r As Range
    
    If Target.Count = 1 Then
        If Not Intersect(Target, Range("A2:A999")) Is Nothing Then
        
            Application.EnableEvents = False
            On Error Resume Next
            
                Set r = Sheets("Stock").Range("A2:A999").Find(Target.Value, LookAt:=xlWhole, MatchCase:=False)
                
                If r Is Nothing Then
                    Target.ClearContents    'No match found
                Else
                    Target.Offset(, 1).Resize(, 4).Value = r.Offset(, 1).Resize(, 4).Value
                    
                End If
            
            On Error GoTo 0
            Application.EnableEvents = True
            
        End If
    End If
End Sub
 
Upvote 0
AlfaFrog again that code works really well but its not just doing exactly what i need. As with the first code it moves,copies & deletes all the data from Stock to Allocation but then i'd like to have the option to type the Roll number again in Allocation to split usage of roll between jobs. Have attached a sample.


Excel Workbook
ABCDEFGH
440Sample Ref no 3319552551.50C000113 Backward Street100
555Sample Ref no 4816183611.50C0002Snap50
655Sample Ref no 1316183611.50C0002Snap50
756Sample Ref no 4918802551.20C000113 Backward Street100
830Sample Ref no 213301871.50C000113 Backward Street80
930Sample Ref no 2513301871.50C000113 Backward Street20
1045Sample Ref no 3821962551.50C0004Seaside100
Allocation
 
Upvote 0
Hey AlfaFrog
Copied a bit of code from your 2nd post to your first one and its working spot on.
Thanks for all the help. Max Respect.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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