Excel 6 Columns Comparison











up vote
-3
down vote

favorite












I am trying to compare 6 columns, 3 columns in the One Sheet and 3 columns in theOther Sheet.



Thank you










share|improve this question
























  • Maybe you should provide an easier example. Why is store 4 show not found? I see it on both lists. Try to create a smaller example that is easier to follow.
    – urdearboy
    Sep 2 at 20:21










  • Thank you Urdearboy for your comment. Unfortunately the situation is even more complicated, I tired to make it easier in my example. Store 4 is showing "Not Found" because it's not listed in the Inventory Sheet for those items. Orange is not listed in the Inventory Sheet, that's why it's showing "Not Found" for all the Stores in the Orders Sheet. Thank you.
    – MyExcelJoe
    Sep 2 at 20:29












  • Agh. Sounds like a job for COUNTIFS which would yield 0 instead of “Not Found” for blank items
    – urdearboy
    Sep 2 at 20:35












  • Part of the problem is that many times items are not even listed at all in the Inventory Sheet, while other times are listed only for some stores.
    – MyExcelJoe
    Sep 2 at 20:42










  • Then the count will be 0 - I don’t see the problem with that unless I’m missing something
    – urdearboy
    Sep 2 at 20:44















up vote
-3
down vote

favorite












I am trying to compare 6 columns, 3 columns in the One Sheet and 3 columns in theOther Sheet.



Thank you










share|improve this question
























  • Maybe you should provide an easier example. Why is store 4 show not found? I see it on both lists. Try to create a smaller example that is easier to follow.
    – urdearboy
    Sep 2 at 20:21










  • Thank you Urdearboy for your comment. Unfortunately the situation is even more complicated, I tired to make it easier in my example. Store 4 is showing "Not Found" because it's not listed in the Inventory Sheet for those items. Orange is not listed in the Inventory Sheet, that's why it's showing "Not Found" for all the Stores in the Orders Sheet. Thank you.
    – MyExcelJoe
    Sep 2 at 20:29












  • Agh. Sounds like a job for COUNTIFS which would yield 0 instead of “Not Found” for blank items
    – urdearboy
    Sep 2 at 20:35












  • Part of the problem is that many times items are not even listed at all in the Inventory Sheet, while other times are listed only for some stores.
    – MyExcelJoe
    Sep 2 at 20:42










  • Then the count will be 0 - I don’t see the problem with that unless I’m missing something
    – urdearboy
    Sep 2 at 20:44













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I am trying to compare 6 columns, 3 columns in the One Sheet and 3 columns in theOther Sheet.



Thank you










share|improve this question















I am trying to compare 6 columns, 3 columns in the One Sheet and 3 columns in theOther Sheet.



Thank you







excel vba excel-vba excel-formula






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 4:29

























asked Sep 2 at 20:07









MyExcelJoe

145




145












  • Maybe you should provide an easier example. Why is store 4 show not found? I see it on both lists. Try to create a smaller example that is easier to follow.
    – urdearboy
    Sep 2 at 20:21










  • Thank you Urdearboy for your comment. Unfortunately the situation is even more complicated, I tired to make it easier in my example. Store 4 is showing "Not Found" because it's not listed in the Inventory Sheet for those items. Orange is not listed in the Inventory Sheet, that's why it's showing "Not Found" for all the Stores in the Orders Sheet. Thank you.
    – MyExcelJoe
    Sep 2 at 20:29












  • Agh. Sounds like a job for COUNTIFS which would yield 0 instead of “Not Found” for blank items
    – urdearboy
    Sep 2 at 20:35












  • Part of the problem is that many times items are not even listed at all in the Inventory Sheet, while other times are listed only for some stores.
    – MyExcelJoe
    Sep 2 at 20:42










  • Then the count will be 0 - I don’t see the problem with that unless I’m missing something
    – urdearboy
    Sep 2 at 20:44


















  • Maybe you should provide an easier example. Why is store 4 show not found? I see it on both lists. Try to create a smaller example that is easier to follow.
    – urdearboy
    Sep 2 at 20:21










  • Thank you Urdearboy for your comment. Unfortunately the situation is even more complicated, I tired to make it easier in my example. Store 4 is showing "Not Found" because it's not listed in the Inventory Sheet for those items. Orange is not listed in the Inventory Sheet, that's why it's showing "Not Found" for all the Stores in the Orders Sheet. Thank you.
    – MyExcelJoe
    Sep 2 at 20:29












  • Agh. Sounds like a job for COUNTIFS which would yield 0 instead of “Not Found” for blank items
    – urdearboy
    Sep 2 at 20:35












  • Part of the problem is that many times items are not even listed at all in the Inventory Sheet, while other times are listed only for some stores.
    – MyExcelJoe
    Sep 2 at 20:42










  • Then the count will be 0 - I don’t see the problem with that unless I’m missing something
    – urdearboy
    Sep 2 at 20:44
















