VBA Find match, if found, copy rows and paste in different workbook

Engalpengal

New Member
Joined
May 10, 2023
Messages
43
Office Version
  1. 365
Platform
  1. Windows
Hello.
I have a list of products in Col "C13:AF" In workbook "Ordre"(sheet "Ordre"), that i vant to match to "AG12".
The range for match is found in Col "AG13:AG"

There will be several hits.
I want all Matches to be copied and paste in another workbook called "Hist" (Sheet "Hist")
Range for paste destination is A9:AD
First available row

The History file will contain a year of prodused products, witch means that this list will be long as the time goes.

Under follows my attempt

VBA Code:
Sub To_Hist()

Dim c As Range, wsSrc As Worksheet, wsOut As Worksheet, wb As Workbook
Dim cDest As Range, wsTrans As Worksheet, rngList As Range

Set wbo = ThisWorkbook '____________________________________________________________________Workbook for search area
Set wbh = Workbooks("Hist.xlsm") '__________________________________________________________Workbook for paste area

Set wsO = wbo.Worksheets("Ordre") '_________________________________________________________Sheet for search area
Set wsH = wbh.Worksheets("Hist") '__________________________________________________________Sheet for paste area

Set srcKey = wsO.Range("AG12") '____________________________________________________________Range for Search key

Set cDest = wsOut.Range("A9:AD") '___________________________________________________________Range for Paste destination

Application.ScreenUpdating = False
For Each c In wsO.Range("AG13:AG" & wsO.Cells(Rows.Count, "AG").End(xlUp).Row).Cells '______Area for search and copy from
    If Not IsError(Application.Match(c.Value, srcKey, 0)) Then '____________________________Any match in lookup list?
        With wsO
            .Range(.Cells(c.Row, "C"), .Cells(c.Row, "AF")).Copy cDest '____________________Width of rows to copy
        End With
        Set cDest = cDest.End(xlUp).Offset(1, 0) '__________________________________________Next paste row
    End If
Next c
Application.ScreenUpdating = True
End Sub

