Consulting

Results 1 to 9 of 9

Thread: error after change const to intger

  1. #1

    error after change const to intger

    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.
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    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

  4. #4
    use:

    Dim molecules(1 To 2, 1 To 10)

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    Last edited by SamT; 08-20-2015 at 07:17 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Quote Originally Posted by talhv4 View Post
    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)

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    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

  9. #9
    Thank you all my software is working now.
    Really saved my life.

Posting Permissions

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