Maybe you should provide an easier example. Why is store 4 show not found? I see it on both lists. Try to create a smaller example that is easier to follow.
– urdearboy
Sep 2 at 20:21




Maybe you should provide an easier example. Why is store 4 show not found? I see it on both lists. Try to create a smaller example that is easier to follow.
– urdearboy
Sep 2 at 20:21












Thank you Urdearboy for your comment. Unfortunately the situation is even more complicated, I tired to make it easier in my example. Store 4 is showing "Not Found" because it's not listed in the Inventory Sheet for those items. Orange is not listed in the Inventory Sheet, that's why it's showing "Not Found" for all the Stores in the Orders Sheet. Thank you.
– MyExcelJoe
Sep 2 at 20:29






Thank you Urdearboy for your comment. Unfortunately the situation is even more complicated, I tired to make it easier in my example. Store 4 is showing "Not Found" because it's not listed in the Inventory Sheet for those items. Orange is not listed in the Inventory Sheet, that's why it's showing "Not Found" for all the Stores in the Orders Sheet. Thank you.
– MyExcelJoe
Sep 2 at 20:29














Agh. Sounds like a job for COUNTIFS which would yield 0 instead of “Not Found” for blank items
– urdearboy
Sep 2 at 20:35






Agh. Sounds like a job for COUNTIFS which would yield 0 instead of “Not Found” for blank items
– urdearboy
Sep 2 at 20:35














Part of the problem is that many times items are not even listed at all in the Inventory Sheet, while other times are listed only for some stores.
– MyExcelJoe
Sep 2 at 20:42




Part of the problem is that many times items are not even listed at all in the Inventory Sheet, while other times are listed only for some stores.
– MyExcelJoe
Sep 2 at 20:42












Then the count will be 0 - I don’t see the problem with that unless I’m missing something
– urdearboy
Sep 2 at 20:44




Then the count will be 0 - I don’t see the problem with that unless I’m missing something
– urdearboy
Sep 2 at 20:44












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










This needs some re-factoring. This will repeat results for repeated items in orders.It uses dictionaries to hold the sums by stores using the items as keys.



Option Explicit
Public Sub GetInventoryForListedItems()
Application.ScreenUpdating = False
Dim wb As Workbook, orders As Worksheet, inventory As Worksheet

Set wb = ThisWorkbook
Set orders = wb.Worksheets("Orders")
Set inventory = wb.Worksheets("Inventory")

Dim store1Dict As Object, store2Dict As Object, store3Dict As Object, store4Dict As Object, orderList As Object
Set store1Dict = CreateObject("Scripting.Dictionary")
Set store2Dict = CreateObject("Scripting.Dictionary")
Set store3Dict = CreateObject("Scripting.Dictionary")
Set store4Dict = CreateObject("Scripting.Dictionary")
Set store5Dict = CreateObject("Scripting.Dictionary")
Set store6Dict = CreateObject("Scripting.Dictionary")
Set orderList = CreateObject("Scripting.Dictionary")

Dim ordersArray(), inventoryArray(), lastRowOrders As Long, lastRowInventory As Long, i As Long, ordersData As Range

