+ Reply to Thread
Results 1 to 20 of 20

loop that keeps producing a next without for error

  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    loop that keeps producing a next without for error

    I'm not entirely sure what I am doing wrong here but if someone could set me right it wold be appreciated. I know it has something to do with the way I am trying to complete the loop.

    I have tried next i, I have tried if's instead of the do until but can't get it to work.

    I want it to cycle through the x and if a value is found then paste into the corresponding i row. If the x value is blank then exit the loop.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: loop that keeps producing a next without for error

    Several issues:

    1. Your nesting of your For... Next and Do... Loop overlap illogically. Do you want to perform the Do... Loop for each x value? (in which case swap lines "Next i" and "Loop" in your code)
    2. You stated For x = 1 to 10, then close that with Next i. Which variable are you trying to loop through - x or i? If it is x then this line should be "Next x".
    3. Your code has an undimensioned variable: CellRefx
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    Well you can tell I haven't been to bed yet can't you lol.

    1. I've swapped the bottom of the code to the following

    Please Login or Register  to view this content.
    2. Great spot on the i/x issue (I wanted next x).

    3. The x of CellRefx is referring to the numbers already defined as ranges. Do I need to dim CellRefx as range as well?

    I've done all you've recommended and there's still no msgbox appearing?

  4. #4
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    OK, saw what I did wrong however I have an interesting quirk happening.

    How do I define the i in the cell references as a number because right now Gi:Ii etc are merging instead of G4:I4?

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: loop that keeps producing a next without for error

    You can't append a variable as a value in the way you are trying it.

    CellRefx doesn't become CellRef1 when x is 1. You'd be better declaring CellRef(10) as an array, then referring to CellRef(x).

    Similarly with your ranges: "Gi:Ii" will not be recognised as "G4:I4" when i is 4. You could refer to that range as
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    for example.
    Last edited by Olly; 03-24-2014 at 05:32 AM.

  6. #6
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    Ok, range sorted (cheers) but not getting the array part.

    Can you explain the CellRef(10) I'm guessing this is setting the limited of my array but shouldn't it be CellRef(1 To 10) as I don't want a 0?
    How do I then refer to CellRef(x)?
    What happens to the For x = 1 to 10 which drives the loop?

  7. #7
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: loop that keeps producing a next without for error

    It's hard to properly unpick your code without an attached workbook, but look at something like this:
    Please Login or Register  to view this content.
    See the different ways we can use variables to refer to ranges?

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: loop that keeps producing a next without for error

    If you want more help with this, I'll really need you to attach a workbook so I can test the code against your actual workbook structure. Don't think you're far off though.

  9. #9
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    OK the module is called "Functions" and the button running code is "Costs & Closures"

    There is a lot happening with this book so please ignore everything else otherwise you will get swamped.

    There is a module called Documents_History which is currently set up to generate the header on the first tab, this code is about producing the info to create a grid below it of all fields in database that has a value.
    Attached Files Attached Files

  10. #10
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: loop that keeps producing a next without for error

    Two questions, before I look at your workbook:

    Which version of Excel are you using? Your profile says 2003, but your attachment is >2007 format?

    What do you actually need help with now - from the code I posted above, which bit doesn't work, or don't you understand well enough to tweak to work? There's no point me spending time covering stuff you are already capable of doing yourself, let's identify and solve the problem that you need help with.

  11. #11
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    Umm, you might want to hold on, I think I've just uploaded an earlier version of the book and deleted the newest version.

    Work uses 2003 but I use 2010 at home. I am currently using 2010

  12. #12
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: loop that keeps producing a next without for error

    Quote Originally Posted by Sc0tt1e View Post
    OK the module is called "Functions" and the button running code is "Costs & Closures"

    There is a module called Documents_History which is currently set up to generate the header on the first tab, this code is about producing the info to create a grid below it of all fields in database that has a value.
    Nope. Not seeing any of that in your attachment.

  13. #13
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    OK, I thought I had just lost over a weeks worth of work but managed to find the document in the temp files.

    I was about to have a serious hissy fit lol.

    Updated document attached
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    It loops through (I think) and I get the msgbox result but it doesn't seem to actually do anything (like make the grid, paste the values and make a new line for each result)

  15. #15
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: loop that keeps producing a next without for error

    Sorry, had to go do some real work.

    Just had a look at your workbook now.

    Your Do ... Loop will never end, as nothing is changing in either the reference or the value of CellRef(x).

    What is it you are trying to achieve? As I understand it: you enter a search string, find the matching row in Sheet2, and for each document that exists in columns BA:CG, copy those values into Sheet1, with some formatting. Right?

    Try this:
    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    I've had a chance to get a bit of sleep now and realised I concentrated more on getting the loop right without checking what was driving it.

    I need the CellRef(x) to drive the loop, If a value if found then a line in sheet1 in the merged cells G-S is created and the values for that CellRef(x).value are pasted, if there is no value then the loop finishes.

    I'm not sure how to make CellRef(x) the driving factor.

  17. #17
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: loop that keeps producing a next without for error

    See the code I just posted. It loops through all ten x values, checks if a matching value is present in the associated column of Sheet2, and if there is a value, copies the data across to Sheet1.

  18. #18
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    Yes, basically it is a way of showing the user all the documents that have been either received or generated on a case (that are stored in the database tab).

    I thought the CellRef(1-12) would set the limit for the loop, either it finds all 12 values or when it reaches an empty value it stops looping. and then the i = i + 1 would just keep creating lines until the loop ends.

    Am I also right in assuming the Do Until loop is the right way of doing this as it checks the values before carrying out the loop rather than the Do Loop Until?

    Should this line not be x, as there are 2 lots of i's?
    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: loop that keeps producing a next without for error

    Please Login or Register  to view this content.
    I'm not sure what the (3 * i)) is doing in this bit of code, also shouldn't the i be an x?
    Otherwise you seem to set x = 1 - 10 as the start of the loop but don't use it anywhere in the loop except next x. Also you use the i in 3 locations?

  20. #20
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: loop that keeps producing a next without for error

    Okay. This chunk of my code:
    Please Login or Register  to view this content.
    Replaces this chunk of your code:
    Please Login or Register  to view this content.
    Instead of 10 variables, we use one array variable. Values 3 to 10 follow a regular pattern to increment the column numbers, so we don't need to write a separate line for each variable - we can loop through them. So, when i is 3, we set CellRef(3) to be S2LI.cells(RowNumba, 62). When i is 4, we set CellRef(4) to be S2LI.Cells(RowNumba,65). And so on. It's just a way of reducing / simplifying the code.


    Now, as for looping through x from 1 to 10:
    Please Login or Register  to view this content.
    I have highlighted in red all instances of the x variable in this loop. We are essentially using the x variable to loop through all the (potential) source data, and using the i variable to format and write the output range.

    I hope that helps clarify what the code is doing. Now, time for ME to ask you another question: does it achieve what you expected? And if not, what needs changed?


+ 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. [SOLVED] Code producing error, only on third run of Userform...?
    By RayJay01 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-12-2019, 10:01 PM
  2. [SOLVED] Workbook Event Producing an Error
    By BeachRock in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2014, 01:42 PM
  3. [SOLVED] IF-Then-Else loop stopping and producing runtime error 1004
    By Optiprime in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-07-2013, 03:24 AM
  4. Array loop producing error "Run time error '1004'"
    By jeskit in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-06-2011, 08:56 AM
  5. VBA code suddleny producing an error
    By drgogo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-15-2010, 07:18 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