+ Reply to Thread
Results 1 to 42 of 42

Stopping loop in Range if there is no value in row

  1. #1
    Registered User
    Join Date
    10-04-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Stopping loop in Range if there is no value in row

    Hi,

    I have a macro where it identifies a blank cell and inputs the word "ERROR" into it if it has no value (which could be a number/word). I have identified the range as 4000 rows so that it can be used on a monthly basis (as there is no way to determine how many rows I receive).

    However the issue is that this macro will populate blank cells within blank rows with "ERROR" as I have identified the range. Is there anyway to stop it when it gets to a completely blank row (please see code below)?

    Please Login or Register  to view this content.

    Any help/advice would be much appreciated!!

    Thanking you in advance

    EM1987

    MODERATOR EDIT: Code Tags Added. Please comply with our rules.
    Last edited by alansidman; 10-11-2013 at 09:43 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    EM1987,
    Please use code tags with your code as per forum's rule.
    You can add

    Exit for

    below

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    04-25-2013
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: Stopping loop in Range if there is no value in row

    Assuming that you have at least one column which always has data, you could use the following:

    Please Login or Register  to view this content.
    It will find the last row in your worksheet based on your column A and then add data to C.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    You can use your existing code.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-04-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Stopping loop in Range if there is no value in row

    Thanks for this, but it stops after the first cell has been changed. Is there any way to base it on the other cells in the range i.e. Rows 1-4000, so if there is anything in those rows then the cell value would change to "ERROR"; but if the row is blank then it will skip those rows?

    (Apologies about the code tags... didn't know about that rule but I'll make sure that I do it in the future!)

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    Is there any column that will always be fully populated?

    Does this work?
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    I am not sue what you are asking.
    The code loops from C2 until the last row in column C. So, if you rows in C do not go far, you are testing the wrong column.
    Since you are testing column C, the code matches the lastrow with the last data in that column.
    What if the code finds the first empty? If you want to exist the loop once an empty row in column C is found, do you want to exit the loop or? If so, add the exit for as I indicated in my first post.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Norie,

    Is there any column that will always be fully populated?

    IMO, If one is testing for column C, I thought the last row with the data should come from that column.

  9. #9
    Registered User
    Join Date
    10-04-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Stopping loop in Range if there is no value in row

    Unfortunately there is no guarantee that all cells in each column will be populated, but there will be data in each row and this is to pick up blank cells if there is some data in each row.

    Thanks

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    If column C is the column with blanks it's probably not a good idea to use it to find out the total no of rows of data.

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    There is no each column in your code.

    Please Login or Register  to view this content.
    This line loops from C2 to C4000, so You are ONLY looping through column C, no other column.

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Norie,
    Yes, I agree, as long as you are looking for the last row in a data (All columns), but the OP code is looping through column C

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    So there's only data in column C?

  14. #14
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Norie,
    I do not know!

    I am only going by the loop in column C, so no need to check other columns for last data, if you are testing column C.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    I'm pretty sure there's data in other columns, have a look at what the OP's post #5.

  16. #16
    Registered User
    Join Date
    10-04-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Stopping loop in Range if there is no value in row

    There is data in most of the columns (A-I), but the rows vary from week-to-week. So the loop is to pick up missing data in column c, and should be based on whether columns A,B,D,E etc have data in it

  17. #17
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Okay, this will pick-up the last row in whole data.

    Please Login or Register  to view this content.

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    The code I posted should replace any blanks in the data with ERROR.

    If you only want the blanks in column C changed you can use this.
    Please Login or Register  to view this content.

  19. #19
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Another option to replace blanks in column C


    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    10-04-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Stopping loop in Range if there is no value in row

    It worked, thanks for your help!!

  21. #21
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Or

    Please Login or Register  to view this content.

  22. #22
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    AB33

    That's kind of like the code I posted.

  23. #23
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Norie,
    Yes, your understanding was correct from outset, except, you wanted to evaluate all columns , while the OP code says column C.
    I still think that using a column C as the last row evaluator works. If the last blank row in column C is in row 3 and the last row in the data is 1m, why does the code need to loop through all these rows while there are no data in column C after row 3?

  24. #24
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    Have a look at the last code I posted.

  25. #25
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Norie,
    Both of your codes are right, but the first code should use the last row in entire data as you are evaluating all columns. So to make sure that you capture all data in all columns, you look for other columns with last row data.

    My point was based on OP's code testing column C.
    I still do not see why the code needs to find the last row in other columns while you are testing column C.
    E.g may help.

    Please Login or Register  to view this content.
    I have data in column C in rows 2 and 3.
    Now, If I were to use the LR, the code loops until the last row in the data, while LR1 stops at row 3, which is the point.
    Why the code needs to loop through the longest row, while there are no data in column after row 3 to test.
    The code should stop looping at the last row in column C, which is row 3.
    I hope you get my point.

  26. #26
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    The last code I posted isn't evaluating all columns.

    It finds all the blanks in the used range and then intersects that with column C to return only the blanks in column C.
    Last edited by Norie; 10-11-2013 at 11:32 AM.

  27. #27
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Norie,
    I was referring to post #6

    "Is there any column that will always be fully populated?"

  28. #28
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    What if there was data in A1:A10, B1:B10, C1:C7 and D1:D10?

    If you used column C to find the last row the blanks in C8:C10 would be ignored.

  29. #29
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Norie,
    The question origin is

    Please Login or Register  to view this content.
    Why are you brining other columns in the equation while you are testing column C?

  30. #30
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    To find the last 'real' row of data.

    In the example I gave in the last post that would be row 10, not row 7.

  31. #31
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    IN the first line one you have C1:C7 , which implies the last row in column C is 7, why do you not to test blanks in C8:C10?
    Last edited by AB33; 10-11-2013 at 11:53 AM.

  32. #32
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Are you implying this line will not find the last "REAL RAW DATA" in column C?

    Please Login or Register  to view this content.

  33. #33
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    What row does Cells.Find("*", , , , xlByRows, xlPrevious).Row return if you have the data set up as I suggested in the previous post?

    That's the code you used and the OP said works and it would return 10 not 7.

  34. #34
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Please Login or Register  to view this content.
    Will return the last row in the active sheet.

  35. #35
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    Quote Originally Posted by EM1987 View Post
    There is data in most of the columns (A-I), but the rows vary from week-to-week. So the loop is to pick up missing data in column c, and should be based on whether columns A,B,D,E etc have data in it
    I think the OP might be saying here to base the last row on columns A, B, D, E etc and not column C.

  36. #36
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Return =10

    Please Login or Register  to view this content.
    Return=7

    I do not need to loop through 10 rows, while the column I am testing last row is 7.

  37. #37
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    If you want to fill in the blanks in C8:C10 you might want to loop to row 10.

  38. #38
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    Norie,
    Okay!
    Good point!
    But my reading was based on the first line of the code.

  39. #39
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    But why do I need to fill in(Test it) rows 8 and beyond, while my last row for which I am testing a column says 7?

  40. #40
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    You shouldn't be using the column you are testing to find the last row of data.

    If you have data in A1:A100, B1:B100, C1:C50, D1:D100 which is the last row of data?

  41. #41
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Stopping loop in Range if there is no value in row

    to find the last row of data of which column? I am not interested in columns A-B and D. I am testing

    Please Login or Register  to view this content.
    which clearly says column C, not

    Please Login or Register  to view this content.
    Yes , if I am using the second line, but the OP words and the first line of the code are not the same.

  42. #42
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Stopping loop in Range if there is no value in row

    Why aren't you interested in column A, B and D?

    They, as well as column C, make up the data.

    What if you wanted to put a formula in column E down to the last row of data?

    Would you use column C?

+ 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] Loop Not Stopping
    By Dummy99 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-17-2016, 03:58 PM
  2. I need help stopping a loop in excel macros
    By vpapolu24 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2013, 04:44 AM
  3. Copy and Paste Loop but keeps stopping
    By kksf in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-27-2013, 04:03 AM
  4. Loop is stopping for no apparent reason.
    By Melissa9 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-02-2012, 03:08 PM
  5. For Loop not looping through whole range stopping after 1 cell
    By jeskit in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-19-2011, 08:43 AM

Tags for this Thread

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