With orders
lastRowOrders = .Cells(.Rows.Count, "A").End(xlUp).Row
Set ordersData = .Range("A2:A" & lastRowOrders)
Select Case lastRowOrders
Case Is < 2
Exit Sub
Case 2
ReDim ordersArray(1, 1): ordersArray(1, 1) = ordersData.Value
Case Else
ordersArray = ordersData.Value
End Select

For i = LBound(ordersArray, 1) To UBound(ordersArray, 1) 'dictionary of the orders to then search for in inventory
orderList(ordersArray(i, 1)) = vbNullString
Next
End With
With inventory
lastRowInventory = .Cells(.Rows.Count, "A").End(xlUp).Row
Select Case lastRowInventory
Case Is < 2
Exit Sub
Case 2
ReDim inventoryArray(1, 3)
inventoryArray(1, 1) = .Range("A2").Value
inventoryArray(1, 2) = .Range("B2").Value
inventoryArray(1, 3) = .Range("C2").Value
Case Else
inventoryArray = .Range("A2:C" & lastRowInventory).Value
End Select

For i = LBound(inventoryArray, 1) To UBound(inventoryArray, 1) 'check if inventory item in orders dictionary
If orderList.Exists(inventoryArray(i, 1)) And IsNumeric(inventoryArray(i, 2)) Then
Select Case inventoryArray(i, 3) ' add to dictionaries based on store
Case 1
store1Dict(inventoryArray(i, 1)) = store1Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 2
store2Dict(inventoryArray(i, 1)) = store2Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 3
store3Dict(inventoryArray(i, 1)) = store3Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 4
store4Dict(inventoryArray(i, 1)) = store4Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 5
store5Dict(inventoryArray(i, 1)) = store5Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 6
store6Dict(inventoryArray(i, 1)) = store6Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
End Select
End If
Next
End With
With orders
For i = LBound(ordersArray, 1) To UBound(ordersArray, 1)
On Error Resume Next
.Cells(i + 1, 11) = store1Dict(ordersArray(i, 1))
.Cells(i + 1, 12) = store2Dict(ordersArray(i, 1))
.Cells(i + 1, 13) = store3Dict(ordersArray(i, 1))
.Cells(i + 1, 14) = store4Dict(ordersArray(i, 1))
.Cells(i + 1, 15) = store5Dict(ordersArray(i, 1))
.Cells(i + 1, 16) = store6Dict(ordersArray(i, 1))
On Error GoTo 0
Next
.Range("E2:J" & lastRowOrders).Replace What:="", Replacement:="Not found"
End With
Application.ScreenUpdating = True
End Sub





share|improve this answer























  • Hi QHarr I hope you can take a look at the new question I posted today. Thank you
    – MyExcelJoe
    Sep 17 at 6:45










  • Looks like you have answers. If not,can you edit your question to make clear what is missing still?
    – QHarr
    Sep 17 at 7:36












  • QHarr I uploaded 2 pictures to explain my issue, and one in particular explains why the proposed solutions don't work. My original code works fine in pasting the result in the other Sheet, just it does not fill the other rows below when the macro is repeated. I think it's over writing. Thank you
    – MyExcelJoe
    Sep 18 at 0:09










  • I have uploaded another picture to better explain the issue with the solutions proposed by the others.
    – MyExcelJoe
    Sep 19 at 3:35










  • Hello QHarr I just posted a new question. I hope you can take a look at it... I posted a picture to explain what I need.
    – MyExcelJoe
    Nov 11 at 4:33













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52140639%2fexcel-6-columns-comparison%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










This needs some re-factoring. This will repeat results for repeated items in orders.It uses dictionaries to hold the sums by stores using the items as keys.



Option Explicit
Public Sub GetInventoryForListedItems()
Application.ScreenUpdating = False
Dim wb As Workbook, orders As Worksheet, inventory As Worksheet

Set wb = ThisWorkbook
Set orders = wb.Worksheets("Orders")
Set inventory = wb.Worksheets("Inventory")

