View Full Version : [SOLVED:] error after change const to intger
talhv4
08-18-2015, 01:46 AM
hey, need your help with my project plz.
my project begins with 
Const NUM_MOLECULES As Byte = 10 'start of main sub need to be Insert Number of Monomers in userform2 
Const LIMIT_HIGH = 10 need to be Insert Matrix Size in userform 2
Const LIMIT_LOW = 1
Const NUM_CYCLES As Integer = 200
but i need NUM_MOLECULES and LIMIT_HIGH to be Variables that the user input in userform 2 but every time I change it appears to me as error
plz. help.
Paul_Hossler
08-19-2015, 09:32 AM
I haven't tried to follow your code, but I think you need to change to ...
'changed
Public NUM_MOLECULES As Byte
Public LIMIT_HIGH As Long
Public LIMIT_LOW As Long
Public NUM_CYCLES As Integer
 
... and in the correct places, set the initial values, get the update them from the user form, and then use the values as needed
talhv4
08-20-2015, 01:16 AM
hey tnx.
but did not work for me.
the line "Dim molecules(1 To 2, 1 To NUM_MOLECULES) As molecule "
has to have const and i dont know with what to Replace it
if anyone have an idea its Welcome
I have until tonight to submit this job please help
use:
Dim molecules(1 To 2, 1 To 10)
Sub Example()
Dim x
x = 3
Dim y As Variant 'Variant required
ReDim y(1 To 2, 1 To x)
Dim Z As Molecule
For i = 1 To x
Z.Size = 1
Z.Color = red
Z.Matched = True
y(2, i) = Z
Next i
End Sub
mikerickson
08-20-2015, 06:45 AM
hey tnx.
but did not work for me.
the line "Dim molecules(1 To 2, 1 To NUM_MOLECULES) As molecule "
has to have const and i dont know with what to Replace it
if anyone have an idea its Welcome
I have until tonight to submit this job please help
If NUM_MOLECULES is a variable you can code that like
Dim molecules() As molecule
'...
ReDim molecules(1 to 2, 1 to NUM_MOLECULES)
Paul_Hossler
08-20-2015, 07:07 AM
I haven't tried to follow your code, ... 
I'm SURE that there's a lot more changes that will be required to initialize the settings, display on the userform, take the userform responses, update variables and use the updated settings
Not sure which textbox maps to which variable.
change the button click
Private Sub CommandButton1_Click() 'excecute
    Dim w, h
    
    w = Me.Controls("width")
    h = Me.Controls("height")
    If Not (IsNumeric(w) And IsNumeric(h)) Then
        MsgBox "height or width is not valid", vbExclamation + vbOKOnly
        Exit Sub
    End If
    NUM_MOLECULES = w
    LIMIT_HIGH = h
    Unload Me
    Finala
End Sub
and change these in the module.
Dim dblMyCount As Double
Public NUM_MOLECULES As Byte
Public LIMIT_HIGH As Byte
Public LIMIT_LOW As Byte
Dim molecules() As molecule
Sub Finala() 'main sub
    LIMIT_LOW = 1
    
    If NUM_MOLECULES = 0 Or LIMIT_HIGH = 0 Then
        MsgBox "Limits out of bounds", vbExclamation + vbOKOnly
        Exit Sub
    End If
    
    running = Not running '?????????????????????????????????????????
    If Not running Then Exit Sub
    Dim i As Integer
    resetall
    Do Until AllMatched Or NoMatches
        Update
        If Not running Then Exit Sub
        Application.Wait Now() + TimeValue("00:00:01")
        DoEvents
        If dblMyCount = ((NUM_MOLECULES / 100) * 80) Then Exit Sub
    Loop
    Cells(8, 12) = "no more matches"
    running = False
End Sub
Sub Update()
    Dim i As Integer, j As Integer, m As Byte
    For i = LBound(molecules, 1) To UBound(molecules, 1)
        For j = LBound(molecules, 2) To UBound(molecules, 2)
            If Not molecules(i, j).matched Then
                m = 1
                Select Case True
                Case cycle = 0
                     'initialise
                    molecules(i, j).val = Application.WorksheetFunction.RandBetween(LIMIT_LOW, LIMIT_HIGH)
                    molecules(i, j).mvdir = CBool(Application.WorksheetFunction.RandBetween(0, 1))
                     
                Case Not molecules(i, j).mvdir And molecules(i, j).val > LIMIT_LOW
                     'moving down
                    molecules(i, j).val = molecules(i, j).val + -m
                     
                Case molecules(i, j).mvdir And molecules(i, j).val < LIMIT_HIGH
                     'moving up
                    molecules(i, j).val = molecules(i, j).val + m
                     
                Case Not molecules(i, j).mvdir And molecules(i, j).val <= LIMIT_LOW
                     'hit low, start moving up
                    molecules(i, j).val = LIMIT_LOW + m
                    molecules(i, j).mvdir = Not molecules(i, j).mvdir
                     
                Case molecules(i, j).mvdir And molecules(i, j).val >= LIMIT_HIGH
                     'hit high, start moving down
                    molecules(i, j).val = LIMIT_HIGH + -m
                    molecules(i, j).mvdir = Not molecules(i, j).mvdir
                     
                End Select
                 
                Cells(i + 1, j).Formula = molecules(i, j).val
            End If
        Next
        DoEvents
    Next
     
    SetCell2
     
    If cycle Then Cells(8, 12) = "cycle: " & cycle Else Cells(8, 12) = "initialising..."
    Cells(8 + cycle, 3).Value = cycle ' chart cycle   
    Dim rngMyRange As Range, rngMyCell As Range
    Set rngMyRange = Range("A2:j2") 'Range to count cells coloured with vbBlue.
    dblMyCount = 0
    For Each rngMyCell In rngMyRange
        If rngMyCell.Interior.color = vbBlue Then dblMyCount = dblMyCount + 1
    Next
    Set rngMyRange = Nothing
    
    Cells(8 + cycle, 4).Value = NUM_MOLECULES - dblMyCount
    Cells(8 + cycle, 5).Value = (LIMIT_HIGH * LIMIT_HIGH) / (Cells(8 + cycle, 4).Value)
    cycle = cycle + 1
End Sub
Sub resetall()
    Range("A2:j3").Interior.Pattern = xlNone
    dblMyCount = 0
    NoMatchCount = 0
    ReDim molecules(1 To 2, 1 To NUM_MOLECULES)
    Clear
    Square_Cell
    Table
    cycle = 0
    Dim i As Integer
    For i = 1 To NUM_MOLECULES
        Cells(1, i).Value = i
    Next
End Sub
talhv4
08-20-2015, 08:21 AM
Thank you all my software is working now.
Really saved my life.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.