Nested loops - multiple conditions

elysium

New Member
Joined
Feb 6, 2016
Messages
13
Hi All, seeking help for a possible VBA solution to a problem I am facing.

I will have 3 worksheets

First worksheet
Blank customer = applies to all customers
Blank product = applies to all products


MarketCustomerProductSiteRatio
USNew York0.7
USOrlando0.3
US999New York1
USAAA111New York1
USBBB222Orlando0.6
USBBB222New York0.4


Second worksheet is where all my customers are listed

MarketCustomer
USAAA
USBBB
USCCC
USDDD
USEEE


The 3rd worksheet is where the results will generate after macro runs:

I am still trying to figure out how to write this nested loop logic, any help would be much appreciated!

MarketCustomerProductSiteRatio
USAAANew York0.7
USAAAOrlando0.3
USBBBNew York0.7
USBBBOrlando0.3
USCCCNew York0.7
USCCCOrlando0.3
USDDDNew York0.7
USDDDOrlando0.3
USEEENew York0.7
USEEEOrlando0.3
USAAA999New York1
USBBB999New York1
USCCC999New York1
USDDD999New York1
USEEE999New York1
USAAA111New York1
USBBB222Orlando0.6
USBBB222New York0.4
 
Wow, 58k lines in just 2 seconds. Thank you so so much! I will make sure to break this down and learn it ?

Try this:
I assumed:
- data is in sheet1, sheet2, sheet3
- data in "sheet2" is sorted by col A (Market).
VBA Code:
Sub a1118750a()
'https://www.mrexcel.com/board/threads/nested-loops-multiple-conditions.1118750/#post-5393973

Dim i As Long, j As Long, k As Long
Dim va, vb, x
Dim d As Object

Application.ScreenUpdating = False
Sheets("Sheet2").Activate
Set d = CreateObject("scripting.dictionary")
d.CompareMode = vbTextCompare

va = Range("A1:B" & Cells(Rows.Count, "A").End(xlUp).Row)
For i = 2 To UBound(va, 1)
j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    i = i - 1

    d(va(i, 1)) = Application.Transpose(Range(Cells(j, "B"), Cells(i, "B")).Value)

Next

Sheets("Sheet1").Activate
va = Range("A2:E" & Cells(Rows.Count, "A").End(xlUp).Row)
ReDim vb(1 To 1000000, 1 To 5)

    For i = 1 To UBound(va, 1)
        If d.Exists(va(i, 1)) Then
            If va(i, 2) = "" Then
                For Each x In d(va(i, 1))
                    k = k + 1
                    vb(k, 1) = va(i, 1)
                    vb(k, 2) = x
                    vb(k, 3) = va(i, 3)
                    vb(k, 4) = va(i, 4)
                    vb(k, 5) = va(i, 5)
                Next
           
            Else
                    k = k + 1
                    vb(k, 1) = va(i, 1)
                    vb(k, 2) = va(i, 2)
                    vb(k, 3) = va(i, 3)
                    vb(k, 4) = va(i, 4)
                    vb(k, 5) = va(i, 5)
       
            End If
       
        End If
   
    Next

Sheets("Sheet3").Activate
Cells.ClearContents
Range("A1").Resize(, 5).Value = Sheets("Sheet1").Range("A1").Resize(, 5).Value
Range("A2").Resize(k, 5) = vb

Application.ScreenUpdating = True
End Sub

Example:
Book1
ABCDE
1MarketCustomerProductSiteRatio
2USNew York0.7
3USOrlando0.3
4US999New York1
5USAAA111New York1
6USBBB222Orlando0.6
7USBBB222New York0.4
8ChinaBeijing3
9ChinaGGG77Beijing4
10ChinaBBB88Hong Kong5
Sheet1

Book1
AB
1MarketCustomer
2ChinaAAA
3ChinaBBB
4ChinaGGG
5USAAA
6USBBB
7USCCC
8USDDD
9USEEE
Sheet2

Book1
ABCDE
1MarketCustomerProductSiteRatio
2USAAANew York0.7
3USBBBNew York0.7
4USCCCNew York0.7
5USDDDNew York0.7
6USEEENew York0.7
7USAAAOrlando0.3
8USBBBOrlando0.3
9USCCCOrlando0.3
10USDDDOrlando0.3
11USEEEOrlando0.3
12USAAA999New York1
13USBBB999New York1
14USCCC999New York1
15USDDD999New York1
16USEEE999New York1
17USAAA111New York1
18USBBB222Orlando0.6
19USBBB222New York0.4
20ChinaAAABeijing3
21ChinaBBBBeijing3
22ChinaGGGBeijing3
23ChinaGGG77Beijing4
24ChinaBBB88Hong Kong5
Sheet3
 
Upvote 0

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're welcome, glad to help, & thanks for the feedback.:)
Note: you don't need to quote the whole post, 1-4 lines should be enough.
 
Upvote 0
Sorry for not elaborating but I am on a company laptop and do not have MSAccess available.. Can I do this on spyder?
Spyder (Python) of course you can, Python is the best, take a look to Pandas Library
 
Upvote 0
Hi Kennypete,

Thank you! This works but unfortunately I have easily over a few hundred customers and I gave up after running this for 20 mins as excel stops responding.

Hoping there is a faster solution.

Regards,
Erica

Hi Erica, my (and it seems others') view is Excel/VBA is ugly for doing this because what you are really doing here is chunky processing (for a much larger dataset). Others made suggestions such as MS Access, which you say is not an option for you, so...

A better solution anyway would be using a language with pattern matching strengths to build shelves/arrays for the sheet1 and sheet2, Python (you asked about Spyder) or Perl perhaps, and minipulate those to product sheet3. If you can run it, check out nested-loops-multiple-conditions.1118750.zip where I illustrate your solution in a standalone Omnimark language procedure (more correctly the free OMLE lite edition, which is decades old now, but hey, it works well still even in Win10). Presuming your PC is not so locked down that you cannot run .exe files or .bat files, if you extract this .zip file to any folder and run the sheet3.bat it will process sheet1.csv and sheet2.csv creating your sheet3.csv, which can then be opened up in Excel. (It would be a simple matter to invoke this with VBA (i.e. have VBA save down your two sheets to a folder, run the OMLE script outside Excel, and reopen the sheet3.csv output)

Incidentally this script will handle any combinations of Markets, plus it will handle quotes in the customer names (which Excel will add if it is something like "ACME & Co., Ltd" because it has to escape out the comma in the cell). I tried it by adding a few hundred customers and some more products and it was all good. Bonus: I expect (given Omnimark's inherent stream processing speed) it will run your hundreds of customers in a few seconds, not minutes or hours, so if you replace sheet1.csv and sheet2.csv with your data it should be lightning fast).

1.jpg
 
Upvote 0

Forum statistics

Threads
1,223,983
Messages
6,175,777
Members
452,668
Latest member
mrider123

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