Consulting

Results 1 to 4 of 4

Thread: "Subscript out of range" ... Issue

  1. #1

    "Subscript out of range" ... Issue

    So, below I have a Variant array. This is supposed to be a 126x12 array. I want to take the elements of this array and then to place them into a single column.

    Where the bold faced text is, I get an "Subscript out of range" error. I have no idea why. I get to the first element of the loop and it kicks out. Anyone have any idea why I would get such an error? The way I defined my matrices and variables seems correct and I know that the numbers match up correctly as well. .

    The issue lies within: BSAAMLValue(s, ZZ)

    Any help is appreciated.

    I placed comments in the code below to help illustrate where the problem is.

    Dim BSAAMLValue(126, 12) As Variant
    Dim main_count As Integer
    
    'This code takes elements from a worksheet and transfers them into an 126x12 array.
    For Z = 1 To 12
    
       For N = 1 To 126
                    BSAAMLValue(N, Z) = wb.Worksheets("BSA|AML").Cells(14 + (N - 1), Z + 6).Value
       Next N
    
    Next Z
    
    'This code takes those array elements and transfers them into a single column matrix. This is done using the main_count variable.
    main_count = 2
    For ZZ = 1 To 12
    
      For s = 1 To 126
                    mainwb.Worksheets("Import_Template").Cells(main_count, 8).Value = BSAAMLValue(s, ZZ)
                    main_count = main_count + 1
      Next s
    
    
    Next ZZ

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum.

    put Option Base 1 at top of the module.

    or declare the variable like Dim BSAAMLValue(1 To 126, 1 To 12) As Variant
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Option Base 1
    
    
    Sub test()
    
        Dim BSAAMLValue(126, 12) As Variant
        Dim i As Long, j As Long, main_count As Long
        
        'This code takes elements from a worksheet and transfers them into an 126x12 array.
        For j = 1 To 12
            For i = 1 To 126
                BSAAMLValue(i, j) = wb.Worksheets("BSA|AML").Cells(14 + (i - 1), j + 6).Value
            Next i
        Next j
        
        'This code takes those array elements and transfers them into a single column matrix. This is done using the main_count variable.
        main_count = 2
        For j = 1 To 12
            For i = 1 To 126
                mainwb.Worksheets("Import_Template").Cells(main_count, 8).Value = BSAAMLValue(i, j)
                main_count = main_count + 1
            Next i
        Next j
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    The first 2 loops are redundant:
    Reduce the reading/writing operations as much as possible.

    sub M_snb()
       sn =sheets("BSA|AML").Cells(14 ,6).resize(126,12)
       redim sp(1 to ubound(sn)*ubound(sn,2),0)
    
       for j=1 to ubound(sp)
         sp(j,0)=sn( (j -1) \ ubound(sn,2) +1,(j-1) mod ubound(sn,2)+1)
       next
    
       sheets("Import_Template").Cells(2, 8).resize(ubound(sp))=sp
    End sub

Posting Permissions

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