Search copy paste from one sheet to another using only selected cells

kevinchurchill

New Member
Joined
Jul 20, 2015
Messages
26
Hello,


I have an inventory XL spread sheet, what I would like to do is search for a specific refference N°(code article SAP) that will be in column B sheet 1 and then copying only the data for that refference that is in rows, N,O,P,R. and then pasting that data to another sheet, i want this as the end user does not need to see the full data relevent to the refference N°.
So the first example below is what I want to end up with on another sheet after searching the first sheet using only the Code article SAP N°.
I hope this is clear.
Many thanks for your help
[TABLE="width: 300"]
<colgroup><col width="80" style="width: 60pt;" span="5"> <tbody>[TR]
[TD="width: 80, bgcolor: transparent"][/TD]
[TD="width: 80, bgcolor: transparent"] N N [/TD]
[TD="width: 80, bgcolor: transparent"]O[/TD]
[TD="width: 80, bgcolor: transparent"]P[/TD]
[TD="width: 80, bgcolor: transparent"]R[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="class: xl65, width: 80, bgcolor: #92D050"]Code article SAP[/TD]
[TD="class: xl65, width: 80, bgcolor: #92D050"]Numéro de commande[/TD]
[TD="class: xl65, width: 80, bgcolor: #92D050"]Date d'envoi de la commande [/TD]
[TD="class: xl65, width: 80, bgcolor: #92D050"]Délais de livraison prévu [/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="class: xl66, bgcolor: white, align: right"]700013205[/TD]
[TD="class: xl66, bgcolor: white, align: right"]4500456266[/TD]
[TD="class: xl67, bgcolor: white, align: right"]12/01/2018[/TD]
[TD="class: xl67, bgcolor: white, align: right"]18/01/2018[/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="width: 912"]
<colgroup><col width="111" style="width: 83pt; mso-width-source: userset; mso-width-alt: 4059;"> <col width="116" style="width: 87pt; mso-width-source: userset; mso-width-alt: 4242;"> <col width="127" style="width: 95pt; mso-width-source: userset; mso-width-alt: 4644;"> <col width="136" style="width: 102pt; mso-width-source: userset; mso-width-alt: 4973;"> <col width="145" style="width: 109pt; mso-width-source: userset; mso-width-alt: 5302;" span="5"> <tbody>[TR]
[TD="class: xl66, width: 111, bgcolor: #D9D9D9"]Date de la DDM[/TD]
[TD="class: xl66, width: 116, bgcolor: #D9D9D9"]date de demande chiffrage[/TD]
[TD="class: xl66, width: 127, bgcolor: #D9D9D9"]Retour chiffrage[/TD]
[TD="class: xl75, width: 136, bgcolor: #92D050"]Date d'envoi de la commande [/TD]
[TD="class: xl75, width: 145, bgcolor: #92D050"]Numéro de commande[/TD]
[TD="class: xl75, width: 145, bgcolor: #92D050"]Code article SAP[/TD]
[TD="class: xl66, width: 145, bgcolor: #D9D9D9"]Montant [/TD]
[TD="class: xl75, width: 145, bgcolor: #92D050"]Délais de livraison prévu [/TD]
[TD="class: xl66, width: 145, bgcolor: #D9D9D9"]Date Reception[/TD]
[/TR]
[TR]
[TD="class: xl70, width: 111, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl70, width: 116, bgcolor: white"]11/01/2018[/TD]
[TD="class: xl70, width: 127, bgcolor: white"]11/01/2018[/TD]
[TD="class: xl76, width: 136, bgcolor: #92D050"]17/01/2018[/TD]
[TD="class: xl77, width: 145, bgcolor: #92D050"]9500303528[/TD]
[TD="class: xl78, width: 145, bgcolor: #92D050"] [/TD]
[TD="class: xl67, width: 145, bgcolor: white"]540,00 €[/TD]
[TD="class: xl76, width: 145, bgcolor: #92D050"]24/01/2018[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]24/01/2018[/TD]
[/TR]
[TR]
[TD="class: xl68, width: 111, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl68, width: 116, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl68, width: 127, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl72, width: 136, bgcolor: #92D050"]16/01/2018[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"]9500303432[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"] [/TD]
[TD="class: xl67, width: 145, bgcolor: white"]224,00 €[/TD]
[TD="class: xl72, width: 145, bgcolor: #92D050"]23/01/2018[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]24/01/2018[/TD]
[/TR]
[TR]
[TD="class: xl68, width: 111, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl68, width: 116, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl68, width: 127, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl72, width: 136, bgcolor: #92D050"]04/01/2018[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"]9500303079[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"] [/TD]
[TD="class: xl67, width: 145, bgcolor: white"]74,00 €[/TD]
[TD="class: xl72, width: 145, bgcolor: #92D050"]10/01/2018[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]12/01/2018[/TD]
[/TR]
[TR]
[TD="class: xl68, width: 111, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl68, width: 116, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl68, width: 127, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl72, width: 136, bgcolor: #92D050"]24/01/2018[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"]4500457298[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"] [/TD]
[TD="class: xl67, width: 145, bgcolor: white"]6 729,17 €[/TD]
[TD="class: xl72, width: 145, bgcolor: #92D050"]06/02/2018[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]13/02/2018[/TD]
[/TR]
[TR]
[TD="class: xl68, width: 111, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl68, width: 116, bgcolor: white"]08/01/2018[/TD]
[TD="class: xl68, width: 127, bgcolor: white"]08/01/2018[/TD]
[TD="class: xl72, width: 136, bgcolor: #92D050"]12/01/2018[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"]4500456266[/TD]
[TD="class: xl74, width: 145, bgcolor: red"]700013205[/TD]
[TD="class: xl67, width: 145, bgcolor: white"]95,00 €[/TD]
[TD="class: xl72, width: 145, bgcolor: #92D050"]18/01/2018[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]16/01/2018[/TD]
[/TR]
[TR]
[TD="class: xl68, width: 111, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl68, width: 116, bgcolor: white"]01/01/2018[/TD]
[TD="class: xl68, width: 127, bgcolor: white"]05/01/2018[/TD]
[TD="class: xl72, width: 136, bgcolor: #92D050"]05/01/2018[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"]4500455815[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"]700013746[/TD]
[TD="class: xl67, width: 145, bgcolor: white"]761,52 €[/TD]
[TD="class: xl72, width: 145, bgcolor: #92D050"]18/01/2018[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]22/01/2018[/TD]
[/TR]
[TR]
[TD="class: xl68, width: 111, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl68, width: 116, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl68, width: 127, bgcolor: white"]12/01/2018[/TD]
[TD="class: xl72, width: 136, bgcolor: #92D050"]17/01/2018[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"]9500303528[/TD]
[TD="class: xl73, width: 145, bgcolor: #92D050"] [/TD]
[TD="class: xl67, width: 145, bgcolor: white"]253,96 €[/TD]
[TD="class: xl72, width: 145, bgcolor: #92D050"]24/01/2018[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]24/01/2018[/TD]
[/TR]
[TR]
[TD="class: xl68, width: 111, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl68, width: 116, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl68, width: 127, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl68, width: 136, bgcolor: white"]04/01/2018[/TD]
[TD="class: xl69, width: 145, bgcolor: white"]9500303079[/TD]
[TD="class: xl71, width: 145, bgcolor: #E2EFDA"] [/TD]
[TD="class: xl67, width: 145, bgcolor: white"]1 065,44 €[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]25/01/2018[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]12/01/2018[/TD]
[/TR]
[TR]
[TD="class: xl68, width: 111, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl68, width: 116, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl68, width: 127, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl68, width: 136, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl69, width: 145, bgcolor: white"]9500303052[/TD]
[TD="class: xl71, width: 145, bgcolor: #E2EFDA"] [/TD]
[TD="class: xl67, width: 145, bgcolor: white"]365,35 €[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]03/01/2018[/TD]
[TD="class: xl68, width: 145, bgcolor: white"]03/01/2018[/TD]
[/TR]
</tbody>[/TABLE]
 
Hello Kevin,

If you have tested the sample in the link from post #8 you will see that only the relevant cells per row of data are transferred over. For example, in the sample file, if you type in SAP number 10005 and click on the "GO" button, only the relevant cells from two rows related to that SAP number are transferred over not the whole column.
If you have tested the code in your actual workbook and you have this issue then there must be something else at play. You may have to upload a sample of your actual workbook for us to work with. You can upload a sample to a free file sharing site such as Drop Box or ge.tt then post the link to your file back here.
Make sure that the sample is an exact replica of your workbook but please use dummy data.

Cheerio,
vcoolio.
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hello Vcoolio,
I have now successfully intergrated your code into my worksheet and with a few tweeks for the final position for the search results it is now working perfect.
I would if I may ask you one more favour as I have tried many things but not succeeded, and that is to have a message box displayed if the SAP number is not found i.e "code article non trouvé".
I can make this box appear when there is no such code found but it also appears when a correct code is found.
One again, many thanks.
Kevin.
 
Upvote 0
Hello Kevin,

Test the following modified code in the sample file in post #8. It will do as you require.

Code:
Sub TransferData()

        Dim SAPSrch As String
        Dim lr As Long, [COLOR=#ff0000]sRng As Range[/COLOR]
        Dim cArr As Variant, pArr As Variant

SAPSrch = Sheet2.[A1].Value
lr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
cArr = Array("N2:N" & lr, "O2:O" & lr, "P2:P" & lr, "R2:R" & lr)
pArr = Array("N", "O", "P", "R")

Application.ScreenUpdating = False

[COLOR=#ff0000]With Sheet1.Range("P:P")[/COLOR]
        [COLOR=#ff0000]Set sRng = .Find(What:=SAPSrch, After:=.Cells(.Cells.Count), lookat:=xlWhole)
        If Not sRng Is Nothing Then[/COLOR]
Sheet1.[A1].CurrentRegion.AutoFilter 16, SAPSrch
        For x = LBound(cArr) To UBound(cArr)
        Sheet1.Range(cArr(x)).Copy
        Sheet2.Range(pArr(x) & Rows.Count).End(3)(2).PasteSpecial xlValues
        Next x
[COLOR=#ff0000]    Else
        MsgBox "Code article non trouvé."
        End If
End With[/COLOR]

Sheet1.[P1].AutoFilter
Sheet2.[A1].Value = "SAP Search"

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub

The lines in red font are the additional lines which should do the task for you.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Hello Vcoolio,
Thanks for the code, On you're test workbook it works fine, when I have transferred it to my workbook and typed a wrong code in the search box all is good, I have the message box as it should be displayed, however when I OK the message box it dissapeares like it should and then when I type a code that is in the P column, I recieve a runtime error 1004 on the line with red text, and the programme is blocked, I have pasted a copy of the code below, you can see that my search box and results columns are not in the same place as on your workbook.


Sub TransferData()
Dim SAPSrch As String
Dim lr As Long, sRng As Range
Dim cArr As Variant, pArr As Variant
SAPSrch = Feuil1.[H4].Value
lr = Feuil2.Range("A" & Rows.Count).End(xlUp).Row
cArr = Array("N5:N" & lr, "O5:O" & lr, "P5:P" & lr, "R5:R" & lr)
pArr = Array("C", "D", "E", "F")
Application.ScreenUpdating = False
With Feuil2.Range("P:P")
Set sRng = .Find(What:=SAPSrch, After:=.Cells(.Cells.Count), lookat:=xlWhole)
If Not sRng Is Nothing Then

Feuil2.[P5].CurrentRegion.AutoFilter 16, SAPSrch
For x = LBound(cArr) To UBound(cArr)
Feuil2.Range(cArr(x)).Copy
Feuil1.Range(pArr(x) & Rows.Count).End(3)(1).PasteSpecial xlValues
Next x
Else
MsgBox "Code article non trouvé."
End If
End With


Feuil2.[P1].AutoFilter
Feuil1.[H4].Value = "7000"

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub

When the message box code is removed it works ok like before.
Best regards, Kevin.
 
Upvote 0
Hello Kevin,

Can you please upload a sample of your workbook to a free file sharing site such as ge.tt or Drop Box then post the link to your file back here. This will give me something more to work with.
Make sure that the sample is an exact replica of your workbook and please use dummy data. Just a few rows of data per sheet will do.

Cheerio,
vcoolio.
 
Upvote 0
Hello Kevin,

I had to transpose your workbook sample to a blank Excel file as the sample you supplied came up with the error "Can't find project or library".

But anyway, following is the code again. You'll notice some subtle adjustments to suit your workbook:-


Code:
Sub TransferData2()

        Dim SAPSrch As String
        Dim lr As Long, sRng As Range
        Dim cArr As Variant, pArr As Variant
        SAPSrch = Sheet1.[H4].Value
        lr = Sheet2.Range([COLOR=#ff0000]"O"[/COLOR] & Rows.Count).End(xlUp).Row
[COLOR=#ff0000]
cArr = Array("N6:N" & lr, "O6:O" & lr, "P6:P" & lr, "R6:R" & lr)[/COLOR]
pArr = Array("C", "D", "E", "F")

Application.ScreenUpdating = False

With Sheet2.Range("P:P")
        Set sRng = .Find(What:=SAPSrch, After:=.Cells(.Cells.Count), lookat:=xlWhole)
If Not sRng Is Nothing Then
Sheet2.[COLOR=#ff0000][A5][/COLOR].CurrentRegion.AutoFilter 16, SAPSrch
        For x = LBound(cArr) To UBound(cArr)
        Sheet2.Range(cArr(x)).Copy
        Sheet1.Range(pArr(x) & Rows.Count).End(3)[COLOR=#ff0000](2)[/COLOR].PasteSpecial xlValues
        Next x
Else
MsgBox "Code article non trouvé."
End If
End With


Sheet2.[COLOR=#ff0000][P5][/COLOR].AutoFilter
Sheet1.[H4].Value = "7000"

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub

The changes are noted in red font in the above code.
The Runtime Error that you received is due to the fact that the CurrentRegion starts at A5 not P5 as in your code in post #14 .

You may notice that I've had to use the English sheet codes (Sheet1, Sheet2 etc.). You'll just need to change them back to the French sheet codes (Feuil1 etc.).

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Hello Vcoolio,
What can I say, you're a Genius, evrything now works perfectly as I had first envisaged, the workbook is now live and with you're coding life will be more easy to search for items and give alerts when needed, much appreciated that you have given you're time to help a complete stranger.
Thank you.
Kevin.
 
Upvote 0
Hello Kevin,

Thank you for the kind words and you're welcome. I'm glad that I was able to help you.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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