VBA code that pulls the data from the workbook to another workbook

MMM_84

New Member
Joined
Jan 13, 2021
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Looking for a VBA code that pulls the data from the workbook to another workbook based on several criteria. Not sure if that was already solved
Precisely, there is a "main" workbook, that contains a lot of information (column A to BN) and there is separate workbook with different worksheet names, matching the "main" workbook's column D data
Thus, I need a vba that copies from "main" workbook yellow marked information based on Green market matching criteria

Main workbook:
Book1.xlsx
ABCDEFGHI
1ID numberReferencePeriodTypeCommentCriteriaetc.1etc.2etc.3
215645441B206-05-99AppleOut of scopeABC1
31545633B207-05-99CarrotIn StockABC3
47863896B308-05-99CucumberShortageABC4
5741526B409-05-99PotatoAbundanceABC5
6896544B510-05-99PeachOut of scopeABC1
79865436B611-05-99StrawberryIn StockABC2
868986546B112-05-99BlueberryIn StockABC7
968986547B213-05-99PeachOut of scopeABC1
1068986548B314-05-99TomatoOut of scopeABC3
1168986549B415-05-99AppleABCABC4
1268986550B516-05-99CarrotAbundanceABC5
1368986551B117-05-99AppleIn StockABC1
1468986552B318-05-99AppleIn StockABC2
1568986553B319-05-99PotatoAbundanceABC7
1668986554B420-05-99CucumberOut of scopeABC5
1768986555B521-05-99BlueberryAbundanceABC4
main



The workbook and the "Apple" tab

Book1.xlsx
ABCDE
1TypeApple
2ReferenceB1B2B3B4
3Criteria
4ABC1ID Number: 68986551 Period: 17-05-1999 Comment: In StockID Number: 15645441 Period: 06-05-1999 Comment: Out of scope
5ABC2ID Number: 68986552 Period: 18-05-1999 Comment: In Stock
6ABC3
7ABC4ID Number: 68986549 Period: 15-05-1999 Comment: ABC
8ABC5
9ABC6
10ABC7
11ABC8
12ABC9
13ABC10
Apple

 

Attachments

  • 1695635399799.png
    1695635399799.png
    2.5 KB · Views: 6
  • 1695635405247.png
    1695635405247.png
    1.5 KB · Views: 5

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
try this, i don't test it yet but you can and feddback to me:
VBA Code:
Sub test()
    Dim shMain As Worksheet
    Dim desSh As Worksheet
    Dim Lr As Long
    Dim xRng As Range
    Dim xCll As Range
    Dim CriteriaRng As Range
    Dim RefRng As Range
    Dim desCll As Range
    Set shMain = ThisWorkbook.Sheets("main") 'this is sheet "main"
    Lr = shMain.Cells(Rows.Count, 1).End(xlUp).Row
    Set xRng = shMain.Range("A2:A" & Lr) 'this is ID Number range
    For Each xCll In xRng.Offset(, 3)  'loop each value in Type range
        If Not IsEmpty(xCll) Then
            Set desSh = ThisWorkbook.Sheets(xCll.Value) 'this sheet has same name as type so you have to make sure all your separate sheet have excatly name as each Type
            Set CriteriaRng = desSh.Range("A4:A99") 'this is criteria range in separate sheet, becase i don't know how many criteria in your main sheet so i set it to 99 criteria
            Set RefRng = desSh.Range("B2:AY2") 'this reference range i set to 50 reference same as above
            Set desCll = desSh.Range(CriteriaRng.Find(xCll.Offset(, 2)).Row, RefRng.Find(xCll.Offset(, -2)).Column) 'this is the cell need to find with match criteria and reference
            desCll.Value = "ID Number: " & xCll.Offset(, -3).Value & Chr(10) & _
            "Period: " & xCll.Offset(, -1).Value & Chr(10) & "Comment: " & xCll.Offset(, 1).Value 'this code get value from main sheet
        End If
    Next xCll
End Sub
 
Upvote 0
try this, i don't test it yet but you can and feddback to me:
VBA Code:
Sub test()
    Dim shMain As Worksheet
    Dim desSh As Worksheet
    Dim Lr As Long
    Dim xRng As Range
    Dim xCll As Range
    Dim CriteriaRng As Range
    Dim RefRng As Range
    Dim desCll As Range
    Set shMain = ThisWorkbook.Sheets("main") 'this is sheet "main"
    Lr = shMain.Cells(Rows.Count, 1).End(xlUp).Row
    Set xRng = shMain.Range("A2:A" & Lr) 'this is ID Number range
    For Each xCll In xRng.Offset(, 3)  'loop each value in Type range
        If Not IsEmpty(xCll) Then
            Set desSh = ThisWorkbook.Sheets(xCll.Value) 'this sheet has same name as type so you have to make sure all your separate sheet have excatly name as each Type
            Set CriteriaRng = desSh.Range("A4:A99") 'this is criteria range in separate sheet, becase i don't know how many criteria in your main sheet so i set it to 99 criteria
            Set RefRng = desSh.Range("B2:AY2") 'this reference range i set to 50 reference same as above
            Set desCll = desSh.Range(CriteriaRng.Find(xCll.Offset(, 2)).Row, RefRng.Find(xCll.Offset(, -2)).Column) 'this is the cell need to find with match criteria and reference
            desCll.Value = "ID Number: " & xCll.Offset(, -3).Value & Chr(10) & _
            "Period: " & xCll.Offset(, -1).Value & Chr(10) & "Comment: " & xCll.Offset(, 1).Value 'this code get value from main sheet
        End If
    Next xCll
