Best Fit Simulation

raven1124

New Member
Joined
Jun 13, 2017
Messages
29
Hello Guys,

I'm trying to simulate first fit and best fit simulation algorithm (memory management) using macro and I'm having a problem with the function I created.
The function has no bug but it only works once and once it's done, it will no longer scan the four labelbox I added and the "winner" integer will remain "0"


Code:
Private Sub BF_Btn_Add_Click()


Dim x As Integer


Dim proname As String
Dim prosize As Double
Dim winner As Double

'For my iteration to get the lowest value possible
winner = 1E+308


Dim result() As Double


'Prompts------------
proname = LCase(InputBox("Please enter the name of your process", "What's the name of your process?"))
prosize = InputBox("Please enter the size of your process", "What's the size of your process?")


'Loop will store the values of four textboxes and subtract the prosize. If the given process box (labelbox) has &HBF00& color, then it means that there is already a process occupied there
For x = 1 To 4
    
    If Controls("BF_lbl_Process" & x).BackColor <> &HBF00& Then
        ReDim Preserve result(x)
        result(x) = CDbl(Controls("BF_Text" & x).Text) - prosize
    
    End If
    
Next x



'Loop will check the array and store the lower to "winner"
For x = 1 To UBound(result)
    If result(x) < winner And result(x) >= 0 Then
        winner = result(x)
    End If
Next x

'Check if it is the lowest
MsgBox ("Winner: " & winner)


'Loop will then look for the specific process box where the winner was found based from winner and change the labelbox property to make sure that it will not be included in the next loop
For x = 1 To 4
    
    MsgBox (CDbl(Controls("BF_Text" & x).Text) & vbCrLf & prosize & vbCrLf & CDbl(Controls("BF_Text" & x).Text) + prosize)
    
    If CDbl(Controls("BF_Text" & x).Text) - prosize = winner Then
    MsgBox ("True")
        If Controls("BF_lbl_Process" & x).BackColor <> &HBF00& Then
            With Controls("BF_lbl_Process" & x)
                .Caption = proname
                .BackColor = &HBF00&
            End With


            Controls("BF_lbl_IF" & x).Caption = CDbl(Controls("BF_Text" & x).Text) - prosize


            Exit For
        End If
    End If




Next x
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello Dave,

I tried addiing the that line of code you recommended but it didn't change the function.

It's still works once then the second run will have all 0 returning :(

But thank you for trying I learned a new thing thanks to you
 
Upvote 0
UPDATE:

It seems that my first loop has the problem
The first run does store the values but in the second run, it only returns 0s

If someone can help me with this, I'll be grateful
 
Upvote 0
Sorry I missed this...
Code:
Dim result() As Variant
U can trial this to see if there's anything in your array...
Code:
For x = Lbound(result) To UBound(result)
msgbox result(x)
Next x
Not sure why your doing your last loop. Once U determine your winner using the array then U know what control U want to change the caption for. Dave
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,972
Members
448,537
Latest member
Et_Cetera

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