I recive and error on following line in the program:
Set cDest = wsOut.Range("A9:AD")
Run time error 91, object variable or with block variable not set
I know this code isnt right, but i do not know how to write it correcly
WB "Ordre"
Ordre-Spor.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAV
10Dato for utført prosessProsesser
11StatusOrdre nrSalgsordre datoForv.fors.datoProdukt nrProduktbeskrivelsePosLengde, mmBredde, mmEkstra infoFarge RALAntKunde nrKunde navnOrdre ansPL, datoPLM, datoOV, datoPR, datoSKS, datoSK, datoSKVD, datoMA, datoLE, datoMV, datoVA, datoBSS, datoCS, datoCSF, datoLA, datoPiU, datoPLPLMOVPRSKSSKSKVDMALEMVVABS-SCS-SCS-FLA
12-------------------------------1---------------
1396484131.05.202305.08.202311051612132Skilt nr 516VH LS R-kl 3 Tosidig H=hldr Gangfelt1778899Hedlund BilMM00.01.190000.01.190000.01.190000.01.1900#######00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900Nytt prodxxxVxx0000000x
1496484131.05.202305.08.202311052200131Skilt nr 522 R-kl 3 H=klmr Gang- og sykkelvegSkjæring1778899Hedlund BilMM00.01.190000.01.190000.01.190000.01.1900#######00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900Nytt prodxxxVxx0000000x
1596484131.05.202305.08.202312010808200Skilt R.kl 1 H=klmr - Møteplass ved brann/evakueringGul-grønn fluor.2778899Hedlund BilMM00.01.190000.01.190000.01.190000.01.1900#######00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900Nytt prodxxxVxx0000000x
1696484131.05.202305.08.202341690000310Underskilt for foldeskilt, Nøytrale underskilt gul2778899Hedlund BilMM00.01.190000.01.190000.01.190000.01.1900#######00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900Nytt prodxxxVxx0000000x
1796535701.06.202308.08.202311072902122Skilt nr 729 R-kl 2 Gatenavn tosidig (10 bokst. TH=70mm)1123456NAF FjellstuenTR00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900Nytt prodxxxxxx0000000x
1896564002.06.202308.08.202321020089400Stolpe, 89 mm/4,0 m60121958647Melund TrafikkKR00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900Nytt prodxOVxxxx0000000x
1996564002.06.202310.08.202321970000500Omstillingskostnad, lakkering1958647Melund TrafikkKR00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900Nytt prodxxxxxx0000000x
2096564002.06.202310.08.202391010101000Frakt totalt1123456NAF FjellstuenTR00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900Nytt prodxxxxxx0000000x
2185446323.04.202311.08.202311040210111Skilt nr 402.1 LS R-kl 1 H=klmr Påbudt kjøreretning1966322Finsnes AutobaneIL00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900Nytt prodxxxxxx0000000x
2285446323.04.202311.08.202321010060250Stolpe, 60 mm/2,5 m1966322Finsnes AutobaneIL00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190001.09.202300.01.19001PLxxxxxxxxxxxxxV
2385446323.04.202311.08.202321127060000Klammer rett, alu., 60 mm, ensidig kompl. m/skrue2966322Finsnes AutobaneIL00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190001.09.202300.01.19001xxxxxxxxxxxxxxV
Ordre
Cells with Conditional Formatting
CellConditionCell FormatStop If True
AV13:AV3000Cell Valuebeginning with "LA"textNO
AV13:AV3000Cell Value="V"textNO
AN13:AN3000Cell Value="V"textNO
AN13:AN3000Cell Valuebeginning with "SK"textNO
AM13:AM3000Cell Valuebeginning with "SK"textNO
AM13:AM3000Cell Value="V"textNO
AL13:AL3000Cell Valuebeginning with "SK"textNO
AL13:AL3000Cell Value="V"textNO
AK13:AK3000Cell Value="V"textNO
AK13:AK3000Cell Valuebeginning with "PR"textNO
AJ13:AJ5000Cell Value="Nytt prod"textNO
AJ13:AJ5000Cell Value="V"textNO
AJ13:AJ5000Cell Valuebeginning with "OV"textNO
AI13:AI3000Cell Value="V"textNO
AH13:AH3000Cell Value="Nytt prod"textNO
E13:E2000Dates OccurringyesterdaytextNO
E13:E2000Dates OccurringtodaytextNO
E13:E2000Dates OccurringtomorrowtextNO
E13:E2000Dates Occurringlast 7 daystextNO
E13:E2000Dates Occurringthis weektextNO
E13:E2000Dates Occurringlast monthtextNO
E13:E2000Dates Occurringlast weektextNO
AH13:AH3000Cell Value="V"textNO
Q13:AF1048576Cell Value>1textNO
Q13:AF1048576Cell Value>0textNO
AI13:AI3000Cell Valuebeginning with "PLM"textNO
AH13:AH3000Cell Valuebeginning with "PL"textNO
B13:B26051Cell Value<0textNO
B13:B26051Cell Valuebeginning with "PL"textNO
B13:B26051Cell Valuebeginning with "PR"textNO
B13:B26051Cell Valuebeginning with "OV"textNO
B13:B26051Cell Valuebeginning with "SK"textNO
B13:B26051Cell Valuebeginning with "LA"textNO