Dim store1Dict As Object, store2Dict As Object, store3Dict As Object, store4Dict As Object, orderList As Object
Set store1Dict = CreateObject("Scripting.Dictionary")
Set store2Dict = CreateObject("Scripting.Dictionary")
Set store3Dict = CreateObject("Scripting.Dictionary")
Set store4Dict = CreateObject("Scripting.Dictionary")
Set store5Dict = CreateObject("Scripting.Dictionary")
Set store6Dict = CreateObject("Scripting.Dictionary")
Set orderList = CreateObject("Scripting.Dictionary")

Dim ordersArray(), inventoryArray(), lastRowOrders As Long, lastRowInventory As Long, i As Long, ordersData As Range

With orders
lastRowOrders = .Cells(.Rows.Count, "A").End(xlUp).Row
Set ordersData = .Range("A2:A" & lastRowOrders)
Select Case lastRowOrders
Case Is < 2
Exit Sub
Case 2
ReDim ordersArray(1, 1): ordersArray(1, 1) = ordersData.Value
Case Else
ordersArray = ordersData.Value
End Select

For i = LBound(ordersArray, 1) To UBound(ordersArray, 1) 'dictionary of the orders to then search for in inventory
orderList(ordersArray(i, 1)) = vbNullString
Next
End With
With inventory
lastRowInventory = .Cells(.Rows.Count, "A").End(xlUp).Row
Select Case lastRowInventory
Case Is < 2
Exit Sub
Case 2
ReDim inventoryArray(1, 3)
inventoryArray(1, 1) = .Range("A2").Value
inventoryArray(1, 2) = .Range("B2").Value
inventoryArray(1, 3) = .Range("C2").Value
Case Else
inventoryArray = .Range("A2:C" & lastRowInventory).Value
End Select

For i = LBound(inventoryArray, 1) To UBound(inventoryArray, 1) 'check if inventory item in orders dictionary
If orderList.Exists(inventoryArray(i, 1)) And IsNumeric(inventoryArray(i, 2)) Then
Select Case inventoryArray(i, 3) ' add to dictionaries based on store
Case 1
store1Dict(inventoryArray(i, 1)) = store1Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 2
store2Dict(inventoryArray(i, 1)) = store2Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 3
store3Dict(inventoryArray(i, 1)) = store3Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 4
store4Dict(inventoryArray(i, 1)) = store4Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 5
store5Dict(inventoryArray(i, 1)) = store5Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 6
store6Dict(inventoryArray(i, 1)) = store6Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
End Select
End If
Next
End With
With orders
For i = LBound(ordersArray, 1) To UBound(ordersArray, 1)
On Error Resume Next
.Cells(i + 1, 11) = store1Dict(ordersArray(i, 1))
.Cells(i + 1, 12) = store2Dict(ordersArray(i, 1))
.Cells(i + 1, 13) = store3Dict(ordersArray(i, 1))
.Cells(i + 1, 14) = store4Dict(ordersArray(i, 1))
.Cells(i + 1, 15) = store5Dict(ordersArray(i, 1))
.Cells(i + 1, 16) = store6Dict(ordersArray(i, 1))
On Error GoTo 0
Next
.Range("E2:J" & lastRowOrders).Replace What:="", Replacement:="Not found"
End With
Application.ScreenUpdating = True
End Sub





share|improve this answer























  • Hi QHarr I hope you can take a look at the new question I posted today. Thank you
    – MyExcelJoe
    Sep 17 at 6:45










  • Looks like you have answers. If not,can you edit your question to make clear what is missing still?
    – QHarr
    Sep 17 at 7:36












  • QHarr I uploaded 2 pictures to explain my issue, and one in particular explains why the proposed solutions don't work. My original code works fine in pasting the result in the other Sheet, just it does not fill the other rows below when the macro is repeated. I think it's over writing. Thank you
    – MyExcelJoe
    Sep 18 at 0:09










  • I have uploaded another picture to better explain the issue with the solutions proposed by the others.
    – MyExcelJoe
    Sep 19 at 3:35










  • Hello QHarr I just posted a new question. I hope you can take a look at it... I posted a picture to explain what I need.
    – MyExcelJoe
    Nov 11 at 4:33

















