+ Reply to Thread
Results 1 to 4 of 4

Create list of non-contiguous cells and loop back to overwrite values

  1. #1
    Registered User
    Join Date
    11-06-2017
    Location
    Edmonton, Canada
    MS-Off Ver
    2016
    Posts
    2

    Create list of non-contiguous cells and loop back to overwrite values

    Hello All,

    I'm a self taught VBA novice and I am in need of assistance. I am looking to make a list based on cell values in different sheets within the same workbook. I am also looking to create a loop when the list reaches, say 10 cells of data, to loop back to the first cell (Oldest value) and record a new value. Example: I have 4 sheets. Cell B2 in Sheet 1 has the word "Bear" and Cell B2 in Sheet 2 has the word "Ball". Sheet 3 Cells B2 and E2 have the words "Wall" and "Echo". Cells B2 and E2 in Sheet 4 have the words "Plant" and "Fin". How would I create a list in Sheet 5 of all those values? Secondly, how would I put a maximum on the sheet 5 range. So when it has all 6 values it continues to list them when they change but once it reaches row 10 it'll loop back to first cell and change that value. So if sheet 1 cell B2 changes to "Apple", Sheet 3 Cells B2 and E2 change to "Wolf" and "Guy", Sheet 4 Cells B2 and E2 now say "Pool" and "Blink" the data would look like:

    Sheet 5 (Initially):
    1 Bear
    2 Ball
    3 Wall
    4 Echo
    5 Plant
    6 Fin
    7 Apple
    8 Wolf
    9 Guy
    10 Pool


    Sheet 5 (After):
    1 Blink
    2 Ball
    3 Wall
    4 Echo
    5 Plant
    6 Fin
    7 Apple
    8 Wolf
    9 Guy
    10 Pool


    The Range would be a set number (10 in this case) that would continue to update itself. I have somewhat managed to extract the values to create a list but I cant loop it back to overwrite the old values. Is this possible? Any help would be appreciated!
    Last edited by mechvba; 11-07-2017 at 01:00 PM. Reason: Spacing issue

  2. #2
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Create list of non-contiguous cells and loop back to overwrite values

    Rng(x) (where Rng is a Range and x is an integer) will reference the cell with an offset x-1 rows from Rng. Example: Range("A1")(5)=2 will put a 2 in cell A5.
    Rng.offset(x) will reference a cell with an offset x rows from Rng. Example: Range("A1").Offset(3)=9 will put a 9 in cell A4.

    Use either of those to access your list by changing x.

    To prevent x from 'overflowing' you can create a variable for x and either keep incrementing it and then use the mod operator, or just keep incrementing it and then when it hits its overflow value, set it back to 0 or 1 (whichever is appropriate).

    There are two tricks, though. One is to update the list when a cell changes. The other is to remember what the last cell was that changed.

    The first problem is resolved by using the worksheet.change event. Google it to see how that is used.

    The second problem should not be an issue if you allow the list to start at the same element every time the spreadsheet is opened (likely you would just start at the top). The 'x' variable used in your code will remember its value through the change events. Since you likely want x to be seen across modules, you will need to declare it as a global public variable (again, google for more details). If you do want it to remember where you left off writing in your list between the file being closed and open again, then you just need to store the x value in a cell somewhere in your workbook. When you start, you'd just get the value from that cell and use it - in fact, in this case, you would not use x as a variable, but would just increment, mod, and use that cell value as your offset.
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

  3. #3
    Registered User
    Join Date
    11-06-2017
    Location
    Edmonton, Canada
    MS-Off Ver
    2016
    Posts
    2

    Re: Create list of non-contiguous cells and loop back to overwrite values

    Thanks for your input! I managed to extract the list. I am stuck on the 'overflowing' problem. I am not sure how I would tell the macro to loop back to top after like 20 rows and overwrite the value.

  4. #4
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Create list of non-contiguous cells and loop back to overwrite values

    Revisiting... Not sure if you resolved this or not, but with a global public variable, you would just keep incrementing 'x' and then have a check with something like
    if x>20 then x=0

+ 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. Replies: 6
    Last Post: 09-29-2015, 12:11 AM
  2. [SOLVED] Loop thru non-contiguous selection of cells
    By btmtdk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2014, 05:52 PM
  3. Create a loop to bring back multiple cells from 1 worksheet to another
    By flintcs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-22-2013, 04:59 PM
  4. [SOLVED] userForm loop that returns info back into cells (with some cells not included)
    By Sleepyshy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-13-2012, 12:04 PM
  5. Replies: 10
    Last Post: 03-19-2012, 05:18 PM
  6. Match Value to a Non-Contiguous List of Values
    By bruce42 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-18-2010, 01:41 PM
  7. Replies: 1
    Last Post: 05-26-2006, 09:50 PM

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