+ Reply to Thread
Results 1 to 13 of 13

For Each Cell in Range Enter Value

  1. #1
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    For Each Cell in Range Enter Value

    Hi all,

    I've been having trouble figuring out how to do this.
    What I want to do is something like this:

    Please Login or Register  to view this content.
    I've tried to run and change this code, but Excel will crash every time.
    I'm not familiar enough with VBA to figure out why. Any help?

    Thanks!
    Last edited by Storm08; 08-18-2022 at 11:10 AM. Reason: Mistakes in code

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: For Each Cell in Range Enter Value

    Could you attach a sample workbook so we can see the issue in context? It would make it a lot easier to fathom what the issue is.

    BSB

  3. #3
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,193

    Re: For Each Cell in Range Enter Value

    Works fine for me in that data is placed in the range: what error do you get?
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  4. #4
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    Re: For Each Cell in Range Enter Value

    Thanks for the quick responses!

    Workbook attached. I've removed the rest of the data in the workbook, but the error "Out of stack space" still remains. I assume that means it gets stuck in a loop?
    Attached Files Attached Files

  5. #5
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: For Each Cell in Range Enter Value

    Every time the worksheet change event runs, it changes a cell value which in turn runs the change event again, and again..... Eventually you run out of memory and get that error.
    Try this:
    Please Login or Register  to view this content.
    It will turn the event trigger off whilst the code runs, then back on again ready for the next worksheet change.

    BSB

  6. #6
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    Re: For Each Cell in Range Enter Value

    Quote Originally Posted by BadlySpelledBuoy View Post
    Every time the worksheet change event runs, it changes a cell value which in turn runs the change event again, and again..... Eventually you run out of memory and get that error.
    Try this:
    Please Login or Register  to view this content.
    It will turn the event trigger off whilst the code runs, then back on again ready for the next worksheet change.

    BSB
    BSB, Thank you very much. That makes a lot of sense.

    I've tested it and it no longer crashes excel, but the value I need in each cell isn't correct.
    What would be the best way to get the range to count up to the value in U11? For instance, the value in U11 is 8, so the cells in the range need display:
    1/1
    2/1
    3/1
    4/1
    5/1
    6/1
    7/1
    8/1

  7. #7
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: For Each Cell in Range Enter Value

    You need to increment your rowNum variable each turn of the loop.

    Please Login or Register  to view this content.
    BSB

  8. #8
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    Re: For Each Cell in Range Enter Value

    Ah, cool.

    Would it also be okay to do this?:
    Please Login or Register  to view this content.

  9. #9
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: For Each Cell in Range Enter Value

    Absolutely. Although perhaps it should be -31 rather than -32?

    When I tested your code the first value was 0/1

    BSB

  10. #10
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    Re: For Each Cell in Range Enter Value

    Quote Originally Posted by BadlySpelledBuoy View Post
    Absolutely. Although perhaps it should be -31 rather than -32?

    BSB
    ...that is why I have excel to do maths for me! Haha, thank you.

    Lastly, what would I add to make the For loop stop when rowNum gets to a certain value? Would that just be an If statement at the first line of the for loop to create the condition?

  11. #11
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: For Each Cell in Range Enter Value

    Certainly one way of doing it.
    Please Login or Register  to view this content.
    Change 10 to whichever number you want to stop at.
    Alternatively, change the "Set cirNum = Range...." line to only include the number of rows you want to run it for.

    Which is the best option would very much depend on your preference. I have little idea of what you're actually trying to do with this workbook so not best placed to answer that for you.

    BSB

  12. #12
    Forum Contributor
    Join Date
    01-12-2012
    Location
    UK
    MS-Off Ver
    Excel 2016
    Posts
    104

    Re: For Each Cell in Range Enter Value

    Ah, I didn't know you could do an Exit For rather than Exit Sub.

    That's more than enough education for me to do what I need to with this.
    Thank you very much BSB!

  13. #13
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: For Each Cell in Range Enter Value

    Very happy I could be of assistance and pass on a little knowledge

    Good luck with the project and you know where we are if you need more help.

    BSB

+ 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] If Cell-X not equal to any cell in Range-Y, then enter value into Range-Y
    By mruu in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-14-2019, 01:58 AM
  2. locking and unlocking range of cell automatically when enter data in cell
    By pchrakesh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-04-2019, 08:57 AM
  3. Vba code when cell in range returns a certain value, enter formula in nearby cell
    By dsrt16 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-06-2018, 08:16 AM
  4. [SOLVED] If cell meets date requirement enter 1, if not enter 0, if blank don't enter anything.
    By billrogers184 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-11-2017, 02:04 PM
  5. VBA to enter name in cell range and not to enter if alread there
    By superchew in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-01-2015, 09:40 PM
  6. Replies: 7
    Last Post: 09-17-2013, 03:42 PM

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