WB "Hist"
Hist.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
7Ordre nrSalgsordre datoForv.fors.datoProdukt nrProduktbeskrivelsePosLengde, mmBredde, mmEkstra infoFarge RALAntKunde nrKunde navnOrdre ansPL, datoPLM, datoOV, datoPR, datoSKS, datoSK, datoSKVD, datoMA, datoLE, datoMV, datoVA, datoBSS, datoCS, datoCSF, datoLA, datoPiU, datoLedetid, dager
8-------------------------------
911223312.04.202314.08.202311011000243Skilt nr 110 MS R-kl 3 G/GR H=nøkkel Vegarbeidflgkjdghf134100Karmøy KommuneØK00.01.190000.01.1900##############00.01.1900#######00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900#######00.01.1900
1011223312.04.202314.08.202311011000331Skilt nr 110 SS R-kl 3 G/GR H=klmr Vegarbeid125719Mesta ASMN00.01.190000.01.1900##############00.01.1900#######00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900#######00.01.1900
1111223312.04.202314.08.202311011000531Skilt nr 110 MS R-kl 3 G/GR m/bakstøtte Vegarbeid5778225719Mesta ASBUR00.01.190000.01.1900##############00.01.1900#######00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900#######00.01.1900
1266554421.08.202304.09.202311072909212Skilt nr 729 R-kl 1 Gatenavn tosidig (13 bokst. TH=105mm)1900380Skjæring99992777777Bærum KommuneRR00.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900#######00.01.190000.01.190000.01.190000.01.190000.01.190000.01.190000.01.1900#######00.01.1900
13
14
15
16
17
18
19
Hist
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C12Dates OccurringyesterdaytextNO
C12Dates OccurringtodaytextNO
C12Dates OccurringtomorrowtextNO
C12Dates Occurringlast 7 daystextNO
C12Dates Occurringthis weektextNO
C12Dates Occurringlast monthtextNO
C12Dates Occurringlast weektextNO
O12:AD12Cell Value>1textNO
O12:AD12Cell Value>0textNO
C11Dates OccurringyesterdaytextNO
C11Dates OccurringtodaytextNO
C11Dates OccurringtomorrowtextNO
C11Dates Occurringlast 7 daystextNO
C11Dates Occurringthis weektextNO
C11Dates Occurringlast monthtextNO
C11Dates Occurringlast weektextNO
O11:AD11Cell Value>1textNO
O11:AD11Cell Value>0textNO
C10Dates OccurringyesterdaytextNO
C10Dates OccurringtodaytextNO
C10Dates OccurringtomorrowtextNO
C10Dates Occurringlast 7 daystextNO
C10Dates Occurringthis weektextNO
C10Dates Occurringlast monthtextNO
C10Dates Occurringlast weektextNO
O10:AD10Cell Value>1textNO
O10:AD10Cell Value>0textNO
C9Dates OccurringyesterdaytextNO
C9Dates OccurringtodaytextNO
C9Dates OccurringtomorrowtextNO
C9Dates Occurringlast 7 daystextNO
C9Dates Occurringthis weektextNO
C9Dates Occurringlast monthtextNO
C9Dates Occurringlast weektextNO
O9:AD9Cell Value>1textNO
O9:AD9Cell Value>0textNO
C13:C501Dates OccurringyesterdaytextNO
C13:C501Dates OccurringtodaytextNO
C13:C501Dates OccurringtomorrowtextNO
C13:C501Dates Occurringlast 7 daystextNO
C13:C501Dates Occurringthis weektextNO
C13:C501Dates Occurringlast monthtextNO
C13:C501Dates Occurringlast weektextNO
O13:AD501Cell Value>1textNO
O13:AD501Cell Value>0textNO
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You only need the first cell in the range for the paste so you only need
VBA Code:
Set cDest = wsOut.Range("A9")
and
VBA Code:
Set cDest = cDest.End(xlUp).Offset(1, 0)
change to
VBA Code:
Set cDest = cDest.Offset(1, 0)

I also can't see you defining the sheet anywhere for wsOut, are you sure it shouldn't be wsH
 
Last edited:
Upvote 0
Thanks Mark you have good eyes.
The program finds the correct lines, but when pasting in "Hist", it doesn't go to the first available row, it overwrites the first line in the specified range
 
Upvote 0
Is there anything in A8 of sheet Hist? and are the cells below A8 all blank?
 
Upvote 0
Going by your XL2BB try the code below (untested)

VBA Code:
Sub To_Hist()

Dim c As Range, wbo As Workbook, wbh As Workbook
Dim wsO As Worksheet, wsH As Worksheet, srcKey As Range

Set wbo = ThisWorkbook '____________________________________________________________________Workbook for search area
Set wbh = Workbooks("Hist.xlsm") '__________________________________________________________Workbook for paste area

Set wsO = wbo.Worksheets("Ordre") '_________________________________________________________Sheet for search area
Set wsH = wbh.Worksheets("Hist") '__________________________________________________________Sheet for paste area

Set srcKey = wsO.Range("AG12") '____________________________________________________________Range for Search key


Application.ScreenUpdating = False

For Each c In wsO.Range("AG13:AG" & wsO.Cells(Rows.Count, "AG").End(xlUp).Row).Cells '______Area for search and copy from
   
    If Not IsError(Application.Match(c.Value, srcKey, 0)) Then '____________________________Any match in lookup list?
        With wsO
            .Range(.Cells(c.Row, "C"), .Cells(c.Row, "AF")).Copy wsH.Cells(Rows.Count, "A").End(xlUp).Offset(1)
        End With
    End If
   
Next c

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

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