up vote
1
down vote



accepted










This needs some re-factoring. This will repeat results for repeated items in orders.It uses dictionaries to hold the sums by stores using the items as keys.



Option Explicit
Public Sub GetInventoryForListedItems()
Application.ScreenUpdating = False
Dim wb As Workbook, orders As Worksheet, inventory As Worksheet

Set wb = ThisWorkbook
Set orders = wb.Worksheets("Orders")
Set inventory = wb.Worksheets("Inventory")

Dim store1Dict As Object, store2Dict As Object, store3Dict As Object, store4Dict As Object, orderList As Object
Set store1Dict = CreateObject("Scripting.Dictionary")
Set store2Dict = CreateObject("Scripting.Dictionary")
Set store3Dict = CreateObject("Scripting.Dictionary")
Set store4Dict = CreateObject("Scripting.Dictionary")
Set store5Dict = CreateObject("Scripting.Dictionary")
Set store6Dict = CreateObject("Scripting.Dictionary")
Set orderList = CreateObject("Scripting.Dictionary")

Dim ordersArray(), inventoryArray(), lastRowOrders As Long, lastRowInventory As Long, i As Long, ordersData As Range

With orders
lastRowOrders = .Cells(.Rows.Count, "A").End(xlUp).Row
Set ordersData = .Range("A2:A" & lastRowOrders)
Select Case lastRowOrders
Case Is < 2
Exit Sub
Case 2
ReDim ordersArray(1, 1): ordersArray(1, 1) = ordersData.Value
Case Else
ordersArray = ordersData.Value
End Select

For i = LBound(ordersArray, 1) To UBound(ordersArray, 1) 'dictionary of the orders to then search for in inventory
orderList(ordersArray(i, 1)) = vbNullString
Next
End With
With inventory
lastRowInventory = .Cells(.Rows.Count, "A").End(xlUp).Row
Select Case lastRowInventory
Case Is < 2
Exit Sub
Case 2
ReDim inventoryArray(1, 3)
inventoryArray(1, 1) = .Range("A2").Value
inventoryArray(1, 2) = .Range("B2").Value
inventoryArray(1, 3) = .Range("C2").Value
Case Else
inventoryArray = .Range("A2:C" & lastRowInventory).Value
End Select

For i = LBound(inventoryArray, 1) To UBound(inventoryArray, 1) 'check if inventory item in orders dictionary
If orderList.Exists(inventoryArray(i, 1)) And IsNumeric(inventoryArray(i, 2)) Then
Select Case inventoryArray(i, 3) ' add to dictionaries based on store
Case 1
store1Dict(inventoryArray(i, 1)) = store1Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 2
store2Dict(inventoryArray(i, 1)) = store2Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 3
store3Dict(inventoryArray(i, 1)) = store3Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 4
store4Dict(inventoryArray(i, 1)) = store4Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 5
store5Dict(inventoryArray(i, 1)) = store5Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 6
store6Dict(inventoryArray(i, 1)) = store6Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
End Select
End If
Next
End With
With orders
For i = LBound(ordersArray, 1) To UBound(ordersArray, 1)
On Error Resume Next
.Cells(i + 1, 11) = store1Dict(ordersArray(i, 1))
.Cells(i + 1, 12) = store2Dict(ordersArray(i, 1))
.Cells(i + 1, 13) = store3Dict(ordersArray(i, 1))
.Cells(i + 1, 14) = store4Dict(ordersArray(i, 1))
.Cells(i + 1, 15) = store5Dict(ordersArray(i, 1))
.Cells(i + 1, 16) = store6Dict(ordersArray(i, 1))
On Error GoTo 0
Next
.Range("E2:J" & lastRowOrders).Replace What:="", Replacement:="Not found"
End With
Application.ScreenUpdating = True
End Sub





