Results 1 to 2 of 2

Thread: [RESOLVED] VBA Excel 2007: Dynamic Arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    14

    Resolved [RESOLVED] VBA Excel 2007: Dynamic Arrays

    Hey everyone,

    I am working on a code that will add a cell that contains the word "Total" to an array and skip everything else. I could conceivably just delete the rows of everything that doesn't contain "Total" and create an array that way but I am trying to avoid deleting data. This code works sometimes and others it does not. Any sight would be very much appreciated. Thanks!!!!!!!

    Code:
    Sub companyarray()
    
    Dim myarray() As String
    Dim index As Integer
    Dim xrow As Integer
    Dim size As Integer
    xrow = 2
    size = 1
    index = 0 
    
    ReDim myarray(size)
    
    
    Sheets("Pivot Information").Select
    
    Range("F3").Select
    
    ''''''FILL ARRAY
    Do Until Cells(xrow, 6).Value = ""
    
    If ActiveCell.Value Like "*Total*" Then
        Cells(xrow, 6).Select
        myarray(index) = Cells(xrow, 6).Value
        
        size = size + 1
        ReDim Preserve myarray(size)
        index = index + 1
        xrow = xrow + 1
    Else:
        xrow = xrow + 1
    
    ActiveCell.Offset(1).Select
    End If
    Loop

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Excel 2007: Dynamic Arrays

    avoid using select, selection, activate or active anything
    try like

    vb Code:
    1. index = 0
    2. redim myarray(index)
    3. with Sheets("Pivot Information")
    4.  
    5. ''''''FILL ARRAY
    6. Do Until .Cells(xrow, 6).Value = ""
    7.  
    8. If .cells(xrow, 6).Value Like "*Total*" Then
    9.     myarray(index) = .Cells(xrow, 6).Value
    10.      index = index + 1
    11.    ReDim Preserve myarray(index)
    12. End If
    13. xrow = xrow +1
    14. Loop
    15. end with
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width