Results 1 to 28 of 28

Generating all possible combinations per macro

Threaded View

  1. #5
    Registered User
    Join Date
    09-30-2014
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    16

    Re: Generating all possible combinations per macro

    Hi, this is the example for N=5 products.
    You see that I built in 5 of the loops, according to N = 5. This needs to become flexible. Shelf-space is S=10 (can already be flexibly set - so this at least is working).

    I am sure the code is not efficient at all :-)

    I shortened the code, so may be that this is not fully working if you copy it - but it should.
    Also, I simplified the evaluation formula, which is resultF(ip) = F(ip)+1 in the below.

    Thank you!

    Best regards
    Kai


    Sub G_combinations()
    
    Dim shelf As Integer
    Dim used As Double
    Dim result As Double
    Dim noproducts As Integer
    Dim ip As Integer
    Dim profit As Double
    Dim result As Double
    Dim resultF() As Variant    
    Dim i As Integer
    Dim F() As Variant
    Dim best_F() As Variant
    
    
    noproducts = 5
    shelf = 10
    
    
    F(1) = -1
    F(2) = -1
    F(3) = -1
    F(4) = -1
    F(5) = -1
    used = 0
    profit = -100000
    
    
    Do While used < shelf
    F(1) = F(1) + 1
    used = Application.Max(0, F(1)) + Application.Max(0, F(2)) + Application.Max(0, F(3)) + Application.Max(0, F(4)) + Application.Max(0, F(5))
    
    If used = shelf Then
    
    For ip = 1 To noproducts
    
    
    
    resultF(ip) = F(ip)+ 1
    
    Next ip
    
    result = resultF(1) + resultF(2) + resultF(3) + resultF(4) + resultF(5)
    
    If result > profit Then
    profit = result
    best_F(1) = Application.Max(F(1), 0)
    best_F(2) = Application.Max(F(2), 0)
    best_F(3) = Application.Max(F(3), 0)
    best_F(4) = Application.Max(F(4), 0)
    best_F(5) = Application.Max(F(5), 0)
    
    Else
    profit = profit
    End If
    
    
    used = 0
    Exit Do
    End If
    
    
    
    Do While used < shelf
    F(2) = F(2) + 1
    used = Application.Max(0, F(1)) + Application.Max(0, F(2)) + Application.Max(0, F(3)) + Application.Max(0, F(4)) + Application.Max(0, F(5))
    
    If used = shelf Then
    
    
    For ip = 1 To noproducts
    
    resultF(ip) = F(ip)+ 1
    
    Next ip
    
    result = resultF(1) + resultF(2) + resultF(3) + resultF(4) + resultF(5)
    
    If result > profit Then
    profit = result
    best_F(1) = Application.Max(F(1), 0)
    best_F(2) = Application.Max(F(2), 0)
    best_F(3) = Application.Max(F(3), 0)
    best_F(4) = Application.Max(F(4), 0)
    best_F(5) = Application.Max(F(5), 0)
    
    Else
    profit = profit
    End If
    
    F(2) = -1
    used = 0
    Exit Do
    End If
    
    
    
    Do While used < shelf
    F(3) = F(3) + 1
    used = Application.Max(0, F(1)) + Application.Max(0, F(2)) + Application.Max(0, F(3)) + Application.Max(0, F(4)) + Application.Max(0, F(5))
    
    
    For ip = 1 To noproducts
    
    resultF(ip) = F(ip)+ 1
    
    Next ip
    
    result = resultF(1) + resultF(2) + resultF(3) + resultF(4) + resultF(5)
    
    If result > profit Then
    profit = result
    best_F(1) = Application.Max(F(1), 0)
    best_F(2) = Application.Max(F(2), 0)
    best_F(3) = Application.Max(F(3), 0)
    best_F(4) = Application.Max(F(4), 0)
    best_F(5) = Application.Max(F(5), 0)
    
    Else
    profit = profit
    End If
    
    
    If used = shelf Then
    F(3) = -1
    used = 0
    Exit Do
    End If
    
    Do While used < shelf
    F(4) = F(4) + 1
    used = Application.Max(0, F(1)) + Application.Max(0, F(2)) + Application.Max(0, F(3)) + Application.Max(0, F(4)) + Application.Max(0, F(5))
    
    
    
    For ip = 1 To noproducts
    
    resultF(ip) = F(ip)+ 1
    
    Next ip
    
    result = resultF(1) + resultF(2) + resultF(3) + resultF(4) + resultF(5)
    
    If result > profit Then
    profit = result
    best_F(1) = Application.Max(F(1), 0)
    best_F(2) = Application.Max(F(2), 0)
    best_F(3) = Application.Max(F(3), 0)
    best_F(4) = Application.Max(F(4), 0)
    best_F(5) = Application.Max(F(5), 0)
    
    Else
    profit = profit
    End If
    
    
    
    If used = shelf Then
    F(4) = -1
    used = 0
    Exit Do
    End If
    
    
    Do While used < shelf
    F(5) = F(5) + 1
    used = Application.Max(0, F(1)) + Application.Max(0, F(2)) + Application.Max(0, F(3)) + Application.Max(0, F(4)) + Application.Max(0, F(5))
    
    
    For ip = 1 To noproducts
    resultF(ip) = F(ip)+ 1
    Next ip
    
    result = resultF(1) + resultF(2) + resultF(3) + resultF(4) + resultF(5)
    
    If result > profit Then
    profit = result
    best_F(1) = Application.Max(F(1), 0)
    best_F(2) = Application.Max(F(2), 0)
    best_F(3) = Application.Max(F(3), 0)
    best_F(4) = Application.Max(F(4), 0)
    best_F(5) = Application.Max(F(5), 0)
    
    Else
    profit = profit
    End If
    
    If used = shelf Then
    F(5) = -1
    used = 0
    Exit Do
    End If
    Loop
    
    Loop
    Loop
    Loop
    Loop
    
    
    
    End Sub
    Last edited by KaNi2015; 09-30-2014 at 03:20 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. generating combinations
    By fern4500 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-11-2012, 08:47 AM
  2. Problem with generating Combinations (Myrna Larson macro)
    By Excelstudent12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2012, 04:28 AM
  3. Generating Combinations of Text
    By financeguy57 in forum Excel General
    Replies: 8
    Last Post: 10-19-2010, 06:42 PM
  4. [SOLVED] Help generating combinations
    By JamRock in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2006, 03:40 PM
  5. Generating excel combinations
    By mark4006 in forum Excel General
    Replies: 2
    Last Post: 03-06-2005, 01:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1