+ Reply to Thread
Results 1 to 20 of 20

Code loop through two tables checking and altering

Hybrid View

  1. #1
    Registered User
    Join Date
    08-30-2008
    Location
    London
    Posts
    35

    Code loop through two tables checking and altering

    Hi I have a looping code problem I was wondering if you could have a quick look for me.

    I have two tables of information. The first is a matrix with some distances. The second is a table of distribution. Ive been trying this for a long time now with limited success using a truth table but i've realised the only way to do this is in code. Ive got limited experience with this so please point me in the right direction.

    Ive written some steps explaining what each table does here -

    Step 1 Check for lowest value B4:F4 in Table 1 (in example is 10)
    Step 2 Check corresponding column destination available capacity in table 2 (Example 500)
    Step 3 Distribute as much as possible from source in table 2 (500)
    Step 4 Reduce value in capacity line by value taken from source
    Step 5 If some source remains move back to table 1 and find next nearest column Dest
    Step 6 Repeat step 2 until all source is gone in row
    Step 7 Check for lowest value B5:F5 in Table 1
    if run out of capacity at all sites stop code
    etc
    until Table 1 column B is empty

    Ive posted a spreadsheet with some before and after tables in it aswell. Its very small and formatted o its easy to see whats happening.

    Thanks for taking a look.
    Last edited by VBA Noob; 11-12-2008 at 05:57 PM. Reason: Marked as solved

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    excel_lover,

    Please read fourm rules below and then add the link to you're cross post

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    08-30-2008
    Location
    London
    Posts
    35
    Sorry VBA Noonb - I have posted this at mrexcel too.
    http://www.mrexcel.com/forum/showthread.php?t=352898

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    This works for your example.
    Sub x()
    
    Dim rngDistance As Range, rngDistr As Range
    Dim nMin As Long, nMinD As Long, r As Long, n As Long
    Dim wf As WorksheetFunction
    
    Set wf = WorksheetFunction
    Set rngDistance = Range("B4:F7")
    Set rngDistr = Range("B11:G15")
    
    For r = 1 To rngDistance.Rows.Count
        Do While rngDistr(r + 1, 1) > 0
            n = n + 1
            nMin = wf.Small(rngDistance.Rows(r), n)
            nMinD = wf.Index(rngDistr, 1, 1 + wf.Match(nMin, rngDistance.Rows(r), 0))
            If nMinD <= rngDistr(r + 1, 1) Then
                rngDistr(r + 1, 1 + wf.Match(nMin, rngDistance.Rows(r), 0)) = nMinD
                rngDistr(r + 1, 1) = rngDistr(r + 1, 1) - nMinD
            ElseIf nMinD > rngDistr(r + 1, 1) Then
                rngDistr(r + 1, 1 + wf.Match(nMin, rngDistance.Rows(r), 0)) = rngDistr(r + 1, 1)
                rngDistr(r + 1, 1) = 0
            End If
        Loop
        n = 0
    Next r
            
    End Sub

  5. #5
    Registered User
    Join Date
    08-30-2008
    Location
    London
    Posts
    35
    Thanks Stephen thats brilliant. Ive tried expanding it though and get an error telling me -

    "Unable to get the small property of the worksheet function class".

    When I debug this it highlights the row containing

    nMin = wf.Small(rngDistance.Rows(r), n)

    Ive posted my alterations below with the code alteration ive made. Where have i gone wrong?

    Thanks
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    I think just change this line so that it includes the row with the distribution amounts (you had B17):
    Set rngDistr = Range("B16:K22")
    The code now runs, although I haven't checked that it gives the right answer!

  7. #7
    Registered User
    Join Date
    08-30-2008
    Location
    London
    Posts
    35
    thanks a lot stephen thats some excellent coding

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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