Runtime error with "do...until"loop

shabbyporpoise

New Member
Joined
Jul 22, 2011
Messages
26
Hello Forum I'm new to excel vba, I have been using this forum quite a bit to search for similar problems. I have found the forum very helpful. With my work we use excel quite a bit, and my employer wants me to learn vba to streamline the worksheets we use. My program has nothing to do with work, it is a personal project however everything would apply to work as well. back story done. I am encountering a Runtime error 1004 "Application-defined or object-defined error I have a list of 500 movies in column A, I am trying to have a loop that goes through the entire list, stops when there are no more items and put all of these items in an array. Thanks for your help forum ---------------------- Option Explicit Sub movieList() Dim num As Integer, names As String, i As Integer Dim rowCount As Variant, myMovies() As Variant i = 0 Do Until IsEmpty(Cells(i, 1)) rowCount = Array("a1" & i) myMovies(i) = Cells(i, 2).Value i = i + 1 MsgBox myMovies(i) Loop End sub -----------------
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I am trying to figure out how to input my code in the nice windows that everyone else is. I am very new to this website. and to forums in general. Please be patient please tell me how to make my posts look nice Thanks
 
Upvote 0
Hello Forum I'm new to excel vba, I have been using this forum quite a bit to search for similar problems. I have found the forum very helpful. With my work we use excel quite a bit, and my employer wants me to learn vba to streamline the worksheets we use. My program has nothing to do with work, it is a personal project however everything would apply to work as well. back story done. I am encountering a Runtime error 1004 "Application-defined or object-defined error" I have a list of 500 movies in column A, I am trying to have a loop that goes through the entire list, stops when there are no more items and put all of these items in an array. Thanks for your help forum ----------------------
Code:
 Option Explicit  
Sub movieList()     
Dim num As Integer, names As String, i As Integer    
 Dim rowCount As Variant, myMovies() As Variant         
 i = 0     
Do Until IsEmpty(Cells(i, 1))        
 rowCount = Array("a1" & i)         
myMovies(i) = Cells(i, 2).Value        
 i = i + 1         
MsgBox myMovies(i)    
 Loop
-----------------
 
Last edited by a moderator:
Upvote 0
Maybe

Rich (BB code):
Sub movieList()
Dim num As Integer, names As String, i As Integer
Dim rowCount As Variant, myMovies()
i = 0
Do Until IsEmpty(Cells(i, 1))
    rowCount = Range("A" & i)
    ReDim Preserve myMovies(1 To i)
    myMovies(i) = Cells(i, 2).Value
    i = i + 1
    MsgBox myMovies(i)
Loop
 
Upvote 0
VoG- the changes you suggested worked great! this was a huge help. A quick question I have about your code(if your too busy don't worry about it) how come you did not put "as variant" after myMovies()?

thanks again VoG:)
 
Upvote 0
It will work with Variant but you need to initialize i = 1 and move the line where i is incremented

Code:
Sub movieList()
Dim num As Integer, names As String, i As Integer
Dim rowCount As Variant, myMovies() As Variant
i = 1
Do Until IsEmpty(Cells(i, 1))
    rowCount = Range("A" & i)
    ReDim Preserve myMovies(1 To i)
    myMovies(i) = Cells(i, 2).Value
    MsgBox myMovies(i)
    i = i + 1
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top