Consulting

Results 1 to 9 of 9

Thread: Subscript out of range error when trying to OR two arrays together.

  1. #1
    VBAX Regular
    Joined
    Jun 2017
    Posts
    23
    Location

    Subscript out of range error when trying to OR two arrays together.

    I have two arrays of the same size, and trying to bit-wise OR them into another array of the same size. When the program gets to the line of code that does the or I'm getting run time error 9: Subscript out of range.

    Sub zap_chip()
        Dim zap_mask(0 To 15) As Long
        Dim verify_low(0 To 15) As Long
        Dim verify_high(0 To 15) As Long
        Dim error_mask(0 To 15) As Long
        Dim zap_req As Boolean
                
        ulMHL200Error = MHL200_GetRegister(ulMHL200Handle, &H0, verify_low(0))
        .
        .
        .
        Array getting populated, just showing one to save space
        
        Dim k As Integer
        For k = 0 To 15 Step 1
            error_mask(k) = verify_low(k)
            verify_high(k) = verify_low(k)
        Next k    
       
        MsgBox "Testing Low Verification" & vbNewLine & "Press Any Key To Continue."
      
        ulMHL200Error = MHL200_ZapVerify(ulMHL200Handle, 0, verify_low, error_mask)
        
        If (ulMHL200Error = eMHL200_OK) Then
            MsgBox "Low Verification Complete" & vbNewLine & "Press Any Key To Continue To High Verification"
            ulMHL200Error = MHL200_ZapVerify(ulMHL200Handle, 1, verify_high, error_mask)
        End If
        
        Dim i As Integer
        If (ulMHL200Error = eMHL200_ZAP_IMPOSSIBLE) Then
            MsgBox "Zapping Impossible. An Already zapped bit can not be 0."
            For i = 0 To 15 Step 1
                If (error_mask(i) <> 0) Then
                    MsgBox "Error Mask Register 0x" & i & vbNewLine & ":0x" & error_mask(i)
                End If
            Next i
            Exit Sub
                    
        ElseIf (ulMHL200Error <> eMHL200_OK) Then
            MsgBox "An Error Occured During Verification." & vbNewLine & "Error Code:" & ulMHL200Error
            Exit Sub      
            
        ElseIf (ulMHL200Error = eMHL200_OK) Then
            MsgBox "Verification Complete" & vbNewLine & "Press Any Key to Continue Zapping"
        End If
        
        zap_req = False
        For i = LBound(zap_mask) To UBound(zap_mask)
        MsgBox "test 1"
        zap_mask(i) = verify_low(i) Or verify_high(i)
            If (zap_mask(i) <> 0) Then
                MsgBox "test 2"
                zap_req = True
            End If
        Next i
        
        If (zap_req = True) Then
            ulMHL200Error = MHL200_ZapEx(ulMHL200Handle, zap_mask)
            If (ulMHL200Error = eMHL200_OK) Then
                MsgBox "Zapping Complete"
            Else: MsgBox "An Error occured during Zapping"
            End If
        Else: MsgBox "All Bits Are Zapped"
        End If
    End Sub
    The line in red is what is giving me the error. Test 1 is displayed only once, so the programing is failing the second it hits that line. Not sure what can be causing it, as from what I saw online it mostly means something is either out of range, or named wrong which I don't see in this case. The section in the middle is just error checking, and should have no affect on the arrays.

    Edit: just realized the line didn't get highlighted that is throwing the error.

    >>>zap_mask(i) = verify_low(i) Or verify_high(i)

    That is the line that is throwing the subscript error
    Last edited by Zuccj; 07-07-2017 at 05:36 AM.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try

    Dim zap_mask(0 To 15) As Variant

    It looks like zap_mask(i) = verify_low(i) Or verify_high(i) will return Boolean
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Second thoughts
    verify_low(i) and verify_high(i) are both Long
    Is your code in effect saying
    zap_mask(i) = 5 or 10
    Sub Test()
    Dim k
    k = 5 Or 10
    MsgBox k
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Jun 2017
    Posts
    23
    Location
    That could be what is causing it. I'm still new to vba is there a way to or two numbers together and not get a boolean result? For each element of verify low and high it can be a number anywhere from 0 to 255. I want zap_mask to be 0-255 as well, but just a bitwise or between the two values.

  5. #5
    VBAX Regular
    Joined
    Jun 2017
    Posts
    23
    Location
    The reason they are long is the dll I'm using is expecting a long in the function calls.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    OR is adding the values. See this simplified test code
    Sub zap_chip()
        Dim zap_mask(0 To 15) As Long
        Dim verify_low(0 To 15) As Long
        Dim verify_high(0 To 15) As Long
        Dim error_mask(0 To 15) As Long
        Dim zap_req As Boolean
         
        Dim k As Integer
        For k = 0 To 15 Step 1
            verify_low(k) = Int(100 * Rnd())
            verify_high(k) = Int(100 * Rnd() + 100)
        Next k
             
        zap_req = True
        For i = LBound(zap_mask) To UBound(zap_mask)
            zap_mask(i) = verify_low(i) Or verify_high(i)   'ERROR
            If (zap_mask(i) <> 0) Then
                msg = msg & verify_low(i) & "; " & verify_high(i) & "; " & zap_mask(i) & vbCr
            End If
        Next i
        MsgBox msg
        
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Jun 2017
    Posts
    23
    Location
    It is doing an or operation for me when I run it. Using that test code:

    first number 10 in binary: 00001010
    second number 179 in binary: 10110011
    result 187 in binary: 10111011

    10111011 is the or of 00001010 and 10110011.

    Edit: that code did compile and run though, so I guess I'll start commenting out lines and see if why it is throwing an error in my code.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I've not used Binary in VBA. If I convert my simple test to Binary inputs, OR is not giving a Binary result according to the msg line but I'm probably missing something.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,743
    Location
    Q: You have verify_low ( 0..15 ) As Long. Does that represent 16 32-bit Longs, or are you only interested in the low order 16 bits of a single 32-bit Long?

    Q: Since we don't have your DLL, can you make a test WB with representative data that does show the issue but delete lines that don't cause a problem?

    Maybe add a testing line like

    verify_low = Array(12,14,16,19......, 15)
    Q: You said it have to be a Long for DLL calling, but are you really only interested in byte values (0..255)?
    ---------------------------------------------------------------------------------------------------------------------

    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

Posting Permissions

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