share|improve this answer























  • Hi QHarr I hope you can take a look at the new question I posted today. Thank you
    – MyExcelJoe
    Sep 17 at 6:45










  • Looks like you have answers. If not,can you edit your question to make clear what is missing still?
    – QHarr
    Sep 17 at 7:36












  • QHarr I uploaded 2 pictures to explain my issue, and one in particular explains why the proposed solutions don't work. My original code works fine in pasting the result in the other Sheet, just it does not fill the other rows below when the macro is repeated. I think it's over writing. Thank you
    – MyExcelJoe
    Sep 18 at 0:09










  • I have uploaded another picture to better explain the issue with the solutions proposed by the others.
    – MyExcelJoe
    Sep 19 at 3:35










  • Hello QHarr I just posted a new question. I hope you can take a look at it... I posted a picture to explain what I need.
    – MyExcelJoe
    Nov 11 at 4:33















up vote
1
down vote



accepted







up vote
1
down vote



accepted






This needs some re-factoring. This will repeat results for repeated items in orders.It uses dictionaries to hold the sums by stores using the items as keys.



Option Explicit
Public Sub GetInventoryForListedItems()
Application.ScreenUpdating = False
Dim wb As Workbook, orders As Worksheet, inventory As Worksheet

Set wb = ThisWorkbook
Set orders = wb.Worksheets("Orders")
Set inventory = wb.Worksheets("Inventory")

Dim store1Dict As Object, store2Dict As Object, store3Dict As Object, store4Dict As Object, orderList As Object
Set store1Dict = CreateObject("Scripting.Dictionary")
Set store2Dict = CreateObject("Scripting.Dictionary")
Set store3Dict = CreateObject("Scripting.Dictionary")
Set store4Dict = CreateObject("Scripting.Dictionary")
Set store5Dict = CreateObject("Scripting.Dictionary")
Set store6Dict = CreateObject("Scripting.Dictionary")
Set orderList = CreateObject("Scripting.Dictionary")

Dim ordersArray(), inventoryArray(), lastRowOrders As Long, lastRowInventory As Long, i As Long, ordersData As Range

With orders
lastRowOrders = .Cells(.Rows.Count, "A").End(xlUp).Row
Set ordersData = .Range("A2:A" & lastRowOrders)
Select Case lastRowOrders
Case Is < 2
Exit Sub
Case 2
ReDim ordersArray(1, 1): ordersArray(1, 1) = ordersData.Value
Case Else
ordersArray = ordersData.Value
End Select

For i = LBound(ordersArray, 1) To UBound(ordersArray, 1) 'dictionary of the orders to then search for in inventory
orderList(ordersArray(i, 1)) = vbNullString
Next
End With
With inventory
lastRowInventory = .Cells(.Rows.Count, "A").End(xlUp).Row
Select Case lastRowInventory
Case Is < 2
Exit Sub
Case 2
ReDim inventoryArray(1, 3)
inventoryArray(1, 1) = .Range("A2").Value
inventoryArray(1, 2) = .Range("B2").Value
inventoryArray(1, 3) = .Range("C2").Value
Case Else
inventoryArray = .Range("A2:C" & lastRowInventory).Value
End Select

For i = LBound(inventoryArray, 1) To UBound(inventoryArray, 1) 'check if inventory item in orders dictionary
If orderList.Exists(inventoryArray(i, 1)) And IsNumeric(inventoryArray(i, 2)) Then
Select Case inventoryArray(i, 3) ' add to dictionaries based on store
Case 1
store1Dict(inventoryArray(i, 1)) = store1Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 2
store2Dict(inventoryArray(i, 1)) = store2Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 3
store3Dict(inventoryArray(i, 1)) = store3Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 4
store4Dict(inventoryArray(i, 1)) = store4Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 5
store5Dict(inventoryArray(i, 1)) = store5Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 6
store6Dict(inventoryArray(i, 1)) = store6Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
End Select
End If
Next
End With
With orders
For i = LBound(ordersArray, 1) To UBound(ordersArray, 1)
On Error Resume Next
.Cells(i + 1, 11) = store1Dict(ordersArray(i, 1))
.Cells(i + 1, 12) = store2Dict(ordersArray(i, 1))
.Cells(i + 1, 13) = store3Dict(ordersArray(i, 1))
.Cells(i + 1, 14) = store4Dict(ordersArray(i, 1))
.Cells(i + 1, 15) = store5Dict(ordersArray(i, 1))
.Cells(i + 1, 16) = store6Dict(ordersArray(i, 1))
On Error GoTo 0
Next
.Range("E2:J" & lastRowOrders).Replace What:="", Replacement:="Not found"
End With
Application.ScreenUpdating = True
End Sub





