+ Reply to Thread
Results 1 to 4 of 4

Variable Length Array does not bring correct data

Hybrid View

  1. #1
    Valued Forum Contributor AZ-XL's Avatar
    Join Date
    03-22-2013
    Location
    Azerbaijan, Baku
    MS-Off Ver
    Excel 2007
    Posts
    605

    Variable Length Array does not bring correct data

    Hi

    I want to create variable length array which should bring different data at each condition.

    My code is
    Sub Test()
    Dim r As Integer
    Dim c As Integer
    
    r = Application.WorksheetFunction.CountIf(Range("A1", Range("A1").End(xlDown)), Cells(1, 10))
    c = Range("A1", Range("A1").End(xlToRight)).Columns.Count
    
    
    ReDim temparray(1 To r, 1 To c)
    
    For ar = 1 To r
        For ac = 1 To c
            For i = 1 To Range("A5", Range("A5").End(xlDown)).Rows.Count
                If Cells(1, 10) = Cells(i, 1) Then
                    temparray(ar, ac) = Cells(i, ac)
                End If
            Next i
        Next ac
    Next ar
    
    Range(Cells(1, 6), Cells(r, 5 + c)) = temparray
    
    End Sub
    But I could not understand at which part I did mistake, because code brings data only one of criteria. Please correct me. I want to understand the code.

    Thank you in advance
    Attached Files Attached Files
    Appreciate the help? CLICK *

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Variable Length Array does not bring correct data

    Sub Test()
        Dim r As Long
        Dim c As Long
        Dim ar As Long
        Dim ac As Long
        Dim i As Long
        Dim temparray As Variant
        
        r = Application.WorksheetFunction.CountIf(Range("A1", Range("A1").End(xlDown)), Cells(1, 10))
        c = Range("A1").End(xlToRight).Column
        ReDim temparray(1 To r, 1 To c)
        
        For ar = 1 To Range("A1").End(xlDown).Row
            If Cells(1, 10) = Cells(ar, 1) Then
                i = i + 1
                For ac = 1 To c
                    temparray(i, ac) = Cells(ar, ac)
                Next ac
            End If
        Next ar
        
        Cells(1, 6).Resize(UBound(temparray, 1), UBound(temparray, 2)).Value = temparray
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Valued Forum Contributor AZ-XL's Avatar
    Join Date
    03-22-2013
    Location
    Azerbaijan, Baku
    MS-Off Ver
    Excel 2007
    Posts
    605

    Re: Variable Length Array does not bring correct data

    Thank you very much.

    Can you tell me what was my mistake?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,659

    Re: Variable Length Array does not bring correct data

    There were several mistakes.
    • Three nested loops when only two are needed (rows and columns)
    • You tested the criteria for every cell (all rows and all columns) when you only need to test the criteria for each row in the first column.
    • Your line For ar = 1 To r only looped through 4 rows as r was not the number of rows in your data. It was the count of the criteria match.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Bring scattered data to correct columns
    By vadaniels in forum Excel Formulas & Functions
    Replies: 16
    Last Post: 12-22-2020, 10:32 PM
  2. Replies: 5
    Last Post: 06-18-2015, 09:10 AM
  3. Array Formula Lookup - Bring back Lowest Date + Bring Back Cell Location
    By Matt1998 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-04-2014, 12:08 PM
  4. VBA variable length array help needed
    By Trinidad3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2010, 06:19 PM
  5. Passing arguments into function to bring data array from closed wb
    By Peter Rooney in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-08-2006, 10:55 AM
  6. [SOLVED] how do I do a variable length array based on the value in a cell
    By Mark Pranger in forum Excel General
    Replies: 1
    Last Post: 01-25-2006, 03:10 AM
  7. Graph with variable data length
    By snoach in forum Excel General
    Replies: 1
    Last Post: 05-27-2005, 05:15 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1