+ Reply to Thread
Results 1 to 4 of 4

How to nest a 'for x = 1 to 4' with another 'for i = 1 to 100' statement in an array?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-16-2015
    Location
    Brighton, England
    MS-Off Ver
    2010
    Posts
    71

    How to nest a 'for x = 1 to 4' with another 'for i = 1 to 100' statement in an array?

    Hi Thanks for looking.

    I have an array --->
    Array(1 to 100, 1 to 4)
    & a counter filling the array

    For x = 1 to 100
          Array(x,1) = Worksheets("Data").Range("B1").offset(x,0)
    next x

    Is it possible: once Array(100,1) has been filled, I can loop it round to Array(1,2) and continue up to Array(100,2)

    My idea is

    For j = 1 to 4
       For x = 1 to 100
            Array(x,j) = Worksheets("Data").Range("B1").offset(x,j)
       Next x
    Next j

    I think the above code is issuing the commands in the wrong directions as my results are skewered. Please could you advise any other ideas for the scenario?

    Thanks for your help
    Last edited by graym463; 03-02-2016 at 12:06 PM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,912

    Re: How to nest a 'for x = 1 to 4' with another 'for i = 1 to 100' statement in an array?

    It looks correct to me. What do you mean by "my results are skewered"? The main "confusion" that I would expect is getting rows and columns transposed. As structured first dimension is for "rows" and the 2nd dimension is for "columns", is it possible that you intended them the other direction? 1 to 4 "rows" and 1 to 100 "columns"?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: How to nest a 'for x = 1 to 4' with another 'for i = 1 to 100' statement in an array?

    and continue up to Array(200,2)
    I assume you meant (100,2)?

    Either way, please post a sample data set and show how you would expect the array to be filled given that data. It doesn't need to be 100x4 big, but just big enough that we can see the pattern you are going for.
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  4. #4
    Registered User
    Join Date
    07-16-2015
    Location
    Brighton, England
    MS-Off Ver
    2010
    Posts
    71

    Re: How to nest a 'for x = 1 to 4' with another 'for i = 1 to 100' statement in an array?

    Hi Thanks guys. Yes sorry that's correct it was meant to be (100,2). I had been sending this data through to a list box which was displaying incorrectly.

    It was being transposed, thanks for pointing that out - it was happening through the 'offset' property (I think I posted it here correctly)

    Thanks for your help / confirmation of the

    Option Explicit
    
    Private Sub UserForm_Initialize()
    
    Dim UB As Variant
    Dim UBc As Long
    Dim lcount As Long
    Dim x As Integer
    Dim j As Integer
    Dim c As Variant
    Dim ArrayToListBox()
    
    Set UB = Range(Worksheets("DATA").Range("A1"), Worksheets("DATA").Range("A" & Rows.Count).End(xlUp)).Offset(1, 0)
    UBc = Range(Worksheets("DATA").Range("A1"), Worksheets("DATA").Range("A" & Rows.Count).End(xlUp)).Count
    
    
    ReDim ArrayToListBox(1 To UBc, 1 To 5)
    
    x = 1
    For Each c In UB
        ArrayToListBox(x, 1) = c.Value
        x = x + 1
    Next c
    x = 0
    
    
    For x = 2 To 5
        If x <> 5 Then j = x Else j = 9
        For lcount = 1 To UBc
            ArrayToListBox(lcount, x) = Worksheets("DATA").Range("A1").Offset(lcount, j - 1)
        Next lcount
    Next x
    
    ResultsData1.ColumnCount = 5
    ResultsData1.List = ArrayToListBox()
    
    'ResultsData1.Clear
    
    End Sub

+ 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. Nest if statement with Do while loop
    By cajand in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-16-2015, 08:23 AM
  2. Problem trying to nest IF statement
    By club rat in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 02-24-2014, 12:15 PM
  3. [SOLVED] Can I nest Countifs Statements in an IF / OR statement?
    By CWatsonJr in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 10-02-2013, 04:13 PM
  4. Replies: 3
    Last Post: 03-20-2012, 02:43 AM
  5. How to nest three AND conditions in an if statement
    By Halfdef in forum Excel General
    Replies: 12
    Last Post: 05-09-2011, 11:04 AM
  6. Nest Min in If statement
    By erock24 in forum Excel General
    Replies: 6
    Last Post: 06-20-2007, 11:20 AM
  7. [SOLVED] Nest IF statement workaround
    By [email protected] in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-26-2006, 11:35 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