share|improve this answer














This needs some re-factoring. This will repeat results for repeated items in orders.It uses dictionaries to hold the sums by stores using the items as keys.



Option Explicit
Public Sub GetInventoryForListedItems()
Application.ScreenUpdating = False
Dim wb As Workbook, orders As Worksheet, inventory As Worksheet

Set wb = ThisWorkbook
Set orders = wb.Worksheets("Orders")
Set inventory = wb.Worksheets("Inventory")

Dim store1Dict As Object, store2Dict As Object, store3Dict As Object, store4Dict As Object, orderList As Object
Set store1Dict = CreateObject("Scripting.Dictionary")
Set store2Dict = CreateObject("Scripting.Dictionary")
Set store3Dict = CreateObject("Scripting.Dictionary")
Set store4Dict = CreateObject("Scripting.Dictionary")
Set store5Dict = CreateObject("Scripting.Dictionary")
Set store6Dict = CreateObject("Scripting.Dictionary")
Set orderList = CreateObject("Scripting.Dictionary")

Dim ordersArray(), inventoryArray(), lastRowOrders As Long, lastRowInventory As Long, i As Long, ordersData As Range

With orders
lastRowOrders = .Cells(.Rows.Count, "A").End(xlUp).Row
Set ordersData = .Range("A2:A" & lastRowOrders)
Select Case lastRowOrders
Case Is < 2
Exit Sub
Case 2
ReDim ordersArray(1, 1): ordersArray(1, 1) = ordersData.Value
Case Else
ordersArray = ordersData.Value
End Select

For i = LBound(ordersArray, 1) To UBound(ordersArray, 1) 'dictionary of the orders to then search for in inventory
orderList(ordersArray(i, 1)) = vbNullString
Next
End With
With inventory
lastRowInventory = .Cells(.Rows.Count, "A").End(xlUp).Row
Select Case lastRowInventory
Case Is < 2
Exit Sub
Case 2
ReDim inventoryArray(1, 3)
inventoryArray(1, 1) = .Range("A2").Value
inventoryArray(1, 2) = .Range("B2").Value
inventoryArray(1, 3) = .Range("C2").Value
Case Else
inventoryArray = .Range("A2:C" & lastRowInventory).Value
End Select

For i = LBound(inventoryArray, 1) To UBound(inventoryArray, 1) 'check if inventory item in orders dictionary
If orderList.Exists(inventoryArray(i, 1)) And IsNumeric(inventoryArray(i, 2)) Then
Select Case inventoryArray(i, 3) ' add to dictionaries based on store
Case 1
store1Dict(inventoryArray(i, 1)) = store1Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 2
store2Dict(inventoryArray(i, 1)) = store2Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 3
store3Dict(inventoryArray(i, 1)) = store3Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 4
store4Dict(inventoryArray(i, 1)) = store4Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 5
store5Dict(inventoryArray(i, 1)) = store5Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
Case 6
store6Dict(inventoryArray(i, 1)) = store6Dict(inventoryArray(i, 1)) + inventoryArray(i, 2)
End Select
End If
Next
End With
With orders
For i = LBound(ordersArray, 1) To UBound(ordersArray, 1)
On Error Resume Next
.Cells(i + 1, 11) = store1Dict(ordersArray(i, 1))
.Cells(i + 1, 12) = store2Dict(ordersArray(i, 1))
.Cells(i + 1, 13) = store3Dict(ordersArray(i, 1))
.Cells(i + 1, 14) = store4Dict(ordersArray(i, 1))
.Cells(i + 1, 15) = store5Dict(ordersArray(i, 1))
.Cells(i + 1, 16) = store6Dict(ordersArray(i, 1))
On Error GoTo 0
Next
.Range("E2:J" & lastRowOrders).Replace What:="", Replacement:="Not found"
End With
Application.ScreenUpdating = True
End Sub






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 3 at 15:01

























