+ Reply to Thread
Results 1 to 12 of 12

Fill array till first empty cell.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-02-2009
    Location
    US, TN
    MS-Off Ver
    Excel 2003
    Posts
    7

    Fill array till first empty cell.

    I thought that this would be so easy. Here's what I have:

    Sub make_array3()
    Dim myarray() As Variant
         Do
            myarray(i) = Sheets("Sheet1").Cells(i, 3).Value
            i = i + 1
        Loop Until IsEmpty(i)
            
    Sheets("Sheet1").Range("D2:D10").Value = myarray
    
    End Sub
    The excel cells with values start in C1 on the Sheet1 tab.
    I get a run time error 1004 in the line: myarray(i) = Sheets("Sheet1").Cells(i, 3).Value

    I am only recopying the data to Column D to see if it is working.
    Please tell me it is something simple.

    ducc

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Fill array till first empty cell.

    In the initial iteration, i is zero, and there is no row zero.

    If you fixed that, the IsEmpty(i) would never be true.

    What (exactly) do you want the code to do?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    10-02-2009
    Location
    US, TN
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Fill array till first empty cell.

    I have text in Col C of sheet Sheet1.

    I would like the values in Col C to be entered into the array 'myarray'.

    I would like to stop this process when the first empty cell in Col C is reached.

  4. #4
    Registered User
    Join Date
    10-02-2009
    Location
    US, TN
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Fill array till first empty cell.

    OK...change is good.

    Sub make_array3()
    
    
    Dim myarray() As Variant
    
    
        Do
            i = 1
            myarray(i) = Sheets("Sheet1").Cells(i, 3).Value
            i = i + 1
        Loop Until IsEmpty(Cells(i, 3))
            
    
    Sheets("Sheet1").Range("D2:D10").Value = myarray
    
    
    End Sub

    But now I get a Script out of range error in the line:
    myarray(i) = Sheets("Sheet1").Cells(i, 3).Value

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Fill array till first empty cell.

    If you have no values below the first blank another alternative avoiding iteration might be:

    Public Sub Example()
    Dim myArray As Variant
    With Sheets("Sheet1")
        myArray = .Range(.Cells(1, "C"), .Cells(.Rows.Count, "C").End(xlUp))
        .Cells(2, "D").Resize(UBound(myArray, 1)).Value = Application.Index(myArray, 0, 1)
    End With
    End Sub

  6. #6
    Registered User
    Join Date
    10-02-2009
    Location
    US, TN
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Fill array till first empty cell.

    Without a doubt it works.
    (How does the topic get a [solved]...is that my job?)

    I don't understand s few of the pieces. And I would still love to know where
    I went wrong in the code I was building. But I can move on with what
    you provided me.

    Thank you DonkeyOte. May all your Wind Mills be Dragons.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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