End Sub
thanks, but I guess this works if all is in one workbook. In my case I have a main wb and the destination wb with different sheets such named after each type in main wb (such as sh "apple", "tomato" and etc.)
 
Upvote 0
thanks, but I guess this works if all is in one workbook. In my case I have a main wb and the destination wb with different sheets such named after each type in main wb (such as sh "apple", "tomato" and etc.)
So you can set destination workbook first and change destination worksheet like this:
Sub test()
Dim shMain As Worksheet
Dim desWb as workbook
Dim desSh As Worksheet
Dim Lr As Long
Dim xRng As Range
Dim xCll As Range
Dim CriteriaRng As Range
Dim RefRng As Range
Dim desCll As Range
Set desWb= getopenfilename("your destination workbook path")
Set shMain = ThisWorkbook.Sheets("main") 'this is sheet "main"
Lr = shMain.Cells(Rows.Count, 1).End(xlUp).Row
Set xRng = shMain.Range("A2:A" & Lr) 'this is ID Number range
For Each xCll In xRng.Offset(, 3) 'loop each value in Type range
If Not IsEmpty(xCll) Then
Set desSh = desWb.Sheets(xCll.Value) 'this sheet has same name as type so you have to make sure all your separate sheet have excatly name as each Type
Set CriteriaRng = desSh.Range("A4:A99") 'this is criteria range in separate sheet, becase i don't know how many criteria in your main sheet so i set it to 99 criteria
Set RefRng = desSh.Range("B2:AY2") 'this reference range i set to 50 reference same as above
Set desCll = desSh.Range(CriteriaRng.Find(xCll.Offset(, 2)).Row, RefRng.Find(xCll.Offset(, -2)).Column) 'this is the cell need to find with match criteria and reference
desCll.Value = "ID Number: " & xCll.Offset(, -3).Value & Chr(10) & _
"Period: " & xCll.Offset(, -1).Value & Chr(10) & "Comment: " & xCll.Offset(, 1).Value 'this code get value from main sheet
End If
Next xCll
End Sub
 
Upvote 0
So you can set destination workbook first and change destination worksheet like this:
Sub test()
Dim shMain As Worksheet
Dim desWb as workbook
Dim desSh As Worksheet
Dim Lr As Long
Dim xRng As Range
Dim xCll As Range
Dim CriteriaRng As Range
Dim RefRng As Range
Dim desCll As Range
Set desWb= getopenfilename("your destination workbook path")
Set shMain = ThisWorkbook.Sheets("main") 'this is sheet "main"
Lr = shMain.Cells(Rows.Count, 1).End(xlUp).Row
Set xRng = shMain.Range("A2:A" & Lr) 'this is ID Number range
For Each xCll In xRng.Offset(, 3) 'loop each value in Type range
If Not IsEmpty(xCll) Then
Set desSh = desWb.Sheets(xCll.Value) 'this sheet has same name as type so you have to make sure all your separate sheet have excatly name as each Type
Set CriteriaRng = desSh.Range("A4:A99") 'this is criteria range in separate sheet, becase i don't know how many criteria in your main sheet so i set it to 99 criteria
Set RefRng = desSh.Range("B2:AY2") 'this reference range i set to 50 reference same as above
Set desCll = desSh.Range(CriteriaRng.Find(xCll.Offset(, 2)).Row, RefRng.Find(xCll.Offset(, -2)).Column) 'this is the cell need to find with match criteria and reference
desCll.Value = "ID Number: " & xCll.Offset(, -3).Value & Chr(10) & _
"Period: " & xCll.Offset(, -1).Value & Chr(10) & "Comment: " & xCll.Offset(, 1).Value 'this code get value from main sheet
End If
Next xCll
End Sub
Because your separate workbook is all reference from your main workbook so i think set those in one wb will be more convenient, when you separate them, you need make sure your destination wb path not change or you should use file picker to pick it each time run macro, but when you just have 1 destination wb i think it will inconvenience
 
Upvote 0
try this, i don't test it yet but you can and feddback to me:
VBA Code:
Sub test()
    Dim shMain As Worksheet
    Dim desSh As Worksheet
    Dim Lr As Long
    Dim xRng As Range
    Dim xCll As Range
    Dim CriteriaRng As Range
    Dim RefRng As Range
    Dim desCll As Range
    Set shMain = ThisWorkbook.Sheets("main") 'this is sheet "main"
    Lr = shMain.Cells(Rows.Count, 1).End(xlUp).Row
    Set xRng = shMain.Range("A2:A" & Lr) 'this is ID Number range
    For Each xCll In xRng.Offset(, 3)  'loop each value in Type range
        If Not IsEmpty(xCll) Then
            Set desSh = ThisWorkbook.Sheets(xCll.Value) 'this sheet has same name as type so you have to make sure all your separate sheet have excatly name as each Type
            Set CriteriaRng = desSh.Range("A4:A99") 'this is criteria range in separate sheet, becase i don't know how many criteria in your main sheet so i set it to 99 criteria
            Set RefRng = desSh.Range("B2:AY2") 'this reference range i set to 50 reference same as above
            Set desCll = desSh.Range(CriteriaRng.Find(xCll.Offset(, 2)).Row, RefRng.Find(xCll.Offset(, -2)).Column) 'this is the cell need to find with match criteria and reference
            desCll.Value = "ID Number: " & xCll.Offset(, -3).Value & Chr(10) & _
            "Period: " & xCll.Offset(, -1).Value & Chr(10) & "Comment: " & xCll.Offset(, 1).Value 'this code get value from main sheet
        End If
    Next xCll
End Sub
I finally decided to have all in one wb, thus tried above one. Unfortunately, didn't work properly. Smth is wrong. Will try to understand what's exactly.....Thanks
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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