answered Sep 2 at 21:16









QHarr

28k81839




28k81839












  • Hi QHarr I hope you can take a look at the new question I posted today. Thank you
    – MyExcelJoe
    Sep 17 at 6:45










  • Looks like you have answers. If not,can you edit your question to make clear what is missing still?
    – QHarr
    Sep 17 at 7:36












  • QHarr I uploaded 2 pictures to explain my issue, and one in particular explains why the proposed solutions don't work. My original code works fine in pasting the result in the other Sheet, just it does not fill the other rows below when the macro is repeated. I think it's over writing. Thank you
    – MyExcelJoe
    Sep 18 at 0:09










  • I have uploaded another picture to better explain the issue with the solutions proposed by the others.
    – MyExcelJoe
    Sep 19 at 3:35










  • Hello QHarr I just posted a new question. I hope you can take a look at it... I posted a picture to explain what I need.
    – MyExcelJoe
    Nov 11 at 4:33




















  • Hi QHarr I hope you can take a look at the new question I posted today. Thank you
    – MyExcelJoe
    Sep 17 at 6:45










  • Looks like you have answers. If not,can you edit your question to make clear what is missing still?
    – QHarr
    Sep 17 at 7:36












  • QHarr I uploaded 2 pictures to explain my issue, and one in particular explains why the proposed solutions don't work. My original code works fine in pasting the result in the other Sheet, just it does not fill the other rows below when the macro is repeated. I think it's over writing. Thank you
    – MyExcelJoe
    Sep 18 at 0:09










  • I have uploaded another picture to better explain the issue with the solutions proposed by the others.
    – MyExcelJoe
    Sep 19 at 3:35










  • Hello QHarr I just posted a new question. I hope you can take a look at it... I posted a picture to explain what I need.
    – MyExcelJoe
    Nov 11 at 4:33


















Hi QHarr I hope you can take a look at the new question I posted today. Thank you
– MyExcelJoe
Sep 17 at 6:45




Hi QHarr I hope you can take a look at the new question I posted today. Thank you
– MyExcelJoe
Sep 17 at 6:45












Looks like you have answers. If not,can you edit your question to make clear what is missing still?
– QHarr
Sep 17 at 7:36






Looks like you have answers. If not,can you edit your question to make clear what is missing still?
– QHarr
Sep 17 at 7:36














QHarr I uploaded 2 pictures to explain my issue, and one in particular explains why the proposed solutions don't work. My original code works fine in pasting the result in the other Sheet, just it does not fill the other rows below when the macro is repeated. I think it's over writing. Thank you
– MyExcelJoe
Sep 18 at 0:09




QHarr I uploaded 2 pictures to explain my issue, and one in particular explains why the proposed solutions don't work. My original code works fine in pasting the result in the other Sheet, just it does not fill the other rows below when the macro is repeated. I think it's over writing. Thank you
– MyExcelJoe
Sep 18 at 0:09












I have uploaded another picture to better explain the issue with the solutions proposed by the others.
– MyExcelJoe
Sep 19 at 3:35




I have uploaded another picture to better explain the issue with the solutions proposed by the others.
– MyExcelJoe
Sep 19 at 3:35












Hello QHarr I just posted a new question. I hope you can take a look at it... I posted a picture to explain what I need.
– MyExcelJoe
Nov 11 at 4:33






Hello QHarr I just posted a new question. I hope you can take a look at it... I posted a picture to explain what I need.
– MyExcelJoe
Nov 11 at 4:33




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52140639%2fexcel-6-columns-comparison%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()